简体   繁体   English

OpenCV inRange()适用于RGB,但不适用于HSV颜色空间

[英]OpenCV inRange() is working for RGB but not HSV color space

Ive generated a scatter plot of my image in RGB and HSV format and am using inRange() to threshold a single color from eyeballing the plot. 我已经以RGB和HSV格式生成了我的图像的散点图,并且正在使用inRange()来限制单色的发生,以免引起眼球。

To get exact RGB and HSV values I'm using paint.net's color picker to get the RGB value and then an RGB to HSV converter to get the HSV values. 为了获得准确的RGB和HSV值,我使用paint.net的颜色选择器获取RGB值,然后使用RGB到HSV转换器获取HSV值。

在此处输入图片说明

The pixel colors and the scatter plot are generated by: 像素颜色和散点图通过以下方式生成:

img = cv2.imread('C:\\b_.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1], 3))
norm = colors.Normalize(vmin=-1., vmax=1.)
norm.autoscale(pixel_colors)
pixel_colors = norm(pixel_colors).tolist()

h, s, v = cv2.split(img_hsv)
fig = plt.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d")

axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
axis.set_xlabel("Hue")
axis.set_ylabel("Saturation")
axis.set_zlabel("Value")
plt.show()

I need to extract the yellow from my image. 我需要从图像中提取黄色。 As mentioned I am using paint.net color picker to get the RGB values of light yellow and dark yellow. 如前所述,我正在使用paint.net颜色选择器来获取浅黄色和深黄色的RGB值。 And then use the converter to get the HSV values for the inRange() function. 然后使用转换器获取inRange()函数的HSV值。

light_yellow = (60, 89, 97)    # HSV VALUES
dark_yellow = (61, 36.6, 43.9)

mask = cv2.inRange(img_hsv, light_yellow, dark_yellow)
result = cv2.bitwise_and(img, img, mask=mask)

but the result generated is a black image, however if I use the RGB values directly of light and dark yellow and use the RGB image, not the HSV converted image, the segmentation works. 但是生成的结果是黑色图像,但是如果我直接使用浅黄色和深黄色的RGB值并使用RGB图像而不是HSV转换的图像,则分割有效。

light_yellow = (249, 249, 125)    # RGB VALUES
dark_yellow = (111, 112, 71)

mask = cv2.inRange(img, light_yellow, dark_yellow)
result = cv2.bitwise_and(img, img, mask=mask)

在此处输入图片说明

Although the above is RGB segmentation, I feel it may be improved in HSV. 尽管以上是RGB分割,但我认为HSV可能会得到改善。 Why is my HSV range not giving an output? 为什么我的HSV范围没有输出?

As it is expressed in the documentation of cvtColor . 正如在cvtColor文档中所表达的那样 When it is CV_8U the H value which normally goes from 0 to 360, it is divided by 2 and goes from 0-180. 当它是CV_8U时,H值通常从0到360,它将被2除以从0-180的值。 The S and V values are usually percentage (0-100%) and they go from 0-255. S和V值通常为百分比(0-100%),范围为0-255。

So your value: 所以你的价值:

light_yellow = (60, 89, 97)    # HSV VALUES
dark_yellow = (61, 36.6, 43.9)

Should be more like: 应该更像:

# (H/2, (S/100) * 255, (V/100) * 255) 
light_yellow = (30, 227, 247)    # HSV VALUES
dark_yellow = (31, 93, 112)

Now you have another problem, inRanges looks for valus inside the low and high range, not by light and dark yellow. 现在您遇到了另一个问题,inRanges会在上下限范围内查找值,而不是浅黄色和深黄色。 So your limits should be: 因此,您的限制应为:

low = (30,93,112)
high = (31, 227,247)

mask = cv2.inRange(img_hsv, low, high)

One more thing, I would use a bigger range for the H color... 30-31 is quite small, maybe 20-32 is better? 还有一件事,我会为H颜色使用更大的范围... 30-31很小,也许20-32更好?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM