Search code examples
opencvframecentroid

Find Centroid Coordinate of whole frame in OpenCV


I'm searching on the internet for an optimum code to find the Centroid's XY-Coordinates of OpenCV Frame but failed to do so.

I know how to find the centroid/center of a contour, as below(in python):

image = cv2.imread("test.png"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] 
#print cnts

# loop over the contours
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"])

Where CX, CY is the required XY-coordinates but how to find these coordinates for whole video-frame/image in OpenCV

Please anyone can help me for the same?


Solution

  • Here is the straightforward yet simple answer to my question,

    image = cv2.imread('test.png')'
    (h, w) = image.shape[:2] #w:image-width and h:image-height
    cv2.circle(image, (w//2, h//2), 7, (255, 255, 255), -1) 
    

    where, w//2, h//2 are the required frame/image centeroid's XY-coordinates.

    I was just not thinking out of the box previously, Cheers :)