- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理的是两张图片:
(这里的图片大小不一样,但是在我的程序中是一样的)
服用skimage.metrics.structural_similarity()
后在上面的两张图片中,我有以下阈值:
如你所见,它由 2 个形状组成,几乎是圆形但又不完全是(右下角多余的部分是圆形的阴影)
我想对这个阈值进行分水岭,以便获得两个圆圈,但我当前的代码给了我这个:
相反,我想要蓝色的东西:
# 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 图片开始:
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
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
)
注意:我使用了逆二进制,以便在两个等高线之间获得漂亮的中间线,否则我们将回到原点。
_, thresh = cv2.threshold(markers, 150, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
注意:我对半径使用了过滤器,因为获取的轮廓数量可能超过两个;您可能还需要使用此过滤器,以确保始终获得您正在寻找的圈子,而不是更大(或更小)的圈子。
circles = []
for cnt in contours:
(cx, cy), radius = cv2.minEnclosingCircle(cnt)
if radius < 100:
circles.append((int(cx), int(cy), int(radius)))
这个过程可能会被优化/减少,因为我即兴创作了一点并且没有考虑太多。但无论如何,我希望这能有所帮助:)
PS:使用“+”或“|”都没有关系在 opencv 中,它们的意思是一样的。虽然我建议您使用“+”,因为它更具可读性,但这完全取决于您。
关于python - 如何分水岭两个相连的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466958/
这是两个非常相似的查询。它们之间的区别在于,在第二个查询中,我添加了名为 ng2s3_map 的新表,并且我只需要其中的一列。问题是第一个查询始终根据需要工作,但不输出类似列,第二个查询仅当表 ng2
我是一名优秀的程序员,十分优秀!