有没有办法在OpenCV中制作一个抗锯齿的圆

fcy6dtqo  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(225)

我正在尝试用Python中的open CV在图片中画一个圆。
这是我希望自己能画出的画面:

下面是我编写的代码:

import cv2  
import numpy as np  
import imutils

text1 = "10x"  
text2 = "20gr"  

# Load image in OpenCV  
image = cv2.imread('Sasa.jfif')
resized = imutils.resize(image, width=500)
cv2.circle(resized,(350,150),65,(102,51,17),thickness=-1)

# Convert the image to RGB (OpenCV uses BGR)  
cv2_im_rgb = cv2.cvtColor(resized,cv2.COLOR_BGR2RGB)  

# Pass the image to PIL  
pil_im = Image.fromarray(cv2_im_rgb)  

draw = ImageDraw.Draw(pil_im)  
# use a truetype font  
font1 = ImageFont.truetype("arial.ttf", 50)  
font2 = ImageFont.truetype("arial.ttf", 25)  

# Draw the text  
draw.text((310,110), text1, font=font1) 
draw.text((325,170), text2, font=font2) 

# Get back the image to OpenCV  
cv2_im_processed = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)  

cv2.imshow('Fonts', cv2_im_processed)  
cv2.waitKey(1)

但这是我的代码生成的:

圆的线不精确。有什么我可以做的,使线精确或有任何其他库,生成圆与精确的线?
任何建议都将不胜感激!

ddrv8njm

ddrv8njm1#

您可以使用抗锯齿功能使圆看起来更好,如here所述:

cv2.circle(resized,(350,150),65,(102,51,17),thickness=-1,lineType=cv2.LINE_AA)

相关问题