我正在尝试找到所附图像中的所有圆形颗粒。这是我拥有的唯一图像(及其反面) .
我已阅读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)
最佳答案
一种简单的方法涉及稍微腐 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))
请注意,我们在这里使用的图像似乎是放大的屏幕截图,而不是 OP 正在处理的原始图像。如果是这样,则必须使参数更小,以识别较小图像中的较小物体。
关于python - 从阈值掩模生成圆形粒子的分割掩模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62946356/