[英]How to convert color into a list? From lower to upper value as a numpy array
import cv2
import numpy as np
img = cv2.imread('image.jpg')
res = img.copy()
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_green = np.array([40,50,50])
upper_green = np.array([80,255,255])
r2, g2, b2 = 255, 0, 0
mask = cv2.inRange(hsv, lower_green, upper_green)
mask = mask/255
mask = mask.astype(np.bool)
res[:,:,:3][mask] = [b2, g2, r2] # opencv uses BGR
cv2.imshow('image', img)
cv2.imshow('Result', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
I want to detect lower to upper values of colors based on click in photo.我想根据点击照片检测 colors 的从低到高的值。 For example in this code, I used
[40,50,50]
as a lower value of green and [80,255,255]
as an upper value of green, but I want to automate this process.例如,在这段代码中,我使用
[40,50,50]
作为绿色的下限值,使用 [80,255,255 [80,255,255]
作为绿色的上限值,但我想自动化这个过程。 For instance, you clicked the green side of the photo and this return you the [40,50,50]
and [80,255,255]
例如,您单击照片的绿色一侧,这将返回
[40,50,50]
和[80,255,255]
You might use lists or dictionaries for the values of each shade (R,G,B), then by an event for the click you could get the value of BGR on the picture (like: 40,50,50) and by comparing each value by conditionals:您可以使用列表或字典来获取每个阴影(R、G、B)的值,然后通过单击事件您可以获得图片上 BGR 的值(例如:40、50、50)并通过比较每个条件值:
(if valueB > 40 and valueB < 80 and valueG > 50 and valueG < 255 and valueR > 50 and valueR < 255: print("the values of the color are [40,50,50] and [80,255,255]") ) (如果 valueB > 40 and valueB < 80 and valueG > 50 and valueG < 255 and valueR > 50 and valueR < 255: print("the values of the color are [40,50,50] and [80,255,255]") )
and in that way for the colors you want to get.并以这种方式获得您想要的 colors。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.