OpenCV Circle mask anti-aliasing

Viewed 374

I'm trying to use OpenCV to overlay two images together.

Input 1 background (b.jpg):

background

Input 2 foreground (f.jpg):

foreground

Desired output:

enter image description here

Real output:

enter image description here

The idea is to overlay circular part of foreground onto background.

I'm using the code:

Mat background = imread("b.jpg");
Mat foreground = imread("f.jpg");
Mat mask{foreground.size(), foreground.type(), Scalar::all(0)};
circle(mask, Point{foreground.cols / 2, foreground.rows / 2}, foreground.cols / 2 - 10, Scalar::all(255), -1, CV_AA);
imwrite("mask.jpg", mask);
foreground.copyTo(background, mask);
imwrite("overlay.jpg", background);

For the mask itself, I can see a perfect circle draw with very smooth edge.

enter image description here

But as soon as I call copyTo with the circular mask. The resulting image has abrupt edges and seems the anti-aliasing part is completely missing.

Is there a way to make the copyTo honoring the anti-aliasing fact? Or there is an easier way to achieve the same output?

0 Answers
Related