我已经掩盖了疫区。我想计算出叶上受感染面积的百分比。这是我的密码。如何计算感染面积的百分比?
import cv2
import numpy as np
img = cv2.imread('AFTER_5736.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# find the green color
mask_green = cv2.inRange(hsv, (36, 0, 0), (86,255,255))
# find the brown color
mask_brown = cv2.inRange(hsv, (8, 60, 20), (30, 255, 255))
# find the yellow color in the leaf
mask_yellow = cv2.inRange(hsv, (14, 39, 64), (40, 255, 255))
# find any of the three colors(green or brown or yellow) in the image
#mask = cv2.bitwise_or(mask_green, mask_brown)
#mask = cv2.bitwise_or(mask, mask_yellow)
mask = cv2.bitwise_not(mask_green)
# Bitwise-AND mask and original image
res = cv2.bitwise_not(img, img, mask= mask)
cv2.imshow("final image", res)
cv2.waitKey(0)
cv2.destroyAllWindows()发布于 2021-05-08 14:40:31
试着取图像的和,看看有多少像素被遮住了该颜色。
>>> sum(sum(mask_brown))
16203
>>> sum(sum(mask_green))
22906
>>> sum(sum(mask_yellow))
9292
>>> brown = sum(sum(mask_brown))
>>> green = sum(sum(mask_green))
>>> yellow = sum(sum(mask_yellow))
>>> total = brown + green + yellow
>>> percentHealthy = green / total
>>> percentHealthy
0.47325468482056154
>>> percentDiseased = (brown + yellow) / total
>>> percentDiseased
0.5267453151794385https://stackoverflow.com/questions/67448621
复制相似问题