Ask Your Question
2

When I run the following code, I get the error

asked 2013-05-26 06:12:55 -0600

shubhamc gravatar image

updated 2013-05-26 06:56:16 -0600

Vladislav Vinogradov gravatar image

OpenCV Error: Bad Flag (parameter or structure field) (Unrecognized or unsupported array type) in unknown function, file ......\modules\core\src\array.cpp line 2482

Here's the code:

#include "stdafx.h"

#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "highgui.h"

using namespace std;


int main(int argc, char *argv[])
{
    cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    cv::VideoCapture VideoFeed("test.avi");
    cv::BackgroundSubtractorMOG bg;
    bg.nmixtures = 10;
    bg.history=500;


    std::vector<std::vector<cv::Point> > contours;

    cv::namedWindow("Frame");
    cv::namedWindow("FG");

   while (VideoFeed.isOpened()) // Returns true if video capturing has been initialized already.
    {
        if(VideoFeed.grab()) // Grabs the next frame from video file or capturing device and return true (non-zero) in the 
        // case of success.
        {
            VideoFeed.retrieve(frame);
            bg.operator ()(frame,fore);
            bg.getBackgroundImage(back);
            cv::erode(fore,fore,cv::Mat());
            cv::dilate(fore,fore,cv::Mat());
            cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
            cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
            cv::imshow("Frame",frame);
            cv::imshow("BG",back);

        }
        if(cv::waitKey(30) >= 0) break;
   }
    return 0;
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-05-26 09:32:40 -0600

Qichao Chen gravatar image

updated 2013-05-26 09:40:23 -0600

The correct code should be as follow:

//You should include correct hpp file


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "vector"

using namespace cv;
using namespace std;

int main(int argc, char *argv[])

{
    Mat frame;
    Mat back;
    Mat fore;
    VideoCapture cap;
    //open video
    cap.open("test.avi");

    //Background Model
    BackgroundSubtractorMOG2 bg;
    vector<vector<Point> > contours;

    namedWindow("Input");
    namedWindow("Frame");
    namedWindow("Foreground");
    namedWindow("Background");
    for(;;)
    {
        //read frame
        if(!cap.read(frame))
        {
            return 0;
        }
        imshow("Input",frame);
        //get foreground
        bg(frame,fore,0.01);
        imshow("Foreground",fore);
        //get background
        bg.getBackgroundImage(back);
        //erode image
        erode(fore,fore,Mat());
        //dilate image
        dilate(fore,fore,Mat());
        //find contours
        findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        //draw contours to frame
        drawContours(frame,contours,-1,Scalar(0,0,255),2);
        imshow("Frame",frame);
        imshow("Background",back);

        if(waitKey(33) >= 0)
        {
            break;
        }

    }
    cap.release();
    return 0;

}

Hope can help you.

edit flag offensive delete link more
0

answered 2013-05-26 09:33:01 -0600

berak gravatar image

oh, that's a fun one ;)

the culprit is bg.getBackgroundImage(back);

i tested this with 2.4.2, there we end up in an empty method in the BackgroundSubtractor base class. (in 2.4.9 it throws a not impl exception)

so your "back" Mat is left untouched, still empty, and imshow does not like it. (complains of an empty image, that's your exception)

BackgroundSubtractorMOG2 can do that. (that's the good news)

the background image has to get expensively reconstructed from a Model, that's not a simple member access

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-26 06:12:55 -0600

Seen: 1,661 times

Last updated: May 26 '13