【问题标题】:Removing blobs from a binary image从二进制图像中删除 blob
【发布时间】:2015-08-15 03:41:48
【问题描述】:

我有一个包含少量斑点的二进制图像。

我想删除小于某个区域的 blob。

谁能给我一个建议?

我正在使用 Open-CV。我做了膨胀和腐蚀来得到那些斑点。所以我需要一些不同的东西来去除小于某个区域的铰孔。

【问题讨论】:

  • 1.运行 conncomp 标记算法。 2. 剔除尺寸小于阈值的区域。另一种变体是 N 次腐蚀和 N 次膨胀后(但 N 越高,blob 的恢复越差)
  • 你能提供一个示例图像吗?

标签: opencv image-processing


【解决方案1】:

你可以这样做:

// 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);
}

【讨论】:

  • 有人可以解释一下 contours_all 是如何出现在 if 条件下的吗?以及它包含的内容。我听不懂
【解决方案2】:
            //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;
                }
            }

试试这个吧……

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-01-29
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多