OpenCV的颜色跟踪和使用findContours函数的Python错误

时间:2017-01-21 19:13:56

标签: python opencv

我想跟踪3种颜色,但我对此声明有疑问:

(ti,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  

ValueError:解包需要2个以上的值

当我尝试: (contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

没有错误,但变量contourshierarchy为空,我收到cv2.imshow("Color Tracking",img)的以下错误:

  

“尺寸错误img”

我使用的是Python 2.7和OpenCV 2.4。

我的代码是:

#importing modules

import cv2   
import numpy as np

#capturing video through webcam
webcam = cv2.VideoCapture(0)

while(webcam.isOpened()):

    ret, img = webcam.read()
    if ret:
        hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    #definig the range of red color
        red_lower=np.array([136,87,111],np.uint8)
        red_upper=np.array([180,255,255],np.uint8)

    #defining the Range of Blue color
        blue_lower=np.array([99,115,150],np.uint8)
        blue_upper=np.array([110,255,255],np.uint8)

    #defining the Range of yellow color
        yellow_lower=np.array([22,60,200],np.uint8)
        yellow_upper=np.array([60,255,255],np.uint8)

    #finding the range  of red,blue and yellow color in the image
        red=cv2.inRange(hsv, red_lower, red_upper)
        blue=cv2.inRange(hsv,blue_lower,blue_upper)
        yellow=cv2.inRange(hsv,yellow_lower,yellow_upper)

    #Morphological transformation, Dilation     
        kernal = np.ones((5 ,5), "uint8")

        red=cv2.dilate(red, kernal)
        res=cv2.bitwise_and(img, img, mask = red)

        blue=cv2.dilate(blue,kernal)
        res1=cv2.bitwise_and(img, img, mask = blue)

        yellow=cv2.dilate(yellow,kernal)
        res2=cv2.bitwise_and(img, img, mask = yellow)    


       #Tracking the Red Color
        (ti,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        #cv2.findContours(red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE[, contours[, hierarchy[, offset]]])
        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)
            if(area>300):
                x,y,w,h = cv2.boundingRect(contour) 
                img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
                cv2.putText(img,"RED color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))

    #Tracking the Blue Color
        (ti,contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)
            if(area>300):
                x,y,w,h = cv2.boundingRect(contour) 
                img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
                cv2.putText(img,"Blue color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0))

    #Tracking the yellow Color
        (ti,contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)
            if(area>300):
                x,y,w,h = cv2.boundingRect(contour) 
                img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
                cv2.putText(img,"yellow  color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0))  
        #cv2.imshow("Redcolour",red)
        cv2.imshow("Color Tracking",img)
        #cv2.imshow("red",res)  
        if cv2.waitKey(10) & 0xFF == ord('q'):
                cap.release()
                cv2.destroyAllWindows()
                break 

1 个答案:

答案 0 :(得分:1)

您好我试图复制您的错误我所注意到的是您的代码中没有控件可以检查您设置的那些颜色是否在框架中可用。这就是为什么你在img size中得到错误的原因,因为它没有找到任何匹配的颜色并且它已经屏蔽了一个空数组。我的建议是尝试使用单帧和单色的代码:

import cv2   
import numpy as np
img = cv2.imread('frame1.jpg')
#Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
kernal = np.ones((5 ,5), "uint8")
# define range of blue color in HSV
yellow_lower=np.array([22,60,200],np.uint8)
yellow_upper=np.array([60,255,255],np.uint8)
# Threshold the HSV image to get only blue colors
yellow=cv2.inRange(hsv,yellow_lower,yellow_upper)
yellow=cv2.dilate(yellow,kernal)
res2=cv2.bitwise_and(img, img, mask = yellow) 
(ti,contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

for pic, contour in enumerate(contours):
    area = cv2.contourArea(contour)
    if(area>300):
        x,y,w,h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        cv2.putText(img,"yellow  color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0))  

cv2.imshow("Color Tracking",img)
cv2.waitKey(0)