如何在 python 中使用 opencv 绘制一个彼此相邻的圆圈,中间有一个文本?
How can I draw a circle next to each other with a text in center uisng opencv in python?
我正在尝试在图像的顶行绘制一个等半径的并发圆,圆心上有一个文本。我可以在图像中心画一个圆圈,如下所示
我使用下面的代码来实现这个
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)
就像这样,我正在尝试在图像顶部绘制一个彼此相邻的圆圈,如下所示
如果我给出 n 个圆圈,我如何找到它们的中心并像上面那样画圆圈?
任何解决此问题的帮助或建议将不胜感激
我还没有添加文本。希望你能做到。
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)
我们将步进器增加“2”,因为
- 第一个圆的中心在 (r,r)
- 第二个圆的中心在 (3r,r)
- 第三个圆的中心在 (5r,r)
.
.
.
等等
这是我得到的结果
在我这边。我使用 linux、Raspberry pi 4 和 4k 分辨率 。所以我修改了 np.zero 以获得更大的尺寸,我可以看到。您可以更改以适合自己的 2k 或 1080p 分辨率。我没有循环减少到一行。使用与上面相同的方法,我添加了一些额外的代码片段。
#!/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)
这是屏幕截图:
我正在尝试在图像的顶行绘制一个等半径的并发圆,圆心上有一个文本。我可以在图像中心画一个圆圈,如下所示
我使用下面的代码来实现这个
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)
就像这样,我正在尝试在图像顶部绘制一个彼此相邻的圆圈,如下所示
如果我给出 n 个圆圈,我如何找到它们的中心并像上面那样画圆圈?
任何解决此问题的帮助或建议将不胜感激
我还没有添加文本。希望你能做到。
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)
我们将步进器增加“2”,因为
- 第一个圆的中心在 (r,r)
- 第二个圆的中心在 (3r,r)
- 第三个圆的中心在 (5r,r) . . . 等等
这是我得到的结果
在我这边。我使用 linux、Raspberry pi 4 和 4k 分辨率 。所以我修改了 np.zero 以获得更大的尺寸,我可以看到。您可以更改以适合自己的 2k 或 1080p 分辨率。我没有循环减少到一行。使用与上面相同的方法,我添加了一些额外的代码片段。
#!/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)
这是屏幕截图: