Ask Your Question
0

findContours - unhandled exception

asked 2015-02-28 15:17:55 -0600

adliner gravatar image

updated 2015-03-01 07:12:59 -0600

Hi guys, I've just began learning how to use OpenCV library. I'm using VS2013(update 3) + OpenCV 3.0 Beta.

I'm encountering some error when trying to use findContours(). I get some unhandled exceptions in opencv_world300.dll. I've searched the web for findContours() related errors and I know that it should get a single channel B&W image, so that's what I do:

int _tmain(int argc, _TCHAR* argv[]) { cv::Mat frame, frame_gray, threshImg; cv::Mat back; cv::Mat fore; cv::VideoCapture cap; cap.open(1);

if (!cap.isOpened())
{
    std::cerr << "ERROR: Could not access the camera!" << std::endl;
}

Ptr<BackgroundSubtractorMOG2> bg = createBackgroundSubtractorMOG2(500, 16, false);


vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

cv::namedWindow("Frame");
cv::namedWindow("Foreground");
cv::namedWindow("Background");

for (;;)
{
    cap >> frame;

    bg->apply(frame, fore);
    bg->getBackgroundImage(back);
    cv::erode(fore, fore, cv::Mat());
    cv::dilate(fore, fore, cv::Mat());

    cvtColor(fore, threshImg, CV_BGR2GRAY);
    findContours(threshImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    cv::imshow("Frame", frame);
    cv::imshow("Background", back);
    cv::imshow("Foreground", fore);

    if (cv::waitKey(30) >= 0) break;
}

return 0;

}

Any clue what I'm doing wrong? :)

Thanks!

UPDATE: Also I tried the code found at: http://stackoverflow.com/questions/84...

int countours(Mat _image) { cv::Mat image = _image; if (!image.data) { std::cout << "Image file not found\n"; return 1; }

//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);

//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = image.clone();
cv::findContours(contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);

//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0, 0, 0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
    cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}

cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);

return 0;

}

and I'm also hitting a break point with a 'Critical error detected c0000374' message in debug output.

UPDATE 2: Looks like it's a bug in opencv_world300.dll - I renamed opencv_world300d.dll to opencv_world300.dll and placed it binary directory and it works now. It must be some bug in release version of library, maybe some compiler optimization doesn't work properly.

edit retag flag offensive close merge delete

Comments

10x your solution works for me.

Lucullus gravatar imageLucullus ( 2015-11-07 16:10:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2016-10-29 08:51:15 -0600

MSX01 gravatar image

updated 2016-10-29 16:50:06 -0600

I had the same problem with exceptions in OpenCV3 with VS2012.

The solution in my case was to declare the contour und history variables globaly, and not inside my function.

This throws an exception at findContours:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

DECLDIR long BCBcvCheckPresence(Mat *SourceImage) {

    vector<cv::Mat> contours;
    vector<Vec4i> hierarchy;
    findContours(&SourceImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
}

then i tried this and it works without exceptions:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

vector<cv::Mat> contours;     //new position
vector<Vec4i> hierarchy;       //new position

DECLDIR long BCBcvCheckPresence(Mat *SourceImage) {

    findContours(&SourceImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
}
edit flag offensive delete link more

Comments

sorry, but this is a bad answer.

your problem is not the same as in the question, in your case, it fails, because you're using pointers to cv::Mat, which is already a "smart pointer", and thus you're leaking refcounts.

please AVOID passing pointers to cv::Mat around in the 1st place.

berak gravatar imageberak ( 2016-10-29 20:06:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-28 15:17:55 -0600

Seen: 4,603 times

Last updated: Oct 29 '16