从HSV图像中提取叶绿素筛选的平均颜色

2024-06-01 07:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: theimportimgnpresmaskarraycv2