Home > Blockchain >  Is there any way to make an anti-aliased circle in OpenCV
Is there any way to make an anti-aliased circle in OpenCV

Time:11-19

I'm trying to draw a circle in a picture using open CV with Python.

Here is the picture I wish I can make :

enter image description here

Here is the code I write :

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)  

But this is what my code generate :

enter image description here

The circle line is not precise. Is there anything I can do to make the line preciser or is there any other library that generates circle with precise line ?

Any suggestion will be very appreciated !

CodePudding user response:

You can use anti aliasing to make the circle look better as described here:

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