使用opencv python进行颜色检测

 PAISONG_675 发布于 2023-02-06 17:24

我试图运行使用Python中的OpenCV编写一个脚本,使用网络摄像头追踪彩色对象(这里的对象是蓝色),这也是OpenCV的文档中提到这里

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

但是这段代码会产生错误:

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

我已经尝试了相关的stackoverflow问题中提供的解决方案,但没有一个帮助.代码有什么问题?为什么会出现这个错误?

我在ubuntu上使用opencv 2.4.2和python 2.7

1 个回答
  • HSV中的蓝色范围应如下:

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

    2023-02-06 17:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有