I'm unable to draw complete contours of a shape using cv2.approxPolyDP()
.
I'm getting the following result:
But I want output like this:
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()
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With