gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:21:11 25 4
gpt4 key购买 nike

我找到了将RGB转换为HSV的方法,但仍然无法找到颜色的上限值和下限值。我该如何计算?

我必须从图像中取出泡菜



直到现在,这是我的代码

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

请帮我

最佳答案

您可以使用以下程序通过点击像素(即您想要的像素)来找到像素的上,下色调值。

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

关于python - 如何在HSV opencv python中指定颜色的上限值和下限值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com