如何分水岭两个相连的圆圈
How to watershed two connected circles
我正在处理的是两张图片:
(这里的图片大小不一样,但是在我的程序中是一样的)
对以上两张图进行 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网站上复制了代码。我尝试在 localMax = peak_local_max(D, indices=False, min_distance=100, labels=thresh)
中更改 min_distance
的参数,但它没有解决我的问题。
我也尝试过使用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 图片开始:
- 填充轮廓:由于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
- 分水岭算法:使用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
)
- 阈值图像(再次)+轮廓: 现在我们终于分离了轮廓,我们现在可以找到封闭的圆。为了做到这一点,我建议对标记设置阈值,这样我们就不会得到更多我们想要的圈子。
注意:我使用了逆二进制,以便在两个等高线之间获得漂亮的中间线,否则我们将回到原点。
_, 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)))
这个过程可能是optimized/reduced,因为我临时凑合了一点,没有花太多心思。但无论如何,我希望这能有所帮助:)
PS: 使用“+”或“|”都没有关系在 opencv 中,它们的意思是一样的。虽然我建议您使用“+”,因为它更具可读性,但这完全取决于您。
我正在处理的是两张图片:
(这里的图片大小不一样,但是在我的程序中是一样的)
对以上两张图进行 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网站上复制了代码。我尝试在 localMax = peak_local_max(D, indices=False, min_distance=100, labels=thresh)
中更改 min_distance
的参数,但它没有解决我的问题。
我也尝试过使用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 图片开始:
- 填充轮廓:由于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
- 分水岭算法:使用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
)
- 阈值图像(再次)+轮廓: 现在我们终于分离了轮廓,我们现在可以找到封闭的圆。为了做到这一点,我建议对标记设置阈值,这样我们就不会得到更多我们想要的圈子。
注意:我使用了逆二进制,以便在两个等高线之间获得漂亮的中间线,否则我们将回到原点。
_, 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)))
这个过程可能是optimized/reduced,因为我临时凑合了一点,没有花太多心思。但无论如何,我希望这能有所帮助:)
PS: 使用“+”或“|”都没有关系在 opencv 中,它们的意思是一样的。虽然我建议您使用“+”,因为它更具可读性,但这完全取决于您。