【问题标题】:Finding the largest blob in a black/white image在黑白图像中查找最大的斑点
【发布时间】:2019-02-19 17:43:18
【问题描述】:

我有这张图: 我想从此图像创建一个蒙版,放置在原始图像之上。我想要获得的面具是顶部的黑色部分。

我尝试使用 OpenCV 中的 simpleBlobDetector 来尝试将白色部分检测为一个大斑点。我没有得到我希望的结果并且不确定该怎么做。

已经使用了R,但我的问题并不是具体关于如何在R中实现这一点。我得到的结果在代码下方。

library(Rvision)
x <- simpleBlobDetector(im, min_threshold = 0, max_threshold = 255)
plot(x)

我不明白为什么这三个黑框被选为 blob,而没有选择的黑框还有很多。

编辑:当我添加 blob_color = 255 以便搜索白色斑点时,没有检测到任何内容。

【问题讨论】:

  • 我不知道 R,但有几个想法是... 1) 在运行 blob 检测器之前尝试反转图像,2) 在运行 blob 检测器之前尝试对图像进行阈值处理,3 ) 在运行斑点检测器之前,尝试在图像周围添加几个像素宽的白色边框。
  • 尝试了 1 和 3,但结果相同。您建议使用什么阈值?感谢您的帮助!
  • 我的意思是在斑点检测器之前你的图像的一个简单的 50% 阈值。 128 以下的所有像素变为纯黑色,所有其他像素变为纯白色。
  • 我的错。他们已经是了。
  • 很抱歉没有成功,我不知道任何关于 R 的建议。希望其他人会帮助你。祝你好运!

标签: r opencv


【解决方案1】:

你可以用 OpenCV 做这样的事情:

// read input image
Mat inputImg = imread("test1.tif", IMREAD_GRAYSCALE);

// create binary image
Mat binImg;
threshold(inputImg, binImg, 254, 1, THRESH_BINARY_INV);

// compute connected components
Mat labelImg;
connectedComponents(binImg, labelImg, 8, CV_16U);

// compute histogram
Mat histogram;
int histSize = 256;
float range[] = { 0, 256 } ;
const float* histRange = { range };
calcHist(&labelImg, 1, 0, Mat(), histogram, 1, &histSize, &histRange, true, false);

// retrieve maximal population
float maxVal = 0;
int maxIdx;
for (int i=1; i<histSize; ++i) {
    if (histogram.at<float>(i) > maxVal) {
        maxVal = histogram.at<float>(i);
        maxIdx = i;
    }
}

// create output mask with bigest population
Mat resImg;
threshold(labelImg, labelImg, maxIdx, 0, THRESH_TOZERO_INV);
threshold(labelImg, resImg, maxIdx-1, 1, THRESH_BINARY);

// write result
imwrite("res.tif", resImg);

你应该得到这样的东西:

【讨论】:

    【解决方案2】:

    我认为您可以将输入转换为二进制,然后提取连接的组件,计算相关的直方图并简单地保留(通过阈值)具有最高人口的直方图类 问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      相关资源
      最近更新 更多