PNG rotation without dark edge

asked 2020-06-23 13:24:09 -0600

litozor gravatar image

updated 2020-06-23 13:32:57 -0600

When a PNG is rotated or blurred there is a dark edge appearing (see image A). It appears partially or totally on large shapes, but it's always visible on thin shapes.

image description

Do you know how to rotate or blur a PNG image with OpenCV without the dark edge?

I tried all the interpolations shown here. Only INTER_NEAREST doesn't produce dark edge, but the quality is not usable.

Rotation code sample:

cv::Size ImageSize = cv::Size(500, 500);
cv::Mat Image(ImageSize, MatType, ImageDataPtr);

cv::Point2f ImageCenter(round(Image.cols/2), round(Image.rows/2));
cv::Mat TransMat = cv::getRotationMatrix2D(ImageCenter, Angle, 1.0);

cv::Rect BoundingBox = cv::RotatedRect(ImageCenter, ImageSize, Angle).boundingRect();

TransMat.at<double>(0, 2) += BoundingBox.width / 2.0 - ImageCenter.x;
TransMat.at<double>(1, 2) += BoundingBox.height / 2.0 - ImageCenter.y;

cv::Mat RotatedImage;
cv::warpAffine(Image, RotatedImage, TransMat, BoundingBox.size(), cv::INTER_CUBIC);

Blur code sample:

cv::Mat BlurredImage;
cv::blur(Image, BlurredImage, cv::Size(2, 2), cv::Point(-1,-1), cv::BORDER_TRANSPARENT);

// OR
cv::Mat BlurredImage;
cv::blur(Image, BlurredImage, cv::Size(2, 2));
edit retag flag offensive close merge delete