如何使用 OpenCV 找到红色区域? [英] How to find the RED color regions using OpenCV?
问题描述
我正在尝试制作一个检测红色的程序.但是有时它比平时更暗,所以我不能只使用一个值.检测不同深浅红色的合适范围是多少?我目前使用的范围是 128, 0, 0 - 255, 60, 60 但有时它甚至没有检测到我放在它前面的红色物体.
RGB
不是用于特定颜色检测的好颜色空间.HSV
将是一个不错的选择.
对于 RED,您可以选择 HSV 范围 (0,50,20) ~ (5,255,255)
和 (175,50,20)~(180,255,255)
使用以下颜色图.当然,RED range
不是那么精确,但还可以.
代码取自我的另一个答案:
相关答案:
<块引用>- 选择使用 `cv::inRange` (OpenCV) 进行颜色检测的正确 HSV 上下边界
- 如何定义阈值以仅检测图像中的绿色对象:Opencv
- 如何在 Python-OpenCV 中使用 `cv2.inRange` 检测两种不同的颜色?
- 检测像素是否为红色
当然,对于具体的问题,也许其他色彩空间也可以.
I am trying to make a program where I detect red. However sometimes it is darker than usual so I can't just use one value. What is a good range for detecting different shades of red? I am currently using the range 128, 0, 0 - 255, 60, 60 but sometimes it doesn't even detect a red object I put in front of it.
RGB
is not a good color space for specific color detection. HSV
will be a good choice.
For RED, you can choose the HSV range (0,50,20) ~ (5,255,255)
and (175,50,20)~(180,255,255)
using the following colormap. Of course, the RED range
is not that precise, but it is just ok.
The code taken from my another answer: Detect whether a pixel is red or not
#!/usr/bin/python3
# 2018.07.08 10:39:15 CST
# 2018.07.08 11:09:44 CST
import cv2
import numpy as np
## Read and merge
img = cv2.imread("ColorChecker.png")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
## Gen lower mask (0-5) and upper mask (175-180) of RED
mask1 = cv2.inRange(img_hsv, (0,50,20), (5,255,255))
mask2 = cv2.inRange(img_hsv, (175,50,20), (180,255,255))
## Merge the mask and crop the red regions
mask = cv2.bitwise_or(mask1, mask2 )
croped = cv2.bitwise_and(img, img, mask=mask)
## Display
cv2.imshow("mask", mask)
cv2.imshow("croped", croped)
cv2.waitKey()
Related answers:
- Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)
- How to define a threshold value to detect only green colour objects in an image :Opencv
- How to detect two different colors using `cv2.inRange` in Python-OpenCV?
- Detect whether a pixel is red or not
Of course, for the specific question, maybe other color space is also OK.
How to read utility meter needle with opencv?
这篇关于如何使用 OpenCV 找到红色区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!