Visualizing Complex Functions

Click here to edit subtitle

Communication Post New Entry

Bilinear Interpolation

Posted by Mr. Watson on February 10, 2013 at 1:20 PM

Here is an article that explains what bilinear interpolation performs and how to perform it:
en.wikipedia.org/wiki/Bilinear_interpolation

Bilinear Interpolation

 

Method:

 

//the algorithm used for bilinear interpolation

//Diagram:

//                  x

// top_y *----|---- +              * = (left_x,top_y) = tlPixel (top left Pixel)

//            |      |      |               + = (right_x,top_y) = trPixel (top right Pixel)

//            |-----|---- |- y         @ = (right_x,low_y) = brPixel (bottom right Pixel)

//            |       |      |              . = (left_x,low_y) = blPixel (bottom left Pixel)

//low_y . -----|----@

//         left_x       right_x


public static int bilinearInterpolation(int tlPixel, int trPixel, int brPixel, int blPixel,

int x, int y, int left_x, int low_y, int right_x, int top_y)

{

//Bilinear Interpolation algorithm adapted from

//http://en.wikipedia.org/wiki/Bilinear_Interpolation

int value =

(

( (blPixel / ( (right_x - left_x)*(top_y - low_y) )) * (right_x - x) * (top_y - y) ) +

( (brPixel / ( (right_x - left_x)*(top_y - low_y) )) * (x - left_x) * (top_y - y) ) +

( (tlPixel / ( (right_x - left_x)*(top_y - low_y) )) * (right_x - x) * (y - low_y) ) +

( (trPixel / ( (right_x - left_x)*(top_y - low_y) )) * (x - left_x) * (y - low_y) )

);

return value;

}

 

How its called:

 

//Interpolation

Color tl, tr, lr, ll; //top left, top right, low right, low left

int alpha, red, blue, green, combo; //each color part of each pixel

 

//goes through dstImage and interpolates

//each pixel based on each of its color components

for (int i = (minx + 1); i < (maxx - 1); i++)

{

for (int j = (miny + 1); j < (maxy - 1); j++)

{

tl = new Color((int)(dstImage.getRGB((i-1), (j-1))));

tr = new Color((int)(dstImage.getRGB((i-1), (j+1))));

lr = new Color((int)(dstImage.getRGB((i+1), (j+1))));

ll = new Color((int)(dstImage.getRGB((i+1), (j-1))));

 

//bilinear interpolation on the alpha component of the 4 pixels

alpha = bilinearInterpolation(tl.getAlpha(), tr.getAlpha(), lr.getAlpha(), ll.getAlpha(), i, j, (i-1), (j-1), (i+1), (j+1));

//" " " " red " " " " "

red = bilinearInterpolation(tl.getRed(), tr.getRed(), lr.getRed(), ll.getRed(), i, j, (i-1), (j-1), (i+1), (j+1));

//" " " " green " " " " "

green = bilinearInterpolation(tl.getGreen(), tr.getGreen(), lr.getGreen(), ll.getGreen(), i, j, (i-1), (j-1), (i+1), (j+1));

//" " " " blue " " " " "

blue = bilinearInterpolation(tl.getBlue(), tr.getBlue(), lr.getBlue(), ll.getBlue(), i, j, (i-1), (j-1), (i+1), (j+1));

//combines the 4 components into the 8-bit integer representation

combo = new Color(red, green, blue, alpha).getRGB();

dstImage.setRGB(i, j, combo);

}

}


Categories: None

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

Already a member? Sign In

0 Comments