Findcontours crash: unsupported format
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);
}
common misconception here:
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 !