Contour approximation not working correctly
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()
please avoid post duplication
Results looks goods Where do you want to put your four points ?
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?
And using just these 4 points above how will i determine if it's a circle? It could be a square or a rectangle?
convexhull solves your problem
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
convexHull() shows 7,8 points for a rectangle :P I wont be able to detect a rectangle with that.
Please anyone :\
If you want to detect only rectangle use approxPolyDP(cnt,0,True) and result musthave 4 points