【发布时间】:2020-05-12 01:37:42
【问题描述】:
所以我有这个 python 脚本,可以检测并打印图像中的一系列 HSV 颜色,但我想向它添加另一个功能。
它希望它能够打印该颜色的百分比。
我的脚本:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('C:/Users/Vishu Rana/Documents/PY/test_cases/image.jpg')
grid_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20,8))
plt.imshow(grid_RGB) # Printing the original picture after converting to RGB
grid_HSV = cv2.cvtColor(grid_RGB, cv2.COLOR_RGB2HSV) # Converting to HSV
lower_green = np.array([25,52,72])
upper_green = np.array([102,255,255])
mask= cv2.inRange(grid_HSV, lower_green, upper_green)
res = cv2.bitwise_and(img, img, mask=mask) # Generating image with the green part
print("Green Part of Image")
plt.figure(figsize=(20,8))
plt.imshow(res)
我需要在此代码中添加什么以使其也打印绿色的百分比。
【问题讨论】:
-
蒙版中绿色像素的百分比?类似:
np.sum(mask) / np.size(mask). -
@Benjamin 将返回原始图像中绿色像素的百分比。
-
我认为它是一个二进制/布尔掩码。但你知道他们怎么说假设......
标签: python numpy opencv colors