Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing contours using cv2.approxPolyDP() in python

I'm unable to draw complete contours of a shape using cv2.approxPolyDP().

I'm getting the following result:

My result

But I want output like this:

This is what I'm looking for

Here's my code:

import cv2
im = cv2.imread('C:\Python27\Test\Targets\s1.jpg') # read picture

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # BGR to grayscale

ret, thresh = cv2.threshold(imgray, 200, 255, cv2.THRESH_BINARY)

countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

epsilon = 0.1 * cv2.arcLength(countours[0], True)
approx = cv2.approxPolyDP(countours[0], epsilon, True)

cv2.drawContours(im, approx, -1, (0, 255, 0), 3)
cv2.imshow("Contour", im)

cv2.waitKey(0)
cv2.destroyAllWindows()
like image 240
Vikas Tomar Avatar asked Jan 05 '23 18:01

Vikas Tomar


2 Answers

cv2.CHAIN_APPROX_SIMPLE removes all redundant points and compresses the contour, thereby saving memory. If you pass to findContours() function cv2.CHAIN_APPROX_NONE parameter instead of cv2.CHAIN_APPROX_SIMPLE, your problem will be solved. Your code should be changed as below:

_, countours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, 
                                                 cv2.CHAIN_APPROX_NONE)
like image 162
aysebilgegunduz Avatar answered Jan 08 '23 05:01

aysebilgegunduz


it's quite long time ago, but I suggest you this way

contours,hierarchy = cv2.findContours(thresh, 1, 2)

contours_sizes= [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]

countours = biggest_contour

this will find biggest contour of the image, which ignores small dots and noise

like image 45
chan so Avatar answered Jan 08 '23 05:01

chan so