Image Scaling

Sometimes, we’ll have to scale an image to fit into an article or enlarge and image to have a better view on the smaller aspects in an image.

There’re a few methods of image scaling methods out there, some of them are: nearest neighbour replication, bilinear interpolation and bicubic interpolation. The method that I’ll be showing here is Bilinear Interpolation which is a widely used method.

The main formula for bilinear interpolation is:
v(x,y) = ax + by + cxy +d

Which is derived from the linear interpolation of a straight line.

Below is the derivation from this article

linear.png

We have Y in between 2 pixels (A and B), from that we can derive the equation

lieq.gif

We know that an image exist in a 2D space, thus in a 2D space we have:

enlarge.png

Same as above, we’ll apply the same formula mentioned above to derive the equation from i and j with respective to A&B and C&D respectively, thus we have:

eq1.gif

and

eq2.gif

Thus with i and j we can apply the same formula for Y:

eq3.gif

After some algebraic manipulation of substituting equation 1 and 2 into 3 we get:

bilineareq.gif

Here we have the same formula for the main formula of bilinear interpolation we mentioned in the very beginning.

Implementation

The method above will be applied for every channel of a pixel (R,G,B,Alpha) or (R,G,B) for 3 Bytes representation of a pixel.

A, B, C, D are from the image to be scaled. They are all neighbouring pixels with a distance of 1.

Y is the resulting pixel of the scaled image.

w, h are the ratio of input image over scaled image width and height.

Plug them into the equation and we’ll get our desired result!


Here is the source code of my implementation: Image Scaling

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s