python - 从阈值掩模生成圆形粒子的分割掩模?

标签 python python-3.x opencv image-processing computer-vision

我正在尝试找到所附图像中的所有圆形颗粒。这是我拥有的唯一图像(及其反面)enter image description here .

我已阅读this post但我无法使用 hsv 值进行阈值处理。我尝试过使用霍夫变换。

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, dp=0.01, minDist=0.1, param1=10, param2=5, minRadius=3,maxRadius=6)

并使用以下代码进行绘图

names =[circles]
for nums in names:
  color_img = cv2.imread(path)
  blue = (211,211,211)
  for x, y, r in nums[0]:
    cv2.circle(color_img, (x,y), r, blue, 1)
    
  plt.figure(figsize=(15,15))
  plt.title("Hough")
  plt.imshow(color_img, cmap='gray')

以下代码用于绘制掩模:

for masks in names:
  black = np.zeros(img_gray.shape)
  for x, y, r in masks[0]:
    cv2.circle(black, (x,y), int(r), 255, -1)  # -1 to draw filled circles
plt.imshow(black, gray)
  

但是我只能得到以下面具,但效果相当差。 result

这是一张图像,展示了什么被认为是粒子,什么不是粒子。 labeled image

最佳答案

一种简单的方法涉及稍微腐 eclipse 图像,分离触摸的圆形对象,然后进行连通分量分析并丢弃大于某个选定阈值的所有对象,最后将图像向后扩展,使圆形对象大约为原始大小再次。我们可以对标记图像进行这种扩张,以便保留分离的对象。

我正在使用DIPlib因为我最熟悉它(我是一名作者)。

import diplib as dip

a = dip.ImageRead('6O0Oe.png')
a = a(0) > 127        # the PNG is a color image, but OP's image is binary,
                      # so we binarize here to simulate OP's condition.

separation = 7        # tweak these two parameters as necessary
size_threshold = 500

b = dip.Erosion(a, dip.SE(separation))
b = dip.Label(b, maxSize=size_threshold)
b = dip.Dilation(b, dip.SE(separation))

output of code above

请注意,我们在这里使用的图像似乎是放大的屏幕截图,而不是 OP 正在处理的原始图像。如果是这样,则必须使参数更小,以识别较小图像中的较小物体。

关于python - 从阈值掩模生成圆形粒子的分割掩模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62946356/

相关文章:

javascript - Web Scrape JS 渲染网站

java - 在java中将图像转换为圆柱形

python - 训练自定义瑞典 spacy 模型

Python Seaborn : Plot multiple distplot in Facetgrid

python - 计算 postgresql 中返回的行数

python - 使用递归在 Python 中对多维数组中的数字求和

c++ - 在 OpenCV 中测试 parallel_for_ 性能

c++ - 如何在关键点写坐标?

python - Keras "trainable"范围

python 字典更新差异