python识别出蓝色_OpenCV& Python - 无法检测蓝色对象

I was looking at this question:

Yet after much trial and error, I still can't figure out how to detect blue objects.

Here is my code:

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

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.

Can someone please help me to convert RGB for blue colours into HSV?

Many thanks in advance,

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值