'How can I draw a circle next to each other with a text in center uisng opencv in python?

I'm trying to draw a concurrent circles of equal radius on top row of image with a text on center of circle. I'm able to draw a single circle on center of image as shown below sample

I used below code to achieve this

import cv2
import numpy as np


img = np.zeros((128, 128, 3), dtype=np.uint8)
h,w = img.shape
CENTER = (64, 64)

cv2.circle(img, CENTER, 48, (127,0,127), -1)

TEXT_FACE = cv2.FONT_HERSHEY_DUPLEX
TEXT_SCALE = 0.5
TEXT_THICKNESS = 2
TEXT = "hello"

text_size, _ = cv2.getTextSize(TEXT, TEXT_FACE, TEXT_SCALE, TEXT_THICKNESS)
text_origin = (int(CENTER[0] - text_size[0] / 2), int(CENTER[1] + text_size[1] / 2))

cv2.putText(img, TEXT, text_origin, TEXT_FACE, TEXT_SCALE, (127,255,127), TEXT_THICKNESS, cv2.LINE_AA)

like this I'm trying to draw a circles next to each other on top of image as below sample2

if I give n number of circles how can I find center of them and draw circles like above ?

Any help or suggestions on solving this will be highly appreciated



Solution 1:[1]

I haven't added the text. I hope you can do that.

import cv2

img = cv2.imread('Resources/black.jpg')

# Height, Width, Channel
dimension = img.shape
width = dimension[1]
height = dimension[0]

#Drawing circle
r = 20
no_of_possible_circles = int(width//(2*r))
stepper = 1
for i in range(0,no_of_possible_circles,):
    cv2.circle(img,(stepper*r,r),r,(0,0,255),cv2.FILLED)
    stepper += 2

cv2.imshow("Image",img)
cv2.waitKey(0)

We increase the stepper by '2' because

  • center of 1st circle 'll be at (r,r)
  • center of 2nd circle 'll be at (3r,r)
  • center of 3rd circle 'll be at (5r,r) . . . and so on

This is the result I got

Final Result

Solution 2:[2]

On my side. I used linux, Raspberry pi 4 with 4k resolution. So I modified np.zero to get bigger size that I can see. You can change to suit yourself 2k or 1080p resolution. I didn't do looping to reduced to one line. Using your same as above, I added some extra snipped code.

#!/usr/bin/python3.9.2
#OpenCV 4.5.5 Raspberry pi4
#Date: 6th April, 2022

import cv2
import numpy as np


img = np.zeros((512, 512, 3), dtype=np.uint8)
hw = img.shape
CENTER = (64, 64)

cv2.circle(img, CENTER, 48, (127,0,127), -1)
cv2.circle(img, (170,64), 48, (127,0,127), -1)
cv2.circle(img, (276,64), 48, (127,0,127), -1)
cv2.circle(img, (382,64), 48, (127,0,127), -1)

TEXT_FACE = cv2.FONT_HERSHEY_DUPLEX
TEXT_SCALE = 0.5
TEXT_THICKNESS = 2
TEXT = "hello"

text_size, _ = cv2.getTextSize(TEXT, TEXT_FACE, TEXT_SCALE, TEXT_THICKNESS)
text_origin_1 = (int(CENTER[0] - text_size[0] / 2), int(CENTER[1] + text_size[1] / 2))
text_origin_2 = (int(CENTER[0] - text_size[0] / 2)+108, int(CENTER[1] + text_size[1] / 2))
text_origin_3 = (int(CENTER[0] - text_size[0] / 2)+216, int(CENTER[1] + text_size[1] / 2))
text_origin_4 = (int(CENTER[0] - text_size[0] / 2)+324, int(CENTER[1] + text_size[1] / 2))

cv2.putText(img, TEXT, text_origin_1, TEXT_FACE, TEXT_SCALE, (127,255,127), TEXT_THICKNESS, cv2.LINE_AA)
cv2.putText(img, TEXT, text_origin_2, TEXT_FACE, TEXT_SCALE, (127,255,127), TEXT_THICKNESS, cv2.LINE_AA)
cv2.putText(img, TEXT, text_origin_3, TEXT_FACE, TEXT_SCALE, (127,255,127), TEXT_THICKNESS, cv2.LINE_AA)
cv2.putText(img, TEXT, text_origin_4, TEXT_FACE, TEXT_SCALE, (127,255,127), TEXT_THICKNESS, cv2.LINE_AA)

cv2.imwrite('img.jpg', img)
cv2.imshow("Image",img)
cv2.waitKey(0)

And here is screenshot: enter image description here

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 Jeru Luke
Solution 2 toyota Supra