Home > Blockchain >  anti-aliased circle is jagged in opencv
anti-aliased circle is jagged in opencv

Time:03-02

It may seem like a common OpenCV recipe for a circle yields really jagged edges to the circles being drawn. Here's minimally viable reproducible code:

import cv2
import numpy as np

image = np.zeros((512,512,3), np.uint8) 
cv2.circle(image, (350, 350), 200, (15,75,50), cv2.FILLED)
cv2.imshow("Circle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

image of jagged circle circumference

It's perhaps a little better in the case of a non-filled circle (when using lineType=cv2.LINE_AA):

similar to previous

But it's sometimes nice to draw circles which are both filled and have a good edge to their circumference at the same time.

I wonder if this outcome can be improved within the framework of using OpenCV, even though OpenCV probably does not try to be a pixel perfect drawing library.

Thanks in advance for your help as I narrow down my choice between OpenCV and Pyglet for a certain computer vision with graphics implementation. And also because it would be nice to have visually pleasing conventional shapes even for just your haphazard use of shape drawing in some OpenCV laden applications.

CodePudding user response:

Use both thickness=cv.FILLED and lineType=cv.LINE_AA in the same call.

You should always consult the documentation of the technologies you use.

import cv2
import numpy as np

image = np.zeros((512,512,3), np.uint8) 
cv2.circle(image, (350, 350), 200, (15,75,50), thickness=cv2.FILLED, lineType=cv2.LINE_AA)
cv2.imshow("Circle", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • Related