2012-11-02 133 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