使用opencvpython进行颜色检测

使用opencvpython进行颜色检测,python,opencv,Python,Opencv,我试图运行一个在python中使用opencv编写的脚本,它使用网络摄像头跟踪彩色对象(这里的对象是蓝色的),opencv的文档中也提到了这一点 但此代码会产生错误: 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

我试图运行一个在python中使用opencv编写的脚本,它使用网络摄像头跟踪彩色对象(这里的对象是蓝色的),opencv的文档中也提到了这一点

但此代码会产生错误:

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
OpenCV错误:输入参数的大小不匹配(较低的二进制数既不是与src大小和类型相同的数组,也不是标量),在范围内,file/build/buildd/OpenCV-2.4.2+dfsg/modules/core/src/arithm.cpp,第2527行
回溯(最近一次呼叫最后一次):
文件“blue.py”,第19行,在
遮罩=cv2.inRange(hsv、下蓝、上蓝)
cv2.error:/build/buildd/opencv-2.4.2+dfsg/modules/core/src/arithm.cpp:2527:error:(-209)下二进制既不是与src大小和类型相同的数组,也不是范围中的标量函数
我尝试过在相关的stackoverflow问题中提供的解决方案,但没有一个有效。 代码有什么问题?为什么会出现此错误


我在ubuntu上使用opencv 2.4.2和python 2.7

HSV中的蓝色范围应如下所示:

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

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


为了在OpenCV python中检测基于颜色的对象,我认为此repo将帮助您查看此GitHub repo:


我确实使用trackbar跟踪了基于HSV颜色的对象,我有一点python背景,但似乎您有一个数据类型问题。np.array([110,50,50],np.uint8)请试试这个….:D,效果不错!。我编写了np.array([110,50,50],dtype=np.uint8)…现在它可以正常工作了!TY鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。请始终引用重要链接中最相关的部分,以防无法访问目标网站或永久脱机。请同时检查
lower_blue = np.array([110, 50, 50], dtype=np.uint8)
upper_blue = np.array([130,255,255], dtype=np.uint8)
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()