亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用python計算灰度圖像中蒙版的面積(以像素為單位)

用python計算灰度圖像中蒙版的面積(以像素為單位)

HUH函數 2022-06-02 18:12:13
下面的硬幣圖是帶有不同蒙版的灰度圖像(以不同的顏色顯示)。有沒有辦法用python計算灰度圖像中每個硬幣的這些掩碼的面積(以像素為單位)。硬幣面具的標簽{"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}}
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

這是一種使用 OpenCV 的方法。我們使用 Otsu 的閾值來獲得二值圖像,這使我們得到所需的前景對象為白色,而背景為黑色。從這里我們使用cv2.countNonZero()它返回蒙版上的白色像素數

http://img1.sycdn.imooc.com//62988d220001f24e03700293.jpg

查找白色像素的數量


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)

如果您想獲得單個硬幣像素區域,那么您可以遍歷每個輪廓??偯娣e應該相同

http://img1.sycdn.imooc.com//62988d2c000155d203630308.jpg

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)


查看完整回答
反對 回復 2022-06-02
  • 1 回答
  • 0 關注
  • 665 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號