Nearest Neighbour to Bilinear Interpolation

≡放荡痞女 提交于 2021-01-08 02:10:45

问题


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.


回答1:


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):

  • the relative size of the target pixel is (3/4,3/4), i.e. each target pixel spans 0.75 the width and height of a source pixel
  • pixel A spans texture coordinates (0,0) to (0.333,0.333)
  • our first target pixel's center would be at (0.125,0.125) (1/width * 0.5, 1/height * 0.5) and thus the target box would span coordinates (0.0, 0.0) to (0.25,0.25)
  • as you can see, our first target pixel completely lies within source pixel A and thus get's A's color.

Now for a second pixel (1/0):

  • the second target pixel's center would be at (0.375,0.125) and thus spans the coordinates (0.25,0.0) to (0.5,0.25)
  • as you can see this will overlap source pixels A, B, D and E
  • the area of our target box is 0.25 * 0.25 = 0.0625
  • the area of the overlap with A is (0.333-0.25) * (0.25 - 0) ~= 0.02075
  • thus our around 33% of our target pixel's box overlap with A
  • the overlap for B would be ((0.5-0333) * (0.25 - 0)) / 0.0625 ~= 67%
  • the color of our target pixel would thus be 33% the color of A + 67% the color of B

Let's consider another pixel, this time (1/1):

  • the overlap for A would be: ((0.333 - 0.25) * (0.333 - 0.25)) / 0.0625 ~= 11%
  • the overlap for B would be: ((0.5-0.333) * (0.333 - 0.25)) / 0.0625 ~= 22%
  • the overlap for D would be: ((0.333 - 0.25) * (0.5-0.333) ) / 0.0625 ~= 22%
  • the overlap for E would be: ((0.5-0.333) * (0.5-0.333) ) / 0.0625 ~= 45%
  • target pixel (1/1)'s color would thus be 11% A + 22% B + 22% D + 45% E


来源:https://stackoverflow.com/questions/35601835/nearest-neighbour-to-bilinear-interpolation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!