B
B
BitNeBolt2018-12-28 17:27:03
Python
BitNeBolt, 2018-12-28 17:27:03

How to fix bug when approximating contours in OpenCV?

You need to find rectangles in the image. After the contours are closed, I try to approximate each of them, but an error occurs. How to fix and what is the problem?

#Отрывок кода
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closed = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)

cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)

#Вот ошибка
    peri = cv2.arcLength(c, True)
cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:237: error: (-215:Assertion failed) count >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::arcLength'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BitNeBolt, 2018-12-29
@BitNeBolt

Must not be used
But this line to define an array with the coordinates of the contours

_, cont, hier = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

D
Dmitry, 2018-12-28
@LazyTalent

Try this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question