I need to find the center of a shape in an image. After contouring and some filtering, I get this shape
I need the center of the "inner" circle you can see on the picture. I tried with convex Hull and the traditional algorithm with distanceTransform, but in this case, the circle touches the external part of the contour. Is there a simple, or not-so-simple solution to get the center of this circle ?
[EDIT] some details
I need to find the center of a shape in an image. After contouring and some filtering, I get this shape
I need the center of the "inner" circle you can see on the picture. I tried with convex Hull and the traditional algorithm with distanceTransform, but in this case, the circle touches the external part of the contour. Is there a simple, or not-so-simple solution to get the center of this circle ?
[EDIT] some details
Share Improve this question asked Mar 17 at 15:38 rvil76rvil76 1599 bronze badges 2- There is no inner circle. A circle needs to be closed. The inner part of the lined curve is not a circle. Inner means in this case, inside your lined curve. Please identify exactly what part you want to extract. Also post your result and identify why it does not work. – fmw42 Commented Mar 17 at 16:23
- show the source data, before your contouring. when you present that, I've got a solution for you. – Christoph Rackwitz Commented Mar 17 at 17:37
1 Answer
Reset to default 0I have found a solution, using floodFill and convexHull : I first fill my "banana" in gray, then draw the convex hull and fill inside in white. With a threshold, I only keep the inner line of the banana.
mask = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask,contours, c[0], 128, 3) #c[0] is the indes of kept contour
dist = cv2.distanceTransform(mask, cv2.DIST_L2, 0)
_, max_val, _, max_indx = cv2.minMaxLoc(dist)
(x, y), radius = max_indx, max_val
cv2.floodFill(mask, None, (x, y), 128) # floodFill contour in gray
cv2.drawContours(mask,hull, c[0], 128, 3)# draw convexHull in gray
cv2.floodFill(mask, None, c[1:3], 255) #c[1:3] is the center of inscribed circle of hull
_,tresh = cv2.threshold(mask,192,255,cv2.THRESH_BINARY)
# now I can go back to standard contour process