2

im trying to do Bilinear Interpolation after rotation in c++ using openCv features, but without using bilinear interpolation implemented in openCv.

At my output images, there are always some artifacts (totaly different colours of pixels).

Im using this formula:

Bilinear interpolation formula

Im not using ceil from math.h, but cvRound() from openCV.

So my input is: lena

And my output with artifacts is:

Lena after rotation and interpolation

Im using that formula for all RGB values so for B it looks:

int l = cvRound(xn);
int k = cvRound(yn);

float a = xn-l;
float b = yn-k;

uchar B = (1-a)*(1-b)*src.at<cv::Vec3b>(l,k).val[0]+a*(1-b)*src.at<cv::Vec3b>(l+1,k).val[0]+b*(1-a)*src.at<cv::Vec3b>(l,k+1).val[0]+a*b*src.at<cv::Vec3b>(l+1,k+1).val[0];

xn and yn are coordinates in transformation MAT, they are floats.

As you can see most of the output picture is calculated as it should be, but somehow they are few artifacts, that make me no sense. I need to get rid of them.

Thanks for any advice.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 2
    did you make sure that all the intetpolation factors sum up to 1? Did you make sure that no unsignrf byte over-/underflows occur? Split up your code line to separate all the values and factors to more easily find the error (optimize later). Maybe first compute the whole value in float precision to prevent Byte range problems (optimize later) – Micka Apr 29 '18 at 12:58
  • Are you sure the artifacts are due to the interpolation stage rather than the rotation? – G.M. Apr 29 '18 at 13:00
  • 1
    A very quick look suggests all the artifacts are in the very darkest areas, so I'm kind of thinking under/overflow when close to zero. Just a thought. – Mark Setchell Apr 29 '18 at 13:21

1 Answers1

6

The abnormal green pixels that you see must be caused by underflow (-1 becoming +255) in the green channel.

Try clamping the interpolation expression to [0,255] before assignment to B.