如何使用OpenCV查找红色的颜色区域? [英] How to find the RED color regions using OpenCV?

查看:1112
本文介绍了如何使用OpenCV查找红色的颜色区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个检测到红色的程序.但是有时它比平时更暗,所以我不能只使用一个值. 检测不同深红色的最佳范围是多少? 我当前使用的范围是128、0、0-255、60、60,但有时甚至无法检测到放置在其前面的红色物体.

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并不是用于特定颜色检测的良好颜色空间. HSV将是一个不错的选择.

RGBis not a good color space for specific color detection. HSV will be a good choice.

对于红色,您可以使用以下颜色图选择HSV范围(0,50,20) ~ (5,255,255)(175,50,20)~(180,255,255).当然,RED range并不那么精确,但是还可以.

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()

相关答案:

  1. 选择使用`cv :: inRange`(OpenCV)进行颜色检测的正确HSV上下边界正确
  2. 如何在Python-OpenCV中使用`cv2.inRange`检测两种不同的颜色?
  3. 检测像素是否为红色
  1. Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)
  2. How to define a threshold value to detect only green colour objects in an image :Opencv
  3. How to detect two different colors using `cv2.inRange` in Python-OpenCV?
  4. Detect whether a pixel is red or not

当然,对于特定的问题,也许其他颜色空间也可以.

Of course, for the specific question, maybe other color space is also OK.

如何使用opencv读取电表针?

这篇关于如何使用OpenCV查找红色的颜色区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆