findContours错误'仅支持8uC1图像'

0xS*_*ina 16 opencv image-processing

试图在二进制图像上运行findContours"

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);
Run Code Online (Sandbox Code Playgroud)

thorws错误:

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

有任何想法吗?谢谢

Vla*_*lad 22

文档:

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

rtype - 所需的输出矩阵类型,或者更确切地说,因为通道数与输入相同的深度; 如果rtype为负,则输出矩阵将与输入具有相同的类型.

你看到通道的数量没有改变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);
Run Code Online (Sandbox Code Playgroud)