简体   繁体   English

如何找到 colors 颜色的“上”和“下”范围?

[英]How to find a colors “Upper” and “Lower” range of a color?

I have just started learning color filtering using opencv.我刚刚开始学习使用 opencv 进行颜色过滤。 I have understood most of the basics but am stuck on one thing.我已经了解了大部分基础知识,但被困在一件事上。

import cv2
import numpy as np

img = cv2.imread("Circles.png")

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_range = np.array([169,100,100])
upper_range = np.array([189,255,255])

mask = cv2.inRange(hsv, lower_range, upper_range)

cv2.imshow("Image", img)
cv2.imshow("Mask",mask)

cv2.waitKey(0)
cv2.destroyAllWindows()

Where can I find the range of the colors which I want to filter?在哪里可以找到要过滤的 colors 的范围?

Thank you谢谢

So basically, what you're trying to do is essentially filter out a color.所以基本上,你要做的基本上是过滤掉一种颜色。 By default the images are represented in three channels Blue, Green, and Red.默认情况下,图像以蓝色、绿色和红色三个通道表示。 But, using this mode of representation, you cannot filter colors easily as the values are split into three channels.但是,使用这种表示模式,您无法轻松过滤 colors,因为这些值被分成三个通道。 That's where the HSV (Hue, saturation, value) mode of representation comes to play.这就是 HSV(色相、饱和度、值)表示模式发挥作用的地方。

The line hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) converts the BGR format image to HSV format representation.hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)将 BGR 格式图像转换为 HSV 格式表示。 Now, you can get the value of your required color and just add +-delta value to H channel and you can filter the color accordingly.现在,您可以获得所需颜色的值,只需将 +-delta 值添加到 H 通道,您就可以相应地过滤颜色。

For example, if you want to filter green color The BGR representation of gree color will be (0,255,0).例如,如果要过滤绿色,则绿色的 BGR 表示将为 (0,255,0)。 First, we need to find the equivalent color representation in HSV that is (60,255,255).首先,我们需要在 HSV 中找到等效的颜色表示,即 (60,255,255)。 We can add a [H-10, 100,100] and [H+10, 255, 255] as upper and lower values accordingly.我们可以相应地添加 [H-10, 100,100] 和 [H+10, 255, 255] 作为上限和下限。

You can convert any BGR to corresponding HSV value using.您可以使用将任何 BGR 转换为相应的 HSV 值。

color_bgr=np.uint8([[[0,255,0]]])
color_hsv = cv2.cvtColor(color_bgr,cv2.COLOR_BGR2HSV)
print(color_hsv)

Please refer to this link for more details https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html更多详情参考此链接

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

相关问题 如何根据较低和较高的粉红色范围找到 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 - How to change words in a list to upper/lower case using range - Python 如何在HSV opencv python中指定颜色的上限值和下限值 - how to i specify the upper and the lower value of a color in HSV opencv python 如何确定HSL颜色检测的上下边界? - How to determine upper and lower boundaries for HSL color detection ? 如何将颜色转换为列表? 从下到上值作为 numpy 数组 - How to convert color into a list? From lower to upper value as a numpy array 如何在python 3中找到在字符串中找到大小写字符的分组 - How to find find a grouping of upper and lower case characters in a string in python 3 您如何使用熊猫找到最低和最高? - How do you find a lower maximum and upper maximum using pandas? 如何通过指定上限和下限来查找字典中的键 - how to find the keys in dictionary by specifying upper and lower limit 如何在 pandas dataframe 中找到下限和上限? - how to find lower bound and upper bound in pandas dataframe? 如何在周期列表中查找给定值的所有下限值和上限值 - How to find all lower and upper values for a given value in a periodic list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM