如何从 OpenCV 中已标记的图像中获取区域属性?

2023-12-07

我使用 OpenCV 中的分水岭算法来标记图像(类似于本教程:https://docs.opencv.org/3.4/d3/db4/tutorial_py_watershed.html)这样最后我获得了一个标签数组,其中每个区域都有一个与其标签相对应的整数值。现在,我想获取每个区域的边界框和面积的坐标。

我知道这很容易完成skimage.measure.regionprops()但出于执行速度的考虑,我希望在不导入skimage的情况下实现这一点,最好直接使用OpenCV。

我尝试过使用cv2.connectedComponentsWithStats()但它似乎只有在图像是二进制的情况下才起作用,而不是在标签已经定义的情况下才起作用。

我尝试对标记图像进行二值化,然后用connectedComponentsWithStats()如下(请注意,本例中背景的标签为 1,我想将其删除):

segmented = cv2.watershed(image.astype('uint8'), markers)

segmented_bin = segmented.copy()
segmented_bin[segmented < 2] = 0
segmented_bin[segmented > 1] = 255
num_labels, label_image, stats, centroids = cv2.connectedComponentsWithStats(segmented_bin.astype('uint8'), 4, cv2.CV_32S)

但这种方法合并了未被背景分隔的区域,这不是预期的效果。

本质上我想知道是否有类似的功能connectedComponentsWithStats()处理已经标记的图像?


由于(如果我没记错的话)每个标签代表一个连续区域,因此我们可以迭代所有非背景标签。

for i in range(2, marker_count + 1):

对于每个标签,我们可以使用以下命令创建相应的二进制掩码(具有该标签的像素变为 255,其他所有像素变为 0)numpy.where.

    mask = np.where(segmented==i, np.uint8(255), np.uint8(0))

Since cv2.boundingRect也可以处理单通道图像,我们可以用它直接从掩模确定边界框。

    x,y,w,h = cv2.boundingRect(mask)

标签的面积只是具有给定标签的像素的计数(即掩模中的所有非零像素)。我们可以简单地使用cv2.countNonZero为了那个原因。由于我们已经知道边界框,因此我们可以通过仅处理相应的 ROI 来节省一些工作。

    area = cv2.countNonZero(mask[y:y+h,x:x+w])

我们就完成了。

    print "Label %d at (%d, %d) size (%d x %d) area %d pixels" % (i,x,y,w,h,area)

控制台输出

Label 2 at (41, 14) size (47 x 49) area 1747 pixels
Label 3 at (111, 30) size (48 x 47) area 1719 pixels
Label 4 at (71, 51) size (56 x 48) area 1716 pixels
Label 5 at (152, 61) size (48 x 47) area 1676 pixels
Label 6 at (25, 75) size (47 x 48) area 1719 pixels
Label 7 at (109, 76) size (49 x 49) area 1748 pixels
Label 8 at (192, 82) size (49 x 48) area 1774 pixels
Label 9 at (64, 97) size (48 x 49) area 1695 pixels
Label 10 at (1, 114) size (47 x 48) area 1720 pixels
Label 11 at (139, 114) size (52 x 48) area 1727 pixels
Label 12 at (97, 132) size (48 x 48) area 1745 pixels
Label 13 at (181, 133) size (48 x 47) area 1667 pixels
Label 14 at (41, 140) size (47 x 48) area 1733 pixels
Label 15 at (129, 167) size (45 x 47) area 1666 pixels
Label 16 at (5, 169) size (50 x 48) area 1713 pixels
Label 17 at (72, 176) size (46 x 48) area 1745 pixels
Label 18 at (171, 177) size (50 x 49) area 1772 pixels
Label 19 at (35, 205) size (46 x 47) area 1702 pixels
Label 20 at (106, 207) size (55 x 49) area 1909 pixels
Label 21 at (155, 219) size (43 x 47) area 1537 pixels
Label 22 at (65, 237) size (51 x 48) area 1713 pixels
Label 23 at (25, 251) size (50 x 49) area 1818 pixels
Label 24 at (108, 264) size (48 x 47) area 1730 pixels
Label 25 at (155, 264) size (46 x 47) area 1711 pixels

Images

  • Input
    Input image
  • Colored labels
    Colored labels
  • Labeled bounding boxes
    Labeled bounding boxes

完整的脚本

import numpy as np
import cv2

# START of original watershed example
# from https://docs.opencv.org/3.4/d3/db4/tutorial_py_watershed.html

img = cv2.imread('water_coins.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
marker_count, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0

segmented = cv2.watershed(img,markers)

# END of original watershed example

output = np.zeros_like(img)
output2 = img.copy()

# Iterate over all non-background labels
for i in range(2, marker_count + 1):
    mask = np.where(segmented==i, np.uint8(255), np.uint8(0))
    x,y,w,h = cv2.boundingRect(mask)
    area = cv2.countNonZero(mask[y:y+h,x:x+w])
    print "Label %d at (%d, %d) size (%d x %d) area %d pixels" % (i,x,y,w,h,area)

    # Visualize
    color = np.uint8(np.random.random_integers(0, 255, 3)).tolist()
    output[mask!=0] = color
    cv2.rectangle(output2, (x,y), (x+w,y+h), color, 1)
    cv2.putText(output2,'%d'%i,(x+w/4, y+h/2), cv2.FONT_HERSHEY_SIMPLEX, 0.4, color, 1, cv2.LINE_AA)

cv2.imwrite('wshseg_colors.png', output)
cv2.imwrite('wshseg_boxes.png', output2)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 OpenCV 中已标记的图像中获取区域属性? 的相关文章

随机推荐