OpenCV& Python - 无法检测蓝色对象 [英] OpenCV & Python -- Can't detect blue objects
问题描述
我正在看这个问题:
然而经过多次试验和错误,我仍然无法弄清楚如何检测蓝色物体。
Yet after much trial and error, I still can't figure out how to detect blue objects.
这是我的代码:
import cv2
import numpy as np
cam=cv2.VideoCapture(0)
n=0
while True:
print n
returnVal,frame=cam.read()
img=cv2.GaussianBlur(frame, (5,5), 0)
img=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
blue_lower=np.array([150,150,0],np.uint8)
blue_upper=np.array([180,255,255],np.uint8)
blue=cv2.inRange(img,blue_lower,blue_upper)
cv2.imshow('img',blue)
n=n+1
key = cv2.waitKey(10) % 0x100
if key == 27: break #ESC
我可以检测到红色对象通过设置t他跟随以下几行:
I can detect red objects by setting the following lines:
red_lower=np.array([0,150,0],np.uint8)
red_upper=np.array([10,255,255],np.uint8)
当我把一张蓝纸放入使用第一个代码在我的摄像头前面,它只显示黑色。
When I put a blue piece of paper in front of my webcam using the first code, it just shows up black.
有人可以帮我转换为蓝色的RGB转换成HSV吗?
Can someone please help me to convert RGB for blue colours into HSV?
非常感谢提前,
推荐答案
蓝色表示在 HSV 的色调在360度左右240度左右.OpenCV-HSV中的Hue范围是0-180,用于存储值在8位。因此,蓝色在OpenCV-HSV中表示为H值 240/2 = 120
。
Blue is represented in HSV at a hue of around 240 degrees out of 360. The Hue range in OpenCV-HSV is 0-180, to store the value in 8 bits. Thus, blue is represented in OpenCV-HSV as a value of H around 240 / 2 = 120
.
To正确检测蓝色,可以选择以下值:
To detect blue correctly, the following values could be chosen:
blue_lower=np.array([100,150,0],np.uint8)
blue_upper=np.array([140,255,255],np.uint8)
这篇关于OpenCV& Python - 无法检测蓝色对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!