2015-04-01 426 views
1

我有這個圖片:opencv的findcontours CV_RETR_EXTERNAL不工作

編輯 很抱歉,但我不得不刪除的圖像!

我需要提取非黑畫面的輪廓,所以我用findcontour與CV_RETR_EXTERNAL參數,但我得到這樣的:

下面的代碼:

static Mat canny_output, grey,draw; 
     vector<vector<Point>> contours; 
     cvtColor(final_img, grey, CV_BGR2GRAY); 
     Canny(grey, canny_output, 100, 200); 
     findContours(canny_output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 

     draw = Mat::zeros(canny_output.size(), CV_8UC3); 


     for (size_t i = 0; i < contours.size(); i++) 
     { 
      drawContours(draw, contours, i, Scalar(255, 0, 0)); 

     } 

我該怎麼解決?

回答

1

只需添加一個二值化最小閾值,並移除坎尼:

cvtColor(final_img, grey, CV_BGR2GRAY); 
//Threshold=1: very low value, anyway the rest of the image is pure black 
threshold(grey, binary, 1, 255, CV_THRESH_BINARY); 
findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 
+1

感謝的人它的工作原理:d – bjorn 2015-04-02 07:54:17