-1

I am working on an openCV project where I need to find a contours of an image. The code is working with the educational video but it produces errors when I tried it myself.

_, contours,_= cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

This is the complete code:

import cv2
import numpy as np

cap = cv2.VideoCapture("eye_recording.flv")

while True:
    ret, frame = cap.read()
    if ret is False:
        break

    roi = frame[269: 795, 537: 1416]
    rows, cols, _ = roi.shape
    gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)

    _, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
    _, contours,_= cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    print( cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE))
    #_,_,_= cv2.findContours(image=threshold, mode= cv2.PETER_TREE, method=cv2.CHAIN_APPROX_SIMPLE)

    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
    for cnt in contours:
        (x, y, w, h) = cv2.boundingRect(cnt)

        #cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
        cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
        cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
        break

    cv2.imshow("Threshold", threshold)
    cv2.imshow("gray roi", gray_roi)
    cv2.imshow("Roi", roi)
    key = cv2.waitKey(30)
    if key == 27:
        break

cv2.destroyAllWindows()

I have tried some solutions online like downloading the openCV contribs as well as printing the returned value by the "findContours" function to see expected result yet I couldn't figure out what is the problem.

2
  • add the complete error message to your question Commented Jun 23, 2021 at 19:40
  • 3
    In OpenCV 2 findContours has two outputs, in OpenCV 3 findContours has three outputs and in OpenCV 4 findContours has two outputs. For compatibility you may use: cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2] or use imutils: import imutils and contours = imutils.grab_contours(cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)) Commented Jun 23, 2021 at 19:47

1 Answer 1

0

I have got some help from a friend, whom I owe the answer credit to Mr.Ahmed, the following code would be a replica of the code line that caused the error:

(cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, _ = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)

The mean reason were in the structure of the python OpenCV library and the way to call the function. The complete code is following:

import cv2
import numpy as np


cap = cv2.VideoCapture("eye_recording.flv")

while True:
    ret, frame = cap.read()
    if ret is False: 
        break

#roi = frame[0:800,0:800]
roi = frame
rows, cols, _ = roi.shape
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)

_, thresh_delta = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
(cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours, _ = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)

for cnt in contours:
    (x, y, w, h) = cv2.boundingRect(cnt)

    #cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
    cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
    cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
    cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
    print("x="+str((x + int(w/2)))+"y="+str((y + int(h/2))))
    break

#cv2.imshow("Threshold", threshold)
cv2.imshow("gray roi", gray_roi)
cv2.imshow("Roi", roi)
key = cv2.waitKey(30)
if key == 27:
    break

cv2.destroyAllWindows()

The video is a Pysource website property. I would include a link to their site for those who are interested in getting it. Pysource.com Eye motion detection example video

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.