我是OpenCV的新手,正在尝试获取图片中叶子的平均“贪婪度”。我使用的是HSV格式,无法确定在应用阈值后如何平均掉整个图像中分离出的像素。
有什么想法我应该怎么做?
import cv2
import numpy as np
def nothing(x):
pass
while True:
img = cv2.imread('leaves.jpg', 1)
sub_img1 = img[300:417, 500:600]
cv2.imwrite('sub1.jpg', sub_img1)
#Transforming the img to HSV format
hsv = cv2.cvtColor(sub_img1, cv2.COLOR_BGR2HSV)
l_g = np.array([28, 52, 61]) #determining the lower limit for color thresholding (hue, saturation, value)
u_g = np.array([59, 255, 255]) #determining the upper limit for color thresholding
mask = cv2.inRange(hsv, l_g, u_g)
res = cv2.bitwise_and(sub_img1, sub_img1, mask=mask)
cv2.imshow("original", img)
cv2.imshow("img", sub_img1)
cv2.imshow("mask", mask)
cv2.imshow("res", res)
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
cv2.destroyAllWindows()
答案 0 :(得分:0)
average_hsv = cv2.mean(hsv, mask)[:3]