## resampling 2D-arrays (images), how to ?

### resampling 2D-arrays (images), how to ?

I am looking for some pseudo-code or implementation
of a function, that takes a 2D-array (image defined by values
at equidistand points) and resamples the data for another grid
of equidistant points by applying an appropriate filter (or some-
thing else). The two grids will have different unit-lengths.

Any help or pointers are highly appreciated.

Malte Lance.

### resampling 2D-arrays (images), how to ?

> I am looking for some pseudo-code or implementation
> of a function, that takes a 2D-array (image defined by values
> at equidistand points) and resamples the data for another grid
> of equidistant points by applying an appropriate filter (or some-
> thing else). The two grids will have different unit-lengths.

> Any help or pointers are highly appreciated.

You could try http://almond.srv.cs.cmu.edu/afs/cs/user/ph/www/heckbert.html
which is Paul Heckbert's web page. On there somewhere is a link along the
lines of: "C code to zoom raster images up or down, with nice filtering"
which you may be able to adapt to do what you want.

> Malte Lance.

Regards,
--

+- Stuart MacKinnon ---------------------------------------------------------+
|  Senior Programmer at eyeOn Software             http://www.eyeonline.com  |

### resampling 2D-arrays (images), how to ?

An algorithm I use for scaling bitmaps to any arbitrary factor goes like
this:

1) Create a blank destination bitmap of size (source_width * scale_factor,
source_height * scale_factor).

2) Loop through every pixel in the destination image.

3) Calculate where that pixel would be in the source image (dest_x /
scale_factor, dest_y / scale_factor) using floating-point math.

4) The result will be a fractional coordinate somewhere between for source
pixels, like this:

A   B
x
C   D

(it may, by chance, land right on a source pixel, but the math will still
come out OK)

5) Perform a linear interpolation of pixels A & B and C & D horizontally,
like this:

AB = x_delta * (B - A) + A
CD = x_delta * (D - C) + C
(x_delta is the fractional portion of the source X value:  source_x -
INT(source_x))

6) Perform a linear interpolation of AB & CD vertically, like this:

result = y_delta * (CD - AB) + AB

7) The resulting pixel value is assigned to the destination image.

This algorithm provides for a high-quality scaled version of the original
image.  It is easy to implement scale-to-gray with this algorithm also.

### resampling 2D-arrays (images), how to ?

> 5) Perform a linear interpolation of pixels A & B and C & D horizontally,
> like this:

[snip]

Quote:> 6) Perform a linear interpolation of AB & CD vertically, like this:

> 7) The resulting pixel value is assigned to the destination image.

You're point-sampling; won't work well for image minification (scale < 1).
Look into "Digital Image Warping" by George Wolberg for a survey of proper
prefilters to be used.

I am looking for some pseudo-code or implementation
of a function, that takes a 2D-array (image defined by values
at equidistand points) and resamples the data for another grid
of equidistant points by applying an appropriate filter (or some-
thing else). The two grids will have different unit-lengths.

Any help or pointers are highly appreciated.