2012-11-02 101 views
16

尝试将二进制图像”上运行findContoursfindContours错误 '只支持8uC1图像'

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错误:

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

任何想法 感谢

回答

19

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.

您看到通道数量没有被convertTo更改,这意味着最有可能获得3个通道(r,g和b)。但是findContours需要单色图像。

您需要将图像转换为黑色和白色:

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); 
+1

有没有一种方法,我可以减少它为单色?我已经有一个黑色和白色的图像,这是从另一个程序canny过滤 – 0xSina

+0

@ 0xSina:是的,只是添加它:) – Vlad

+0

很酷,谢谢! – 0xSina