findContours not working properply [closed]

asked 2015-11-24 07:11:19 -0600

arp1561 gravatar image

updated 2015-11-24 08:07:56 -0600

I am using findContours on a simple white circle i created on a paint application with a black back ground. The problem is that while the tutorials tell me that there should be only one contour surrounding that circle, when i output the contours, there are 2 different points.

Due to this reason, i am not able to use contour approx method.

Can anyone help me with this?

Thanks in Advance!

                    import cv2
                    import numpy as np

                    img = cv2.imread("url.png")

                    canny = cv2.Canny(img,0,0)

                    imgs,cnts,hie = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

                    cv2.namedWindow("img",cv2.WINDOW_NORMAL)

                    cnt = cnts[1]

                   epsilon = 0.1*cv2.arcLength(cnt,True)
                   approx = cv2.approxPolyDP(cnt,epsilon,True)

                   cv2.drawContours(img,approx,-1,(0,255,0),3)

                   cv2.imshow("img",img)

                  cv2.waitKey(0)
                  cv2.destroyAllWindows()

This is the original image(/upfiles/14483724218429244.png) This is the editied image for approximation(/upfiles/14483724673701482.png)

The black and white image/(/upfiles/14483740423517343.jpeg)

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by pklab
close date 2015-11-24 09:49:22.227229

Comments

1

Please, post code and images

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 07:17:30 -0600 )edit

Please, don't post links. Instead, write down here your code (it should contain just a few lines) and also directly post your images.

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 07:35:42 -0600 )edit

@LorenaGdL There you go!

arp1561 gravatar imagearp1561 ( 2015-11-24 07:36:16 -0600 )edit

Your original image is not a white circle over a black background. You have a blue circle, surrounded by a red circle, over a white background. So yes, probably 2 contours. Anyway, I still don't know what's your real problem

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 08:04:52 -0600 )edit

Well, even if i create an image via paint. This problem still persists.

arp1561 gravatar imagearp1561 ( 2015-11-24 08:06:48 -0600 )edit

I've linked the black and white image also

arp1561 gravatar imagearp1561 ( 2015-11-24 08:07:01 -0600 )edit

The black&white image is a .jpeg image, and thus it has compression artifacts that make it non-binary. On the other hand, RETR_TREE might not be your best mode (maybe for your application RETR_EXTERNAL suits better). Also, you can always filter the found contours to get the biggest one, and approximate that one (use contourArea())

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 08:27:23 -0600 )edit

closed because duplicated here http://answers.opencv.org/question/77... may because user wasn't able to upload images in this post ?!?

pklab gravatar imagepklab ( 2015-11-24 09:49:13 -0600 )edit