Results 1 to 2 of 2
  1. #1
    Xpectro is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    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

  1. scaling image
    By OmerHalit in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2010, 01:30 AM
  2. how to use javax.media.jai.Interpolation
    By MINGxDOG in forum Java 2D
    Replies: 0
    Last Post: 11-16-2009, 08:01 PM
  3. Scaling a JPEG
    By ScottVal in forum Advanced Java
    Replies: 5
    Last Post: 03-21-2009, 08:47 PM
  4. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM
  5. scaling images
    By willemjav in forum Java Applets
    Replies: 7
    Last Post: 06-19-2008, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •