Ask Your Question
0

Contour approximation not working correctly

asked 2015-11-24 08:20:47 -0600

arp1561 gravatar image

I have a white circle with black lines going from either side(as shown in the picture). The problem is that when I use the contour approx method, it just outputs points around the image. rather than a whole complete circle

GNU nano 2.2.6 File: test.py Modified

                   import cv2

                   img = cv2.imread("circ.jpeg")
                   canny = cv2.Canny(img,150,300)
                   ima,cnts,hie= cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


                   cnt = cnts[0]
                   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 image of the circle. This is how it comes.

edit retag flag offensive close merge delete

Comments

please avoid post duplication

pklab gravatar imagepklab ( 2015-11-24 09:50:20 -0600 )edit

Results looks goods Where do you want to put your four points ?

LBerger gravatar imageLBerger ( 2015-11-24 11:13:16 -0600 )edit

I actually dont want four points. I used the same technique for a similar type of rectangle with defects and it gives 4 points at 4 corners. That would be sufficient to know that it is a rectangle. But what if I want contour lines and not just points? How will I do that?

arp1561 gravatar imagearp1561 ( 2015-11-24 11:44:18 -0600 )edit

And using just these 4 points above how will i determine if it's a circle? It could be a square or a rectangle?

arp1561 gravatar imagearp1561 ( 2015-11-24 11:44:53 -0600 )edit
1

convexhull solves your problem

sturkmen gravatar imagesturkmen ( 2015-11-24 11:58:28 -0600 )edit

No I don't think that approxdp is a right way to know what shape you have found. In your code (i'm not a python user) you try to approximate your contour with a polygon where distnace between side and your contour would be less than 10% of perimeter. Results is four points but Is it a circle or an ellipse or rectangle with this results I don't know. May be you should explain your context of problem a little more

LBerger gravatar imageLBerger ( 2015-11-24 12:09:04 -0600 )edit

convexHull() shows 7,8 points for a rectangle :P I wont be able to detect a rectangle with that.

Please anyone :\

arp1561 gravatar imagearp1561 ( 2015-11-24 12:43:21 -0600 )edit

If you want to detect only rectangle use approxPolyDP(cnt,0,True) and result musthave 4 points

LBerger gravatar imageLBerger ( 2015-11-24 12:59:47 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-11-27 13:30:36 -0600

pklab gravatar image

updated 2015-11-27 13:37:52 -0600

Contour lines doesn't exists because a contour is a set of points. You can draw lines between points with polylines.

By the way as already said by @sturkmen: convexHull solves your problem ...try it.

ima,cnts,hie= cv2.findContours(canny,cv2.CV_RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnt = cnts[0]
hull = cv2.convexHull(cnt)
cv2.polylines(img,hull,true,(0,255,0),3)

image description

edit flag offensive delete link more

Comments

For me, it does not create a proper circle.

import cv2
import numpy as np

im = cv2.imread("circle.png")
img = im
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

(_,cnts,_) = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

print len(cnts)
cnt=cnts[0]
hull = cv2.convexHull(cnt)
cv2.drawContours(im,hull,-1,(0,255,0),3)

cv2.imshow("img",im)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is how mine looks like http://postimg.org/image/igdb1270x/76...

arp1561_ans gravatar imagearp1561_ans ( 2015-11-28 00:38:20 -0600 )edit

And even if i use poly lines instead of drawContours(), it gives me the same output

arp1561_ans gravatar imagearp1561_ans ( 2015-11-28 00:43:00 -0600 )edit
1

In your image all points are all in the right position check drawing... along the circle. cnts is a vector of contours(all contours in your image), cnt is a vector of points (all points for i-th contour). Also if you have just 1 contour.

With C++ all is fine, I don't have python ready to test so check some of this tutorials: Contours - 1: Getting Started , Contours - 2 : Brotherhood, Contours in OpenCV

pklab gravatar imagepklab ( 2015-11-28 02:54:45 -0600 )edit

@arp1561_ans this question looks duplicated here. Please use just one !

pklab gravatar imagepklab ( 2015-11-28 03:11:49 -0600 )edit

Please keep this question active.

I really need to figure out what the problem is. I really hope you can help me out

arp1561_ans gravatar imagearp1561_ans ( 2015-11-28 03:47:36 -0600 )edit

Your problem is simply and has been stated multiple times before! You are plotting the points instead of interconnecting them into a line. Loop over your points and draw lines between them and you problem shall be fixed...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-30 06:23:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-24 08:20:47 -0600

Seen: 2,238 times

Last updated: Nov 27 '15