How to crop away convexity defects?

后端 未结 3 1108
半阙折子戏
半阙折子戏 2020-11-28 10:21

I\'m trying to detect and fine-locate some objects in images from contours. The contours that I get often include some noise (maybe form the background, I don\'t know). The

3条回答
  •  北海茫月
    2020-11-28 11:00

    As a starting point and assuming the defects are never too big relative to the object you are trying to recognize, you can try a simple erode+dilate strategy before using cv::matchShapes as shown below.

     int max = 40; // depending on expected object and defect size
     cv::Mat img = cv::imread("example.png");
     cv::Mat eroded, dilated;
     cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(max*2,max*2), cv::Point(max,max));
     cv::erode(img, eroded, element);
     cv::dilate(eroded, dilated, element);
     cv::imshow("original", img);
     cv::imshow("eroded", eroded);
     cv::imshow("dilated", dilated);
    

提交回复
热议问题