【问题标题】:color detection using opencv python使用opencv python进行颜色检测
【发布时间】:2014-01-03 21:25:22
【问题描述】:

我正在尝试在 python 中运行使用 opencv 编写的脚本,该脚本使用网络摄像头跟踪彩色对象(这里的对象是蓝色的),opencv 的文档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()

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

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

【问题讨论】:

  • 我有一点 python 背景,但您似乎遇到了数据类型问题。 np.array([110, 50, 50], np.uint8) 请试试这个..
  • :D 成功了! ..我写了 np.array([110, 50, 50], dtype=np.uint8)...现在它工作正常! ty

标签: python opencv


【解决方案1】:

HSV 中蓝色的范围应为:

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

【讨论】:

    【解决方案2】:

    这是一个 HSV 颜色阈值脚本,用于确定下限和上限范围,而不是猜测和检查

    import cv2
    import sys
    import numpy as np
    
    def nothing(x):
        pass
    
    # Load in image
    image = cv2.imread('1.png')
    
    # Create a window
    cv2.namedWindow('image')
    
    # create trackbars for color change
    cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
    cv2.createTrackbar('SMin','image',0,255,nothing)
    cv2.createTrackbar('VMin','image',0,255,nothing)
    cv2.createTrackbar('HMax','image',0,179,nothing)
    cv2.createTrackbar('SMax','image',0,255,nothing)
    cv2.createTrackbar('VMax','image',0,255,nothing)
    
    # Set default value for MAX HSV trackbars.
    cv2.setTrackbarPos('HMax', 'image', 179)
    cv2.setTrackbarPos('SMax', 'image', 255)
    cv2.setTrackbarPos('VMax', 'image', 255)
    
    # Initialize to check if HSV min/max value changes
    hMin = sMin = vMin = hMax = sMax = vMax = 0
    phMin = psMin = pvMin = phMax = psMax = pvMax = 0
    
    output = image
    wait_time = 33
    
    while(1):
    
        # get current positions of all trackbars
        hMin = cv2.getTrackbarPos('HMin','image')
        sMin = cv2.getTrackbarPos('SMin','image')
        vMin = cv2.getTrackbarPos('VMin','image')
    
        hMax = cv2.getTrackbarPos('HMax','image')
        sMax = cv2.getTrackbarPos('SMax','image')
        vMax = cv2.getTrackbarPos('VMax','image')
    
        # Set minimum and max HSV values to display
        lower = np.array([hMin, sMin, vMin])
        upper = np.array([hMax, sMax, vMax])
    
        # Create HSV Image and threshold into a range.
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, lower, upper)
        output = cv2.bitwise_and(image,image, mask= mask)
    
        # Print if there is a change in HSV value
        if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
            print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
            phMin = hMin
            psMin = sMin
            pvMin = vMin
            phMax = hMax
            psMax = sMax
            pvMax = vMax
    
        # Display output image
        cv2.imshow('image',output)
    
        # Wait longer to prevent freeze for videos.
        if cv2.waitKey(wait_time) & 0xFF == ord('q'):
            break
    
    cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案3】:

      为了在 OpenCV-python 中根据颜色检测对象,我认为这个 repo 将帮助你查看这个 GitHub repo:

      https://github.com/shashiben/Opencv/blob/master/Object%20Detection/object_detect_with_hsv.py

      我确实使用轨迹栏根据 HSV 颜色跟踪了对象

      【讨论】:

      • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。另请查看how to write a good answer
      猜你喜欢
      • 2021-12-25
      • 2021-11-24
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多