Bilinear interpolation to enlarge bitmap images

f0rfun picture f0rfun · Jan 10, 2012 · Viewed 16.6k times · Source

I'm a student, and I've been tasked to optimize bilinear interpolation of images by invoking parallelism from CUDA.

The image is given as a 24-bit .bmp format. I already have a reader for the .bmp and have stored the pixels in an array.

Now I need to perform bilinear interpolation on the array. I do not understand the math behind it (even after going through the wiki article and other Google results). Because of this I'm unable to come up with an algorithm.

Is there anyone who can help me with a link to an existing bilinear interpolation algorithm on a 1-D array? Or perhaps link to an open source image processing library that utilizes bilinear and bicubic interpolation for scaling images?

Answer

nsanders picture nsanders · Jan 10, 2012

The easiest way to understand bilinear interpolation is to understand linear interpolation in 1D.

This first figure should give you flashbacks to middle school math. Given some location a at which we want to know f(a), we take the neighboring "known" values and fit a line between them.

Linear interpolation in 1D.

So we just used the old middle-school equations y=mx+b and y-y1=m(x-x1). Nothing fancy.

We basically carry over this concept to 2-D in order to get bilinear interpolation. We can attack the problem of finding f(a,b) for any a,b by doing three interpolations. Study the next figure carefully. Don't get intimidated by all the labels. It is actually pretty simple.

Bilinear interpolation as three 1D interpolations.

For a bilinear interpolation, we again using the neighboring points. Now there are four of them, since we are in 2D. The trick is to attack the problem one dimension at a time.

We project our (a,b) to the sides and first compute two (one dimensional!) interpolating lines.

  • f(a,yj) where yj is held constant
  • f(a,yj+1) where yj+1 is held constant.

Now there is just one last step. You take the two points you calculated, f(a,yj) and f(a,yj+1), and fit a line between them. That's the blue one going left to right in the diagram, passing through f(a,b). Interpolating along this last line gives you the final answer.

I'll leave the math for the 2-D case for you. It's not hard if you work from the diagram. And going through it yourself will help you really learn what's going on.

One last little note, it doesn't matter which sides you pick for the first two interpolations. You could have picked the top and bottom, and then done the third interpolation line between those two instead. The answer would have been the same.