python颜色识别,使用opencv python进行颜色检测

I am trying to run a script written using opencv in python which uses webcam to track colored objects (here the object is blue colored), which is also mentioned in opencv's documentation here

import cv2

import numpy as np

cap = cv2.VideoCapture(0)

while(1):

# Take each frame

_, frame = cap.read()

# Convert BGR to HSV

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV

lower_blue = np.array([110,50,50])

upper_blue = np.array([130,255,255])

# Threshold the HSV image to get only blue colors

mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Bitwise-AND mask and original image

res = cv2.bitwise_and(frame,frame, mask= mask)

cv2.imshow('frame',frame)

cv2.imshow('mask',mask)

cv2.imshow('res',res)

k = cv2.waitKey(5) & 0xFF

if k == 27:

break

cv2.destroyAllWindows()

But this code produces error :

OpenCV Error: Sizes of input arguments do not match (The lower bounary is neither an array of the same size and same type as src, nor a scalar) in inRange, file /build/buildd/opencv-2.4.2+dfsg/modules/core/src/arithm.cpp, line 2527

Traceback (most recent call last):

File "blue.py", line 19, in

mask = cv2.inRange(hsv, lower_blue, upper_blue)

cv2.error: /build/buildd/opencv-2.4.2+dfsg/modules/core/src/arithm.cpp:2527: error: ( (-209) The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange

I've tried solutions provided in related stackoverflow questions, but none of them helped.

What is the problem with the code ? why this error arises ?

I'm using opencv 2.4.2 & python 2.7 on ubuntu

解决方案

The range of blue color in HSV should be given as :

lower_blue = np.array([110, 50, 50], dtype=np.uint8)

upper_blue = np.array([130,255,255], dtype=np.uint8)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值