0
votes

OpenCV version: 3.4.0 (couldn't create tag for it)

While trying to approximate found contours I'm getting following errors:

cv2.error: /io/opencv/modules/imgproc/src/shapedescr.cpp:237: error: (-215) count >= 0 && (depth == 5 || depth == 4) in function arcLength

The error is caused by line

eps=cv2.arcLength(cnt,True)

Also if I comment this part of code

eps=cv2.arcLength(cnt,True)

approx = cv2.approxPolyDP(cnt,0.01*eps,True)

I get following error

cv2.error: /io/opencv/modules/imgproc/src/drawing.cpp:2506: error: (-215) npoints > 0 in function drawContours

from line:

cv2.drawContours(gray1,[cnt],0,(0,0,255, 1),3)

Maybe it is (somehow) caused by first splitting and then merging channels of the video input?

I found similar problem which seems to be solved by

gray1 = cv2.convertScaleAbs(gray1)

Which infortunately wasn't my case. Help will be greatly appreciated. :)

I'm providing the code below:

import numpy as np
import cv2

cap = cv2.VideoCapture('video2.mp4')


while cap.isOpened():
    ret, frame = cap.read()

    frame = cv2.resize(frame, (640, 480), interpolation = cv2.INTER_LINEAR)
    gray1=frame # Is this right?

    frameArea = frame.shape[0]*frame.shape[1]

    # split the RGB image into R,G,B channels respectively
    b, g, r = frame[:, :, 0], frame[:, :, 1], frame[:, :, 2]

    # put back thresholded channels into one RGB image
    retvalb, b = cv2.threshold(b, 90, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    retvalg,g = cv2.threshold(g, 0, 70, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    retvalr,r = cv2.threshold(r, 0, 70, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    frame = cv2.merge((b,g,r))

    # create gray image in order to further threshold the result
    gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY);
    retvalgray, gray = cv2.threshold(gray1,0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU);

    gray1 = cv2.bilateralFilter(gray1, 11, 17, 17)
    gray1 = cv2.convertScaleAbs(gray1)
    # find the region of interest by drawing contours around it
    _, val,cnts = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

    for cnt in cnts:
                eps=cv2.arcLength(cnt,True)
                approx = cv2.approxPolyDP(cnt,0.01*eps,True)
                cv2.drawContours(gray1,[cnt],0,(0,0,255, 1),3)

    cv2.imshow("Detection", gray1)
    if cv2.waitKey(1) & 0xFF is ord('q'):
            cv2.destroyAllWindows()
            print("Stop programm and close all windows")
            break
2

2 Answers

0
votes

The problem was pretty simple - wrong variable order in findcountours method

WRONG:

_ , val,cnts = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

RIGHT:

_ , cnts, val = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

0
votes

The findContour() outputs a modified image, the contours and hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.

For Example: im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

You are iterating over the hierarchy. Not the Contours.

See the documentation for more information.