从二进制映像中删除斑点 [英] Removing blobs from a binary image

查看:68
本文介绍了从二进制映像中删除斑点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制图像,其中包含少量斑点.

I have a binary image which contain few blobs.

我要删除小于特定区域的斑点.

I want to remove blobs which are less than a certain area.

有人可以建议我吗?

我正在使用Open-CV.我做了扩张和侵蚀以获得这些斑点.因此,我需要一些其他方法来去除小于特定区域的扩孔斑点.

I am using Open-CV. i did dilation and erosion to get those blobs. so i need something different to remove the reaming blobs which are less than a certain area.

推荐答案

您可以执行以下操作:

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

这篇关于从二进制映像中删除斑点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆