gpt4 book ai didi

python - 如何分水岭两个相连的圆圈

转载 作者:行者123 更新时间:2023-12-02 16:04:14 25 4
gpt4 key购买 nike

我正在处理的是两张图片:

enter image description here enter image description here

(这里的图片大小不一样,但是在我的程序中是一样的)

服用skimage.metrics.structural_similarity()后在上面的两张图片中,我有以下阈值:

enter image description here

如你所见,它由 2 个形状组成,几乎是圆形但又不完全是(右下角多余的部分是圆形的阴影)

我想对这个阈值进行分水岭,以便获得两个圆圈,但我当前的代码给了我这个: enter image description here

相反,我想要蓝色的东西:

enter image description here

# import the necessary packages
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
from scipy import ndimage
import numpy as np
import cv2
from skimage.metrics import structural_similarity

imageA = cv2.imread("frames/thing150.png") #the left image
imageB = cv2.imread("frames/thing180.png") #the right image
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

(score, diff) = structural_similarity(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")

thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# cv2.namedWindow("Thresh", cv2.WINDOW_NORMAL)
# cv2.imshow("Thresh", thresh)

# 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=100, 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)
print(f"[INFO] {len(np.unique(labels)) - 1} unique segments found")

# loop over the unique labels returned by the Watershed
# algorithm
for label in np.unique(labels):
# if the label is zero, we are examining the 'background'
# so simply ignore it
if label == 0:
continue
# otherwise, allocate memory for the label region and draw
# it on the mask
mask = np.zeros(grayB.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)
c = max(cnts, key=cv2.contourArea)
# draw a circle enclosing the object
((x, y), r) = cv2.minEnclosingCircle(c)
cv2.circle(imageB, (int(x), int(y)), int(r), (0, 255, 0), 2)
cv2.putText(imageB, "#{}".format(label), (int(x) - 10, int(y)),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
print(len(cnts))
print("----")
print(np.unique(labels))
# show the output imageB
cv2.namedWindow("Output", cv2.WINDOW_NORMAL)
cv2.imshow("Output", imageB)
cv2.waitKey(0)

我对分水岭不熟悉,所以我从this复制了代码网站。我尝试改变 min_distance 的参数在localMax = peak_local_max(D, indices=False, min_distance=100, labels=thresh) ,但这并没有解决我的问题。

我也尝试过使用 OpenCV 的分水岭算法,但由于某种原因它没有奏效。如果这比 skimage 的好,那么我会试试看。

如有任何建议,我们将不胜感激。

P.S. 是吗 thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

或者 thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

我看到各种来源都在使用它们,它们都对我有用,有什么区别吗?

最佳答案

此解决方案将从您上传的 thresh 图片开始:

  1. 填充轮廓:由于 thresh 图像没有真正连接(白色空间之间有很多黑色空间),我们必须找到一种方法来填充这些“孔”。我们可以使用一些开放内核,但大小在图像之间往往会有所不同。我认为最好找到外部轮廓并通过填充它们来创建某种掩码。
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# get largest contour (snowman figurine)
idx = np.argmax([cv2.contourArea(cnt) for cnt in contours])
cnt = contours[idx]
# create mask
thresh = cv2.fillPoly(thresh, np.int32([cnt]), 0, (255, 255, 255)) #Bug with fillPoly, needs explict cast to 32bit

Binarized mask

  1. 分水岭算法:使用watershed algorithm实现以分离两个连接的对象。我做了一些小的修改,因为教程没有像人们希望的那样更新。
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
background = cv2.dilate(opening, kernel, iterations=3)

# extract foreground
dst = cv2.distanceTransform(opening, cv2.DIST_L2, 5, dstType=cv2.CV_32F)
_, foreground = cv2.threshold(dst, 0.7 * dst.max(), 255, cv2.THRESH_BINARY)
foreground = np.uint8(foreground)
unknown = cv2.subtract(background, foreground)

# get markers
_, markers = cv2.connectedComponents(foreground)
markers += 1
markers[unknown == 255] = 0

# Since the original image is in fact two different images, use
# instead the thresh image, which is the combination of both.
thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
markers = cv2.watershed(thresh, markers)
# normalize markers so they can be visualized
markers = cv2.normalize(
markers, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U
)

Separated contours

  1. 阈值图像(再次)+ 轮廓:现在我们终于分离了轮廓,现在我们可以找到封闭的圆圈。为了做到这一点,我建议对标记设置阈值,这样我们就不会得到更多我们想要的圈子。

注意:我使用了逆二进制,以便在两个等高线之间获得漂亮的中间线,否则我们将回到原点。

_, thresh = cv2.threshold(markers, 150, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

thresholdd image

  1. 近似圆:现在我们可以找到最小外接圆。

注意:我对半径使用了过滤器,因为获取的轮廓数量可能超过两个;您可能还需要使用此过滤器,以确保始终获得您正在寻找的圈子,而不是更大(或更小)的圈子。

circles = []
for cnt in contours:
(cx, cy), radius = cv2.minEnclosingCircle(cnt)
if radius < 100:
circles.append((int(cx), int(cy), int(radius)))

Minimum enclosing circles

这个过程可能会被优化/减少,因为我即兴创作了一点并且没有考虑太多。但无论如何,我希望这能有所帮助:)

PS:使用“+”或“|”都没有关系在 opencv 中,它们的意思是一样的。虽然我建议您使用“+”,因为它更具可读性,但这完全取决于您。

关于python - 如何分水岭两个相连的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com