Skip to content

Instantly share code, notes, and snippets.

@rezamarzban
Created August 22, 2022 04:15
Show Gist options
  • Save rezamarzban/65701519bd5c75d1cd80ef34116ab1d4 to your computer and use it in GitHub Desktop.
Save rezamarzban/65701519bd5c75d1cd80ef34116ab1d4 to your computer and use it in GitHub Desktop.
Function to calculate image centroid by using OpenCV in Python3
import cv2
i = cv2.imread('img6.jpg')
img_g = cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)
ret,img = cv2.threshold(img_g,127,255,0)
M = cv2.moments(img)
X = int(M["m10"] / M["m00"])
Y = int(M["m01"] / M["m00"])
cv2.circle(i, (X, Y), 5, (255, 255, 255), -1)
cv2.imshow("Output", i)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment