Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findContours error 'support only 8uC1 images'

Trying to run findContours on a binary image"

Mat conv(image.size(), CV_8U);
image.convertTo(conv, CV_8U);
vector<vector<cv::Point> > contours;

findContours(conv, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

thorws error:

OpenCV Error: Unsupported format or combination of formats ([Start]FindContours support only 8uC1 images) in cvStartFindContours, 

Any ideas? Thanks

like image 913
0xSina Avatar asked Nov 02 '12 22:11

0xSina


1 Answers

From the documentation:

C++: void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) const
Parameters:

rtype – desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.

You see that the number of channels is not changed by convertTo, this means most probably that you get 3 channels (r, g and b). However findContours requires a monochrome image.

You need to convert the image to black-and-white:

cv::Mat bwImage;
cv::cvtColor(image, bwImage, CV_RGB2GRAY);
vector< vector<cv::Point> > contours;
cv::findContours(bwImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
like image 64
Vlad Avatar answered Oct 25 '22 06:10

Vlad