Bilinear Interpolation

Started by
1 comment, last by Mordoch Bob 22 years ago
Ok, so here is my bilinear interpolation macro. This works, but I''m putting it here just to enlighten those who don''t know how the algorithm goes. #define lerp(a,l,h) (l + a*(h-l)) #define bilin(x,y,a,b,c,d) lerp(y,lerp(x,a,b),lerp(x,c,d)) To inperpolate between four points, the code would go something like this: bilin(xpos,ypos,a[x][y],a[x+1][y],a[x][y+1],a[x+1][y+1]) However, this presents a problem when trying to access the outermost points of the data, because it causes an attempt to access non-existent data. However, all the ways that I''ve tried to get around this don''t really work, and while I''m sure that there''s a standard solution to this, I can''t see it. If anyone can enlighten me, I''d be happy to know exactly what the fix is.
_________________________________________________________________________________The wind shear alone from a pink golfball can take the head off a 90-pound midget from 300 yards.-Six String Samurai
Advertisement
There''s plenty of ways you can do it. You could repeat the last value at the edges, or you can wrap back around to the other side. They''re probably the easiest ways, it just depends on your dataset as to which gives the best results. The other way is to define your data area to be 1 bigger than it needs to be...

codeka.com - Just click it.
Since this is a sort of "for those of you who didn''t know" thread, there are also higher quality forms of interpolation.

The first is bicubic, which is essentially the same as bilinear, except it takes the values of the pixels two away as well.

The second is sinous, which requires the same data as bilinear, but interpolates according to a sinous curve instead of a straight line.

The third probably has a name because it''s probably been done before, but I don''t know what it is because reinvented it on my own! It''s a weighted average of the four surrounding points depending on distance:

  //This has LOTS of room for optimization, which I''ll leave//as an exercise for the reader!!inline float square(float x){      return x*x;}float myInterpolate(float x, float y, float ** map){      int topLeftX = (int)x;      int topLeftY = (int)y;      float distA = sqrtf(square(x-topLeftX)+(y-topLeftY)),            distB = sqrtf(square(x-topLeftX-1)+(y-topLeftY)),            distC = sqrtf(square(x-topLeftX)+(y-topLeftY-1),            distD = sqrtf(square(x-topLeftX-1)+(y-topLeftY-1)),            total = distA+distB+distC+distD;      return (map[topLeftX][topLeftY]*distA +              map[topLeftX+1][topLeftY]*distB +              map[topLeftX][topLeftY+1]*distC +              map[topLeftX+1][topLeftY+1]*distD) / total;}  

This topic is closed to new replies.

Advertisement