'Trying to print the center position of a square

I have tried to use cv2.putText and it appears to show the position based on the the top right of the window and not the actual center of the image. It will probably be an obvious fix since I just started using opencv

import os
import numpy as np

font = cv2.FONT_HERSHEY_SIMPLEX
  

org = (50, 50)
fontScale = 1
color = (255, 0, 0)
  
radius = 3
thickness = 2
cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)



video_capture = cv2.VideoCapture(0)
while (True):


    
    ret, frames = video_capture.read()
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )
        
    for (x, y, w, h) in faces:


        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
        text = (x+w//2), (y+h//2) 
        
        cv2.circle(frames, (cx, cy), radius, (255, 0, 0), -1)
        cv2.putText(frames, str(text), org, font, fontScale, color, thickness)






        
    cv2.imshow('Video', frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break   


video_capture.release()
cv2.destroyAllWindows()


Solution 1:[1]

With the cv2.getTextSize() function, you can calculate the pixel size of the text you will put and subtract it from the text's position. In this way, the text will be right on the center.

text = (x+w//2), (y+h//2)

text_size,t = cv2.getTextSize(text=str(text), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, thickness=1)
text_size_x,text_size_y = text_size

text_pos =  (x+w//2)-(text_size_x//2), (y+h//2)+(text_size_y//2)  

Here is a working code

import os
import numpy as np
import cv2

font = cv2.FONT_HERSHEY_SIMPLEX
  

org = (50, 50)
fontScale = 1
color = (255, 0, 0)
  
radius = 3
thickness = 2
cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)



video_capture = cv2.VideoCapture(0)
while (True):


    
    ret, frames = video_capture.read()
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )

    for (x, y, w, h) in faces:


        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
        text = (x+w//2), (y+h//2)

        text_size,t = cv2.getTextSize(text=str(text), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, thickness=1)
        text_size_x,text_size_y = text_size

        text_pos =  (x+w//2)-(text_size_x//2), (y+h//2)+(text_size_y//2)

        #cv2.circle(frames, (cx, cy), radius, (255, 0, 0), -1)
        cv2.putText(frames, str(text), text_pos, font, fontScale, color, thickness)

    cv2.imshow('Video', frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break   


video_capture.release()
cv2.destroyAllWindows()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kozmoonot