首页 > 解决方案 > 如何从已在 OpenCV 中标记的图像中获取区域属性?

问题描述

我正在使用 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()处理已标记图像的功能?

标签: pythonopencvimage-segmentationconnected-componentslabeling

解决方案


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

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

对于每个标签,我们可以使用numpy.where.

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

由于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

图片

  • 输入
    输入图像
  • 彩色标签
    彩色标签
  • 带标签的边界框
    带标签的边界框

完整的脚本

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)

推荐阅读