简体   繁体   English

如何在HSV opencv python中指定颜色的上限值和下限值

[英]how to i specify the upper and the lower value of a color in HSV opencv python

I found way to convert RGB to HSV, but still I am unable to find the upper and lower value of color. 我找到了将RGB转换为HSV的方法,但仍然无法找到颜色的上限值和下限值。 How to do i calculate that? 我该如何计算?

I have to take out the pickachu from the image 我必须从图像中取出泡菜

在此处输入图片说明

and this my code till now 直到现在,这是我的代码

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)


    lower_red = np.array([30,50,50])
    upper_red = np.array([255,255,180])  #it is trial and error

    mask = cv2.inRange(frame, lower_red, upper_red)
    res = cv2.bitwise_and(frame, frame, mask= mask)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
       break
cv2.destroyAllWindows()
cap.release()

please help me 请帮我

you can use the following program to find the upper and lower hue values for the pixel by clicking on it(ie the pixel you want). 您可以使用以下程序通过单击像素(即所需的像素)来找到像素的上,下色调值。

import cv2
import numpy as np

image_hsv = None   # global ;(
pixel = (20,60,80) # some stupid default

# mouse callback function
def pick_color(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        pixel = image_hsv[y,x]

        #you might want to adjust the ranges(+-10, etc):
        upper =  np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 40])
        lower =  np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 40])
        print(pixel, lower, upper)

        image_mask = cv2.inRange(image_hsv,lower,upper)
        cv2.imshow("mask",image_mask)

def main():
    import sys
    global image_hsv, pixel # so we can use it in mouse callback

    image_src = cv2.imread(sys.argv[1])  # pick.py my.png
    if image_src is None:
        print ("the image read is None............")
        return
    cv2.imshow("bgr",image_src)

    ## NEW ##
    cv2.namedWindow('hsv')
    cv2.setMouseCallback('hsv', pick_color)

    # now click into the hsv img , and look at values:
    image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)
    cv2.imshow("hsv",image_hsv)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__=='__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 cv::inRange (OpenCV) 为颜色检测选择正确的 HSV 上下边界 - Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV) 如何根据较低和较高的粉红色范围找到 roi_corners,以便它可以在 python 和 opencv 中模糊 - How to find roi_corners based on lower and upper pink color range so it can be blurred in python with opencv 如何在python opencv中获得hsv颜色的低值和高值 - How to get low and high values of hsv color in python opencv (如何)在 opencv 和 python 的帮助下,我可以将 HSV 的 v 值标准化为 0 和 1 之间吗? - (How) can I normalize the HSV's v value to be between 0 and 1 with the help of opencv and python? 如何将颜色转换为列表? 从下到上值作为 numpy 数组 - How to convert color into a list? From lower to upper value as a numpy array 在Python中为变量指定范围(上限和下限) - Specify a range (upper and lower bound) for a variable in Python python + opencv - 如何绘制hsv范围? - python+opencv - How to plot hsv range? python opencv 如何更改 HSV 通道中的色调 - python opencv how to change hue in HSV channels 如何在Python中组织大小写单词? - How do I organize lower and upper case words in Python? 如何找到 colors 颜色的“上”和“下”范围? - How to find a colors “Upper” and “Lower” range of a color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM