简体   繁体   English

如何将颜色转换为列表? 从下到上值作为 numpy 数组

[英]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.

相关问题 如何将上下阈值应用于NumPy数组? - How to apply lower and upper threshold to NumPy array? 如何将 Numpy Python 中的 4 维数组的下三角形复制到上三角形? - How to copy lower triangle to upper triangle for a 4 dimension array in Numpy Python? 如何在HSV opencv python中指定颜色的上限值和下限值 - how to i specify the upper and the lower value of a color in HSV opencv python 如何在周期列表中查找给定值的所有下限值和上限值 - How to find all lower and upper values for a given value in a periodic list 如何从图像文件&gt; numpy数组&gt;单一RGB颜色的x / y坐标列表转换 - how to convert from image file > numpy array > list of x/y coords of a single RGB color 将字典中列表的值转换为大写/小写 - Convert values of a list in dictionary to upper/lower case numpy中是否有一个函数来替换numpy数组的下对角线和上对角线值? - Is there a function in numpy to replace lower and upper diagonal values of a numpy array? AttributeError: 'list' object 没有属性 'lower'。 如何修复代码以将其转换为上层或下层? - AttributeError: 'list' object has no attribute 'lower'. How to fix the code in order to have it convert to upper or lower? 如何找到 colors 颜色的“上”和“下”范围? - How to find a colors “Upper” and “Lower” range of a color? 如何将下对角元素的 Numpy 数组转换为完整矩阵的数组 - How to convert a Numpy array of lower diagonal elements to an array of full matrices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM