Python中的OpenCV轮廓:如何解决列表索引超出范围'

时间:2016-03-22 16:58:14

标签: python opencv

最近,我一直在使用python中的openCV基于轮廓跟踪对象。我希望我的代码只绘制最大轮廓(大于20px)。

如果我在将球拿到相机上的同时启动程序,代码就可以工作,但是当我从视图中移除球时程序崩溃并给我:

if cv2.contourArea(cnt[0]) > 20:
IndexError: list index out of range

任何人都知道如何解决这个问题?

我的代码:

import cv2
import numpy as np

#Get current frame from camera
cap = cv2.VideoCapture(-1)

#Set size of said frame
cap.set(3, 160)
cap.set(4, 120)

while True:

    _, frame = cap.read()

    #Remove Noise and Grain
    median = cv2.medianBlur(frame,5)

    # Convert BGR to HSV
    hsv = cv2.cvtColor(median, cv2.COLOR_BGR2HSV)

    # define range of red color in HSV
    lower_red = np.array([0,200,0])
    upper_red = np.array([40,255,255])

    # Threshold the HSV image to get only red colors
    thresh = cv2.inRange(hsv, lower_red, upper_red)

    #Eliminate small peices of a thresholded image
    kernel = np.ones((5,5),np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

    #Detect contours in an image
    contours, hierarchy =  cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    #Find only the biggest contour
    cnt = sorted(contours, key=cv2.contourArea, reverse=True)[:1]

    #If there is a "biggest contour," then check to make sure it is the right size
    if cnt != None:
        if cv2.contourArea(cnt[0]) > 20:
            target = True
        else:
            target = False;

        if target == True:
            cv2.drawContours(frame, cnt, -1, (0,255,0), 3)
        else:
            print 'False \n'
    else:
         print 'No Contours Detected \n'


    cv2.imshow('Frame',frame)
    cv2.imshow('Threshold', thresh)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
         break

 cv2.destroyAllWindows()

感谢您的帮助。

0 个答案:

没有答案