I am not able to properly find the center of a triangle using openCV, the center point was drawn at the bottom tip of the triangle. Can anyone help me what is wrong in the code?
import cv2
import numpy as np
import stackImages as stack
img = cv2.imread('triangle.jpg',0)
NewImg = img.copy()
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.circle(NewImg, (cx, cy), 2, (0, 0, 255), 3)
cv2.putText(NewImg, "centroid", (cx, cy),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
while (True):
#imgStack = stack.stackImages(0.8,([img, NewImg]))
cv2.imshow('Sample', NewImg)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break


