3
3
3Create2020-06-14 21:41:12
Python
3Create, 2020-06-14 21:41:12

List index out of range when using contours?

I decided to highlight the contours through the camera.
Before that, I tried it through the video. Works there.
Crashes list index out of range .
Maybe you need to somehow process the video stream through numpy?

from ueye.ueye_camera import UeyeCameraCapture
import cv2
import numpy as np

cam1 = UeyeCameraCapture(0)


def camera1():
    ret, frame = cam1.read()
    thresh = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    sortcontourArea = [cnt for cnt in contours if 0 < cv2.contourArea(cnt) < 2000]
    good_contours = sorted(sortcontourArea, key=cv2.contourArea)

    contour1 = good_contours[0]
    contour2 = good_contours[1]

    cv2.drawContours(frame, contour1, 0, 255, -1)
    cv2.drawContours(frame, contour2, 0, 255, -1)

    cv2.imshow('frame', frame)


while True:
    camera1()

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()


Camera Connection success
Traceback (most recent call last):
  File "D:/Crea/P/Python/lesson1/npy/np6.py", line 25, in <module>
    camera1()
  File "D:/Crea/P/Python/lesson1/npy/np6.py", line 15, in camera1
    contour1 = good_contours[0]
IndexError: list index out of range
destructor called
Camera connection closed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-08-03
@2ord

Judging by the error, the case when it is sortcontourAreaempty is not processed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question