首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >球面反射物体的分水岭分割

球面反射物体的分水岭分割
EN

Stack Overflow用户
提问于 2020-12-09 02:07:43
回答 1查看 106关注 0票数 1

我正在尝试用分水岭做一些图像分割,以检测图像中的所有球。我关注了一个pyimage tuto。但是我得到了非常糟糕的结果。我猜想问题出在反射上。尽管如此,图像还是相当干净的,实例看起来也是可以分离的。

我在这里使用的方法正确吗?我错过了什么吗?我测试了cellpose,得到了近乎完美的结果。当然,这不是同一种方法,我希望用“经典”的计算机视觉技术来获得一些东西。

以下是我的代码,原始图像和当前结果。我已经尝试更改参数,但我不确定我在这里做什么。我也看了看inRange,但我担心球永远不会是同一颜色的。

原图:https://i.stack.imgur.com/7595R.jpg

代码语言:javascript
复制
import numpy as np
from scipy import ndimage
import cv2
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
import imutils
from matplotlib import pyplot as plt


img = cv2.imread('balls.jpg')
gray = - cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

plt.imshow(gray)

# Things I tried...
# gray = cv2.dilate(gray,kernel,iterations = 1)
# hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# h, s, v = cv2.split(hsv)

thresh = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

# compute the exact Euclidean distance from every binary
# pixel to the nearest zero pixel, then find peaks in this
# distance map
D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D, indices=False, min_distance=30, labels=thresh)
# perform a connected component analysis on the local peaks,
# using 8-connectivity, then appy the Watershed algorithm
markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=thresh)

# draw on mask
for label in np.unique(labels):
    # if the label is zero -> 'background'
    if label == 0:
        continue

    mask = np.zeros(gray.shape, dtype="uint8")
    mask[labels == label] = 255
    # detect contours in the mask and grab the largest one
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    c = max(cnts, key=cv2.contourArea)

    # draw a circle enclosing the object
    ((x, y), r) = cv2.minEnclosingCircle(c)
    cv2.circle(img, (int(x), int(y)), int(r), (0, 255, 0), 2)
    cv2.putText(img, "#{}".format(label), (int(x) - 10, int(y)), 
                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

plt.imshow(img)

标签:https://i.stack.imgur.com/M6hZb.png

EN

回答 1

Stack Overflow用户

发布于 2020-12-09 06:28:57

使用opencv/samples/mouse_and_match.py的matchTemplate“解决方案”

使用你喜欢的任何东西来寻找峰值。

是的,使用这种方法,你必须手动选择一个模板。

为了解决这个问题,可能会有一些利用自相似性(自相关)的方法。给读者做练习。

你不能选择整个球,因为它们的大小不同,所以这已经是模板匹配的一个很大的缺点,但圆圈周围的矩形包含大量的非对象像素,这会降低相关性分数,无论该部分在哪里变化。

选择反射效果(从一个中球上),因为反射显示的环境具有良好的强对比度。

注意到靠近顶部略向右的一个小球了吗?这并不是很好,原因有很多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65204430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档