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