# I – Nearest Neighbour Interpolation

Nearest neighbour interpolation is the simplest approach to interpolation. Rather than calculate an average value by some weighting criteria or generate an intermediate value based on complicated rules, this method simply determines the “nearest” neighbouring pixel, and assumes the intensity value of it.

First, let’s consider this in a 1-dimensional case. Observe the plot below.

Let’s say that we wanted to insert more data points between the points x1 (=2) and x2 (=3), that approximate the values between them, which range between f(x1) (=4) and f(x2) (=6). Using nearest neighbour interpolation, our result would look like:

We can see above that for each data point, xi, between our original data points, x1 and x2, we assign them a value f(xi) based on which of the original data points was closer along the horizontal axis.

Now, extending this to 2D, assume that we want to re-size a tiny, 2×2 pixel image, X, as shown below, so that it “fits” in the larger 9×9 image grid, Y, to the right of it.

As shown above, when we resize by a non-integral factor (as outlined in the beginnging of this section on interpolation) pixels cannot simply be cloned by column/row – we need to interpolate them. The squares (representing pixels) forming a vertical and horizontal line through the rightmost image, for example, cannot contain different color values. They can only contain a single color value.

To visualize nearest neighbour interpolation, consider the diagram below. The data points in the set X represent pixels from the original source image, while the data points in the set Y represent pixels in our target output image.

So, for each pixel in the output image Y, we must calculate the nearest neighbouring pixel in our source image X. Furthermore, we should only need to rely on 4 specific data points: X(A,B), X(A+1,B), X(A,B+1), and X(A+1,B+1). We can handle this decision very quickly with a few IF statements, as outlined in the pseudocode below.

This is very straightforward, but how do we properly index our images when we are dealing with an image larger than 2×2 pixels? We need a transformation that will handle this. Consider a small image which is ‘m’ pixels wide by ‘n’ pixels high, which we want to re-size to ‘p’ pixels wide by ‘q’ pixels high, assuming that p>m and q>n. Now, we need two scaling constants:

• s1 = p/m
• s2 = q/n

Now, we simply loop through all the pixels in the target/output image, addressing the source pixels to copy from by scaling our control variables by s1 and s2, and rounding the resulting scaled index values. The following example shows this in action on a 2×2 pixel “checkerboard” pattern image.

As shown in the images below, the 2×2 checkerboard image was upsampled to a 640×480 pixel image without any changes at all. Furthermore, due to the simplicity of this algorithm, the operation takes very little time to complete. Note the units on the axes for the images below. They look the same, but the one on the left is actually MUCH smaller than the one on the right.

The major drawback to this algorithm is that, despite its speed and simplicity, it tends to generate images of poor quality. Although the checkerboard pattern was upsampled flawlessly, images such as photographs come out “blocky”, though with little to no noticable loss of sharpness, as demonstrated below. The key point here is that this is a simple and fast algorithm. Included below are two more examples that demonstrate the drawbacks of this algorithm.

Now, after applying the nearest neighbour algorithm, we get the following results:

As we can see in the results above (you can click on it to get a better view) the results are of poor quality. Although the sharpness of the original image is retained, we notice how the image on the left of an ‘X’ has “jaggies” (ie: jagged edges) and the image on the right looks pixelated and distorted. This is referred to as aliasing, and there are several ways to deal with it. Two of the most straightforward ways are using a better interpolation method, as covered on the proceeding subsection on interpolation, or the use of spatial domain image filtering, which is covered in the sections on filtering. Finally, included below is the code used to generate the above results, along with the nearest neighbour algorithm MATLAB code rewritten as a MATLAB M-function for convenience.

# Sample Code:

## 7 thoughts on “I – Nearest Neighbour Interpolation”

1. Igor on said:

Hi. I’m trying to implement my own nearest neighbour resizing algorithm. Your article is really helpfull. I just don’t understand why are you substracting 1 from j and k in your code, although in text you are only dividing p/m and q/n. This seems more logical to me, but I do have problems with my code, because I’m getting coordinates (0,0) in my code, which are of course invalid.

• Hi there.

The subtracting of (1) from J and K is basically to deal with how arrays are indexed in MATLAB. eg: In C/C++, an array of ‘N’ integers is addressed on [0,N-1], while in MATLAB, it is addressed on [1,N]. If you don’t include this piece of code, you will attempt to access non-existent array members.

Cheers!

2. Igor on said:

I’m very grateful for your answer. But I just can’t see how substraction of j and k is related to array indexing – we are actually looking for ratio in that equation, and your technique always seems to return a bit too big ratio. I also don’t understand why are you using dot operator in front of division operator? I’m real beginner in matlab and I’m trying to relearn math I forgot many years ago, so I might be missing something 🙂

I found a working solution here – in second post:
http://stackoverflow.com/questions/1550878/nearest-neighbor-interpolation-algorithm-in-matlab
This seems to avoid access to nonexistent pixels and it seems to be cleaner, easier to understand solution:

Ratio in this case is always correct. Because we substract 0.5 from j and add 0.5back after division, we never get any nonexisting coordinates. This seems a cleaner solution to me, but it might be wrong in some way.
x_output = round((j-0.5)/resizing_factor+0.5);

What do you think?

Thanks.

• I think the best way to see why I’m using the subtraction of 1 for my scaling coefficients would be to just remove the semi-colon at the end of a few lines of my code and let MATLAB display the results while generating them. It should should how this effectively scales the image coordinates so the array indexes are never out of bounds.

3. Nastaran on said:

Hi,

Your code is very neat and easy to understand. However, it is only useful when we want to produce an output image which is larger than input image. How does your code change if we want to produce an output image which is smaller than input image.

Consider the following example:

Input image resolution: 256×256
We want to have the output image with 1024×1024 resolution.

4. Nathan on said: