Results 1 to 2 of 2
 06152010, 12:07 AM #1Member
 Join Date
 Jun 2010
 Posts
 1
 Rep Power
 0
Bilinear Interpolation Scaling help
Hi,
I'm trying to program a class for a project, that reads a picture as BufferedImage, and resize it using Bilinear Interpolation (Using the mathematical formulas, not the methods included in java). I almost sure that the Bilinear Interpolation part of the method is done, but after many thinking, searching and losing hope, I just can't figure out how to apply the scaling part to the method.
Here's the method:
Java Code:public static BufferedImage resize(BufferedImage image, int w2, int h2) { BufferedImage outputImage = new BufferedImage( w2, h2, BufferedImage.TYPE_INT_RGB ); // Ratio between original picture and resized picture float x_ratio = ((float) (image.getWidth()1))/w2 ; float y_ratio = ((float) (image.getHeight()1))/h2; // Four pixels a,b,c,d int a, b, c, d, i, j, color; float alpha, beta, xabRed, xabGreen, xabBlue, xcdRed, xcdGreen, xcdBlue, xfinalRed, xfinalGreen, xfinalBlue; for (int x = 0; x < image.getHeight()1; x++) for( int y = 0; y < image.getWidth()1; y++){ //Find the color values of the 4 pixels around the desired point to interpolate a = image.getRGB(x, y); b = image.getRGB(x, y + 1); c = image.getRGB(x + 1, y); d = image.getRGB(x + 1, y + 1); i = (int) (x_ratio * x); j = (int) (y_ratio * y); alpha = (x_ratio * x)  i; beta = (y_ratio * y)  j; // a,b represents 2 of the 4 points for interpolation xabRed = alpha * ((b >> 16)&0xff) + (1  alpha) * ((a >> 16)&0xff); xabGreen = alpha * ((b >> 8)&0xff) + (1  alpha) * ((a >> 8)&0xff); xabBlue = alpha * (b &0xff) + (1  alpha) * (a &0xff); // c,d represents 2 of the 4 points for interpolation xcdRed = alpha * ((d >> 16)&0xff) + (1  alpha) * ((c >> 16)&0xff); xcdGreen = alpha * ((d >> 8)&0xff) + (1  alpha) * ((c >> 8)&0xff); xcdBlue = alpha * (d &0xff) + (1  alpha) * (c &0xff); // Desired point xfinalRed = beta * xcdRed + (1  beta) * xabRed; xfinalGreen = beta * xcdGreen + (1  beta) * xabGreen; xfinalBlue = beta * xcdBlue + (1  beta) * xabBlue; // Prints the RGB values, for bound confirmation System.out.println((int) xfinalRed + " " + (int) xfinalGreen + " " + (int) xfinalBlue); // Sets the new color value for the pixel color = new Color((int)xfinalRed, (int)xfinalGreen, (int)xfinalBlue).getRGB(); // Set the pixel outputImage.setRGB(x, y, color); } return outputImage; }
Thanks in advance
 06152010, 07:12 PM #2Member
 Join Date
 Jun 2010
 Posts
 28
 Rep Power
 0
For each (x,y) in the scaled image, divide by scaleX && scaleY to get (x', y')  the coordinates in the original image that maps to (x,y).
Find the four points that surround (x',y') in the original image, do the bilinear interpolation, and finally store the results at (x,y) in the scaled image.
So...
Java Code:for(int y = 0; y < outputImage.getHeight(); y++) { for(int x = 0; x < outputImage.getWidth(); x++) { //the point in the original image that maps to (x,y) double p = x/scaleX; double q = y/scaleY; //find the four points that surround (p,q) ... //do the bilinear interpolation ... //store the results in (x,y) int color = (255<<24)(xfinalRed<<16)(xfinalGreen<<8)(xfinalBlue); outputImage.setRGB(x, y, color); } }
Similar Threads

scaling image
By OmerHalit in forum Advanced JavaReplies: 3Last Post: 05072010, 01:30 AM 
how to use javax.media.jai.Interpolation
By MINGxDOG in forum Java 2DReplies: 0Last Post: 11162009, 08:01 PM 
Scaling a JPEG
By ScottVal in forum Advanced JavaReplies: 5Last Post: 03212009, 08:47 PM 
Scalingache and mouse dragging
By willemjav in forum Java AppletsReplies: 19Last Post: 07192008, 12:17 AM 
scaling images
By willemjav in forum Java AppletsReplies: 7Last Post: 06192008, 09:54 AM
Bookmarks