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

Ale*_*lek 6 python opencv

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

如果我在将球举到相机的同时启动程序,则代码有效,但是当我从视图中取出球时,程序崩溃并给我:

if cv2.contourArea(cnt[0]) > 20:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

我的代码:

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()
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。