从HSV图像中获取平均颜色以进行叶绿素筛选

时间:2019-06-17 14:05:11

标签: python opencv

我是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()

1 个答案:

答案 0 :(得分:0)

average_hsv = cv2.mean(hsv,  mask)[:3]