7
votes

I'm using openCv's warpAffine() function to rotate an image with this code-

void rotateImage(const Mat &src, Mat &dest, double angle)
{
    cv::Point2f point(src.cols/2, src.rows/2);

    Mat rotationMatrix = cv::getRotationMatrix2D(point, angle, 1.0);

    cv::warpAffine(src, dest, rotationMatrix, cv::Size(src.cols, src.rows));
}

It works fine if I rotate the image with some angle multiple of 90 (in degrees) but severly blurs the image with any arbitrary rotation angle.

eg- the original image

enter image description here

After rotating by 90 degree multiple times-

enter image description here

After rotating by 30 degree multiple times-

enter image description here

So is there any way I can rotate the image by any arbitrary angle and avoid the caused blur?

1
This is still an existing problem, and the answer given is not solving it.Schütze

1 Answers

11
votes

You could try to use the flags option to see the result of different interpolation methods:

INTER_NEAREST - a nearest-neighbor interpolation

INTER_LINEAR - a bilinear interpolation (default)

INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.

INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood

INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood

For example:

cv::warpAffine(src, dest, rotationMatrix, cv::Size(src.cols, src.rows),cv::INTER_CUBIC);

You will always lose some quality though, because of the interpolation. It is better not to do multiple warps on the same image after each other.