Ask Your Question

RLRobinson's profile - activity

2019-12-01 14:51:21 -0600 received badge  Famous Question (source)
2019-01-11 04:27:15 -0600 received badge  Notable Question (source)
2018-05-15 04:05:11 -0600 received badge  Popular Question (source)
2016-09-17 21:37:44 -0600 commented answer error calling contourArea

Doh! Thanks.

2016-09-17 21:37:32 -0600 received badge  Scholar (source)
2016-09-17 18:31:17 -0600 asked a question error calling contourArea

Basic motion detection, not sure why error below is being thrown, how to fix?

OpenCV Error: Assertion failed (npoints >= 0 && (depth == CV_32F || depth == CV_32S)) in contourArea, file /home/jshmo/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp, line 314 terminate called after throwing an instance of ‘cv::Exception’ what(): /home/jshmo/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp:314: error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function contourArea

int main (int argc, char * const argv[])
{
   const string EXT = “.jpg”; // extension of any images 
       if (argc<3) {
       fprintf(stderr, “Usage: motion INPUT_FILE_NAME OUTPUT_FILE_NAME [ObjectSizeSensitivity]\n”);
       exit(1);
    }
    double detected_area; 
    int min_area = atoi(argv[3]);
    Mat kernel_dilate = getStructuringElement(MORPH_RECT, Size(3,3));
    VideoCapture camera(argv[1]);
    Size size2 = Size(1920, 1080);
    int codec = CV_FOURCC(‘M’, ‘P’, ‘E’, ‘G’);
    VideoWriter out(argv[2], codec, 6.0, size2, true);

    Mat result, new_frame, avg, avgconv, frameDelta, thresh;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    camera.read(new_frame);
    avg = Mat::zeros(new_frame.size(), CV_32FC1);
    // 
    while ( 1 ){
        //Take a new image
        camera.read(new_frame);
        if(!new_frame.empty()) { 
        result = new_frame;
        cvtColor(new_frame, new_frame, CV_RGB2GRAY);
        GaussianBlur(new_frame, new_frame, Size(21, 21), 0);
        accumulateWeighted(new_frame, avg, 0.5);
        convertScaleAbs(avg,avgconv);
        absdiff(new_frame,avgconv,frameDelta);
        threshold(frameDelta, thresh, 35, 255, THRESH_BINARY);
        dilate(thresh, thresh, kernel_dilate, Point(-1, -1), 2, BORDER_CONSTANT);
        findContours(thresh, contours, hierarchy, RETR_EXTERNAL,CHAIN_APPROX_SIMPLE, Point(0, 0));

        for(size_t i = 0; i < contours.size(); i++)
         detected_area = contourArea(i);
                if (detected_area > min_area) {
            out << result;
                }
        }
    else
        {
        camera.open(argv[1]);
         }
       }
      return 0;
}