使用Python计算灰度图像中口罩的区域面积(以像素为单位)

5
以下硬币图像是一张灰度图,带有不同颜色的不同掩模。有没有办法用Python计算每个硬币掩模(以像素为单位)的面积?

enter image description here

硬币面具的标签
{"classes": [{"title": "coin1", "shape": "polygon", "color": "#BE5C3C", "geometry_config": {}}, {"title": "coin2", "shape": "polygon", "color": "#961D82", "geometry_config": {}}, {"title": "coin3", "shape": "polygon", "color": "#C1BB5C", "geometry_config": {}}, {"title": "coin4", "shape": "polygon", "color": "#D0021B", "geometry_config": {}}, {"title": "coin5", "shape": "polygon", "color": "#417505", "geometry_config": {}}], "tags": []}

硬币面具的注释
{"tags": [], "description": "", "objects": [{"description": "", "bitmap": null, "tags": [], "classTitle": "coin1", "points": {"exterior": [[59.0, 85.0], [65.0, 70.0], [76.0, 63.0], [89.0, 61.0], [105.0, 63.0], [116.0, 78.0], [118.0, 98.0], [103.0, 117.0], [80.0, 118.0], [61.0, 103.0]], "interior": []}}, {"description": "", "bitmap": null, "tags": [], "classTitle": "coin2", "points": {"exterior": [[103.0, 43.0], [104.0, 28.0], [118.0, 17.0], [136.0, 16.0], [151.0, 22.0], [161.0, 34.0], [159.0, 53.0], [150.0, 68.0], [127.0, 73.0], [109.0, 62.0], [105.0, 54.0]], "interior": []}}, {"description": "", "bitmap": null, "tags": [], "classTitle": "coin3", "points": {"exterior": [[112.0, 143.0], [121.0, 129.0], [148.0, 124.0], [165.0, 141.0], [166.0, 160.0], [159.0, 175.0], [138.0, 184.0], [119.0, 174.0], [112.0, 161.0]], "interior": []}}, {"description": "", "bitmap": null, "tags": [], "classTitle": "coin4", "points": {"exterior": [[44.0, 137.0], [69.0, 134.0], [81.0, 152.0], [80.0, 171.0], [64.0, 181.0], [46.0, 178.0], [37.0, 168.0], [33.0, 151.0]], "interior": []}}, {"description": "", "bitmap": null, "tags": [], "classTitle": "coin5", "points": {"exterior": [[183.0, 117.0], [189.0, 100.0], [201.0, 93.0], [220.0, 98.0], [226.0, 111.0], [223.0, 126.0], [211.0, 136.0], [194.0, 135.0]], "interior": []}}], "size": {"height": 206, "width": 244}}
2个回答

10
这里介绍一种使用OpenCV的方法。我们使用Otsu阈值来获取二进制图像,这样就可以将前景对象留在白色背景中,而将背景变成黑色。接下来我们使用cv2.countNonZero() 函数来返回掩模中白色像素的数量。

要找到白色像素的数量

pixels = cv2.countNonZero(thresh) # OR
# pixels = len(np.column_stack(np.where(thresh > 0)))

像素数 198580

我们还可以计算像素数与总图像面积的百分比比率。

image_area = image.shape[0] * image.shape[1]
area_ratio = (pixels / image_area) * 100

区域比例为24.43351838727459。
import cv2
import numpy as np

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
pixels = cv2.countNonZero(thresh)
# pixels = len(np.column_stack(np.where(thresh > 0)))

image_area = image.shape[0] * image.shape[1]
area_ratio = (pixels / image_area) * 100

print('pixels', pixels)
print('area ratio', area_ratio)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

如果您想获取每个硬币像素区域的面积,那么可以遍历每个轮廓(contour)。总面积应该是相同的。

import cv2
import numpy as np

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
total = 0

for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    mask = np.zeros(image.shape, dtype=np.uint8)
    cv2.fillPoly(mask, [c], [255,255,255])
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    pixels = cv2.countNonZero(mask)
    total += pixels
    cv2.putText(image, '{}'.format(pixels), (x,y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)

print(total)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey(0)

谢谢。但它是否提供每个硬币的像素信息? - user1703276
像素信息是什么意思? - nathancy
我的意思是coin1,coin2 ..... coin5的区域。我猜你需要一个有掩码标签的.json文件?如果是的话,我刚刚更新了问题并提供了标签信息。 - user1703276
遮罩标签是不必要的,您可以迭代每个轮廓,然后找到每个单独的硬币区域。 - nathancy
是否可以将这些像素值保存在与它们的标签对应的文件中?就像 coin1 46728,coin2 47968 .... 这样。 - user1703276
2
你需要将硬币标签与其轮廓匹配,然后将此信息写入文本文件中。我不确定如何进行匹配。最好另开一个问题来讨论。 - nathancy

0
每个标记区域的真实像素数量是每个标记区域面积的估计值。

是的,但是当我有许多带有许多掩码的图像时,我该如何获取它们? - user1703276

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接