如何在 Python-OpenCV 中使用 `cv2.inRange` 检测两种不同的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48109650/
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
How to detect two different colors using `cv2.inRange` in Python-OpenCV?
提问by Nhiên Ng? ?ình
How can I define "lower" and "upper" range of two different color, such as red and blue (because red and blue are not next to each other in the HSV color)
如何定义两种不同颜色的“下”和“上”范围,例如红色和蓝色(因为红色和蓝色在 HSV 颜色中并不相邻)
This one belongs to red:
这个属于红色:
lower_red = np.array([160,20,70])
upper_red = np.array([190,255,255])
and this one belongs to blue:
而这个属于蓝色:
lower_blue = np.array([101,50,38])
upper_blue = np.array([110,255,255])
I tried to combine them using if condition or make their own function but not work, can you guys show me the solution?
我尝试使用 if 条件组合它们或创建自己的函数但不起作用,你们能告诉我解决方案吗?
P/s: OpenCV in Python
P/s:Python 中的 OpenCV
回答by u3547485
As you get two masks of color
s, then use cv2.bitwise_or
to get the final mask.
当你得到两个color
s 的掩码时 ,然后使用cv2.bitwise_or
来获得最终的掩码。
import cv2
## Read
img = cv2.imread("sunflower.jpg")
## convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
## mask of green (36,0,0) ~ (70, 255,255)
mask1 = cv2.inRange(hsv, (36, 0, 0), (70, 255,255))
## mask o yellow (15,0,0) ~ (36, 255, 255)
mask2 = cv2.inRange(hsv, (15,0,0), (36, 255, 255))
## final mask and masked
mask = cv2.bitwise_or(mask1, mask2)
target = cv2.bitwise_and(img,img, mask=mask)
cv2.imwrite("target.png", target)
Source:
来源:
Find green and yellow(the range is not that accurate):
找到绿色和黄色(范围不是那么准确):
BTW, to get more accurate range, here is a refer map in my related answer:
顺便说一句,为了获得更准确的范围,这是我的相关答案中的参考地图:
How to define a threshold value to detect only green colour objects in an image :Opencv
回答by GPPK
The below image shows the HSV Colour space, which works using Hue, Saturation & Value (or lightness).
下图显示了 HSV 色彩空间,它使用色调、饱和度和值(或亮度)。
When working in the HSV colour space it is important to remember this and that concepts such as Red & Green are a sort-of conversion back to a different data type.
在 HSV 颜色空间中工作时,重要的是要记住这一点,并且诸如红色和绿色之类的概念是一种转换回不同数据类型的方式。
Your upper and lower boundaries can tehrefore only be one point in this space but can include parts of the red and blue spectrums, i.e. purple. You would need to select threshold values that meet the criteria of whatever processing output you need.
因此,您的上下边界只能是该空间中的一个点,但可以包括红色和蓝色光谱的一部分,即紫色。您需要选择满足您需要的任何处理输出标准的阈值。
Either that or run two seperate loops, the first to threshold out the Red, and the second to threshold out your blue and then blend the two images together using OpenCV Blend functions. See herefor blending two colour spaces.
要么运行两个单独的循环,第一个循环对红色进行阈值处理,第二个循环对蓝色进行阈值处理,然后使用 OpenCV Blend 函数将两个图像混合在一起。有关混合两个颜色空间的信息,请参见此处。
回答by Abhishek yadav
# Make a copy of the image
image_copy = np.copy(image)
## TODO: Define the color selection boundaries in RGB values
# play around with these values until you isolate the blue background
lower_blue = np.array([200,0,0])
upper_blue = np.array([250,250,255])
# Define the masked area
mask = cv2.inRange(image_copy, lower_blue, upper_blue)
# Vizualize the mask
plt.imshow(mask,cmap='gray')