Reputation: 39
I'm currently working on a project which requires me to read images and enlarge them.
I'm currently using the nearest neighbour algorithm to enlarge my image, but i'm trying to use bilinear interpolation and finding it hard to implement.
Here is my current NN algorithm:
public void doThumbnailResize() {
resizedImage = new BufferedImage(55, 55, BufferedImage.TYPE_3BYTE_BGR);
int h = resizedImage.getHeight(), h2 = image1.getHeight();
int w = resizedImage.getWidth(), w2 = image1.getWidth();
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
float y = j * ((float) h2 / (float) h);
float x = i * ((float) w2 / (float) w);
resizedImage.setRGB(i, j, image1.getRGB((int) x, (int) y));
}
}
image_icon.setIcon(new ImageIcon(resizedImage));
}
I'm finding it hard to find material online and was wondering if somebody could point me in the right direction and maybe some pseudocode.
Thanks in advance.
Upvotes: 0
Views: 1638
Reputation: 88707
Bilinear interpolation basically works on interpolating a square of 4 pixels. You'd first look for the nearest neighbor, i.e. which source pixel a target pixel maps to, and the location relative to that pixel. Then you create a box that is one target-pixel-size wide and high and check which pixels that box intersects (would be up to 4) and calculate the relation of that overlap to the box size. That percentage would then be applied to the color and be added to the final color.
Example:
Assume we have a 9x9 image like this (each character means a pixel):
ABC
DEF
GHI
Now we want to enlarge that to 4x4 and calculate the interpolation of your first pixel (0/0):
Now for a second pixel (1/0):
Let's consider another pixel, this time (1/1):
Upvotes: 1