Python 使用 openCV 识别 HSV 中的颜色范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36817133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifying the range of a color in HSV using openCV
提问by Harish
I am working on identifying the color yellow using openCV in python. I have come to this step where I have to define the lower and upper range of the color yellow in HSV.
我正在使用 python 中的 openCV 识别黄色。我已经到了这一步,我必须在 HSV 中定义黄色的下限和上限。
Example for defining the range of Blue:
定义蓝色范围的示例:
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
The HSV is usually defined in percentage, I want to know how to define the range for yellow like the example.
HSV通常以百分比定义,我想知道如何像示例一样定义黄色的范围。
This is the colorspaces tutorial I've been following.
Edit: There are some suggestions in the blog mentioned above, but it does not give me the desired output.
编辑:上面提到的博客中有一些建议,但它没有给我想要的输出。
回答by Yash
It's Simple. You can use the function, cv2.cvtColor()
.
很简单。您可以使用该功能,cv2.cvtColor()
.
Instead of passing an image, you just pass the BGR values which you want to convert to hsv.
您只需传递要转换为 hsv 的 BGR 值,而不是传递图像。
For Example, to find the HSV value of Green, type the following command
例如,要查找 Green 的 HSV 值,请键入以下命令
import numpy as np
import cv2
green = np.uint8([[[0, 255, 0]]]) #here insert the bgr values which you want to convert to hsv
hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print(hsvGreen)
lowerLimit = hsvGreen[0][0][0] - 10, 100, 100
upperLimit = hsvGreen[0][0][0] + 10, 255, 255
print(upperLimit)
print(lowerLimit)
Now, the Upper Limit will be [H+10, 100,100]
现在,上限将是 [H+10, 100,100]
and Lower Limit will be [H-10, 255, 255]
和下限将是 [H-10, 255, 255]
Official documentation (see the last part of following webpage) https://docs.opencv.org/3.1.0/df/d9d/tutorial_py_colorspaces.html
官方文档(请参阅以下网页的最后一部分)https://docs.opencv.org/3.1.0/df/d9d/tutorial_py_colorspaces.html
回答by sturkmen
take a look at this pageyou will find HSV values of the color you want.
看看这个页面,你会发现你想要的颜色的 HSV 值。
For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. Different softwares use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges.
对于 HSV,色调范围为 [0,179],饱和度范围为 [0,255],值范围为 [0,255]。不同的软件使用不同的尺度。因此,如果您将 OpenCV 值与它们进行比较,则需要对这些范围进行归一化。
i guess you are searching the values like below for yellow
我猜你正在搜索下面的黄色值
lower_blue = np.array([25,50,50])
upper_blue = np.array([32,255,255])
回答by ssssoooo
if i want to do it ,first find rgb numbers for yellow (i use 'Edit colors 'in paint) then change them to HSV whith this method:
如果我想这样做,首先找到黄色的 rgb 数字(我在油漆中使用“编辑颜色”),然后使用此方法将它们更改为 HSV:
u = np.uint8([[[0,236,236]]])
# define range of blue color in HSV
lower_yellow = np.array(cv2.cvtColor(l,cv2.COLOR_BGR2HSV))
upper_yellow = np.array( cv2.cvtColor(u,cv2.COLOR_BGR2HSV))```
回答by onur aslan
If you take pictures from a camera, it will depend on lighting condition. If your aim is to track some objects, you should always update your HSV values. My advise is keep your boundary as narrow as possible in your lighting condition.
如果您从相机拍摄照片,这将取决于照明条件。如果您的目标是跟踪某些对象,则应始终更新 HSV 值。我的建议是在您的照明条件下保持您的边界尽可能窄。