简体   繁体   English

如何在 python 的中心 uisng opencv 中用文本绘制一个彼此相邻的圆圈?

[英]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我可以在图像中心画一个圆圈,如下所示样本

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像这样我试图在图像顶部绘制一个彼此相邻的圆圈,如下所示样品2

if I give n number of circles how can I find center of them and draw circles like above?如果我给出 n 个圆圈,如何找到它们的中心并像上面那样画圆圈?

Any help or suggestions on solving this will be highly appreciated任何解决此问题的帮助或建议将不胜感激

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我们将步进器增加“2”,因为

  • center of 1st circle 'll be at (r,r)第一个圆的中心位于 (r,r)
  • center of 2nd circle 'll be at (3r,r)第二个圆的中心在 (3r,r)
  • center of 3rd circle 'll be at (5r,r).第三个圆的中心位于 (5r,r)。 . . . . and so on等等

This is the result I got这是我得到的结果

最后结果

On my side.在我这边。 I used linux, Raspberry pi 4 with 4k resolution .我用的是 linux,4k分辨率的树莓派 4。 So I modified np.zero to get bigger size that I can see.所以我修改了 np.zero以获得更大的尺寸,我可以看到。 You can change to suit yourself 2k or 1080p resolution.您可以更改以适合自己的 2k 或 1080p 分辨率。 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:这是截图: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM