java - Bilinear interpolation -


i got code scaling image bilinear interpolation.i know works can't figure out 1 thing if approximated pixel value edge(by edge mean in last row or last column) pixel in input image can gt pixel of coordinate (x+1,y+1) ,this should lead array index out of range error no such error occurs why? code is:

public int[] resizebilineargray(int[] pixels, int w, int h, int w2, int h2) {     int[] temp = new int[w2*h2] ;     int a, b, c, d, x, y, index, gray ;     float x_ratio = ((float)(w-1))/w2 ;     float y_ratio = ((float)(h-1))/h2 ;     float x_diff, y_diff, ya, yb ;     int offset = 0 ;     (int i=0;i<h2;i++) {         (int j=0;j<w2;j++) {             x = (int)(x_ratio * j) ;             y = (int)(y_ratio * i) ;             x_diff = (x_ratio * j) - x ;             y_diff = (y_ratio * i) - y ;             index = y*w+x ;              // range 0 255 bitwise , 0xff             = pixels[index] & 0xff ;             b = pixels[index+1] & 0xff ;             c = pixels[index+w] & 0xff ;             d = pixels[index+w+1] & 0xff ;              // y = a(1-w)(1-h) + b(w)(1-h) + c(h)(1-w) + dwh             gray = (int)(                     a*(1-x_diff)*(1-y_diff) +  b*(x_diff)*(1-y_diff) +                     c*(y_diff)*(1-x_diff)   +  d*(x_diff*y_diff)                     ) ;              temp[offset++] = gray ;                                            }     }     return temp ; } 

the reason x_ratio , y_ratio computed incorrectly.

consider last pixel of last row:

i=h2, j=w2  

then:

x = x_ratio * j = (w-1)/w2 * (w2-1) = (w-1) * (w2-1)/w2 <= w-1 y = y_ratio * = (h-1)/h2 * (h2-1) = (h-1) * (h2-1)/h2 <= h-1  index = y*w+x <= (h-1)*w + (w-1) < w*h 

so index less size of pixels array.


note however, dirty hack, result in inaccurate results, small images.

you should calculate width/height ratio follows:

float x_ratio = ((float)w)/w2; float y_ratio = ((float)h)/h2; 

and create function converts coordinates array index - let's name coord2index. function takes out-of range coordinates account , implements so-called boundary option, simulates there pixels outside image boundary.

common options boundary are:

  • symmetric - pixels outside image bounds computed mirror-reflecting image @ border. best possibility in case.

  • replicate - pixels outside image bounds assumed equal nearest pixel @ border. simplest way.

  • circular - image virtually repeated periodically in directions. used advanced image processing algorithms; not image resizing.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -