I would like to get the enclosing circle on simple filled shapes. I'm using opencv and python for that. I use the minEnclosingCircle function. However on some examples I get weird results:
The center is supposed to be centred on the shape, and the circle is supposed to touch the shape. This is obviously not the case.
The interesting thing is that the center is not the same with the same picture rotated 90°. It is less shifted. There is no reason why a rotation changes the center of the minimum enclosing circle. This means this function is bugged, and I need an alternative. I just need the center of the circle though.
Here is a small version of my code:
import cv2
import numpy as np
img = cv2.imread('testodd3.jpg',cv2.CV_LOAD_IMAGE_COLOR)
pic = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(pic,127,255,0)
thresh = cv2.bitwise_not(thresh)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(img,center,radius,(0,255,0),3)
cv2.circle(img,center,3,(255,255,0),3)
cv2.drawContours(img, [cnt], 0, (0,0,255), 3)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I saw a similar problem, but the solution uses a C++ library: Unexpected results of minEnclosingCircle in OpenCV