从二进制图像中移除斑点

9

我有一张二进制图像,其中包含几个斑点。

我想要删除面积小于某个特定值的斑点。

有没有人能给我建议?

我正在使用Open-CV。我进行了膨胀和腐蚀操作以获取这些斑点。因此,我需要一些不同的方法来去除那些面积小于某个特定值的斑点。


1
  1. 运行连通组件标记算法。
  2. 丢弃面积小于阈值的区域。另一种变体是N次腐蚀,然后-N次膨胀(但N越高,斑点的恢复效果越差)。
- Eddy_Em
你能提供一个示例图像吗? - Omar Wagih
2个回答

3
你可以像这样做:

你可以像这样做:

// your input binary image
// assuming that blob pixels have positive values, zero otherwise
Mat binary_image; 

// threashold specifying minimum area of a blob
double threshold = 100;

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<int> small_blobs;
double contour_area;
Mat temp_image;

// find all contours in the binary image
binary_image.copyTo(temp_image);
findContours(temp_image, contours, hierarchy, CV_RETR_CCOMP,
                                                  CV_CHAIN_APPROX_SIMPLE);

// Find indices of contours whose area is less than `threshold` 
if ( !contours_all.empty()) {
    for (size_t i=0; i<contours.size(); ++i) {
        contour_area = contourArea(contours_all[i]) ;
        if ( contour_area < threshold)
            small_blobs.push_back(i);
    }
}

// fill-in all small contours with zeros
for (size_t i=0; i < small_blobs.size(); ++i) {
    drawContours(binary_image, contours, small_blobs[i], cv::Scalar(0), 
                                                 CV_FILLED, 8);
}

1
有人能解释一下,轮廓(contours_all)是如何出现在if条件语句中的吗?它包含了什么?我无法理解。 - Teh Sunn Liu

0
            //src_gray is your image that we threshold
            threshold(src_gray, threshold_output, NULL, 255, THRESH_OTSU);
            /// Find contours
            findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
            /// Approximate contours
            vector<Rect> boundRect(contours.size());
            for (unsigned int i = 0; i < contours.size(); i++)
            {   //identify bounding box
                boundRect[i] = boundingRect(contours[i]);
            }
            for (unsigned int i = 0; i < contours.size(); i++)
            {

                if ((boundRect[i].area() < //enter your area here))
                {
                    src_gray(boundRect[i])=//set it to whatever value you want;
                }
            }

好的,让我们试试看...


添加更多细节 - Priyank Pathak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接