How to find the center of the contour?
I've got some images with contours and I need to find their center. I'm using OpenCV and it will be great if the solution will be within OpenCV.
I've got some images with contours and I need to find their center. I'm using OpenCV and it will be great if the solution will be within OpenCV.
If you need to solve it with OpenCV, just use momentum and find contours for the mask. Here is how you can make it.
import cv2
import imutils
import numpy as np
import matplotlib.pyplot as plt
mask = cv2.imread('resources/img.png')
plt.imshow(mask, cmap='gray')
plt.show()
gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the center of the shape on the image
cv2.circle(mask, (cX, cY), 7, (0, 0, 255), -1)
# show the image
plt.imshow(mask, cmap='gray')
plt.show()
This is how you can get the centers for every contour.
Great, it is so helpful. Thanks