Ask Your Question
0

Findcontours crash: unsupported format

asked 2018-03-15 10:47:45 -0600

nickb gravatar image

Hi, I'm trying to find contours on a thresholded image, as far as I can tell this should be very simple. I load in a 16bit image threshold it then try to run findcontours on the output of that. I can see the threshold works properly, but I get an error about unsupported format when trying to run findcontours. I'm following the example code for image type so I can't see whats wrong. I'm using VS2017 with a self compiled OpenCV 3.4.0 with QT support.

Here's the error:

OpenCV Error: Unsupported format or combination of formats ([Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only) in cvStartFindContours_Impl, file C:\opencv\sources\modules\imgproc\src\contours.cpp, line 199

And the code:

#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

int main() {
    Mat image = imread("C:/Users/elp14nab/Desktop/2ms 500 fps 730C/2ms 500 fps 730C/Image7_00726.tif", CV_LOAD_IMAGE_ANYDEPTH );

    Mat globalThresh = Mat(image.rows, image.cols, CV_8UC1);
    threshold(image, globalThresh, 30000, 65535, CV_THRESH_BINARY);

    imshow("Global Threshold", globalThresh);
    waitKey(0);

    vector <vector<Point>> contours;
    vector <Vec4i> hierarchy;
    findContours(globalThresh, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    Mat contourImg = Mat::zeros(image.rows, image.cols, CV_8UC3);
    for (size_t i = 0; i < contours.size(); i++) {
        drawContours(contourImg, contours, (int)i, Scalar(255,0,0), 2, 8, hierarchy, 0, Point());
    }
    imshow("Contours", contourImg);
    waitKey(0);
}
edit retag flag offensive close merge delete

Comments

common misconception here:

 Mat globalThresh = Mat(image.rows, image.cols, CV_8UC1);

it will get overwritten anyway. don't preallocate it, you're only fooling yourself into believing, it would be CV_8U, which it won't be !

also, read docs !

dst output array of the same size and type and the same number of channels as src.

berak gravatar imageberak ( 2018-03-15 11:00:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-15 11:25:23 -0600

nickb gravatar image

Yep this was it, thanks.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-15 10:46:34 -0600

Seen: 2,116 times

Last updated: Mar 15 '18