Nearest Neighbor Interpolation for Resize Image

If you are not a computer scientist maybe you will think that resize image is an easy task. Just scroll the mouse wheel and all done. But do you know the process behind just “scroll the mouse wheel” ? I am going to discuss the simplest way of Image Scaling(what most people know as resize image). The most simplest way is Nearest Neighbor Interpolation, of course there are some other sophisticated interpolation methods.

Before we discuss about the method, It’s better if you’ve already known about the basic properties of image. If you aren’t , you can imagine images as a matrix. Look the picture below

Image[{{0., 1., 0.}, {1., 0., 1.}, {0., 1., 0.}}]
image matrix

Based on the picture above you can image if you have image with width=3 pixel and height=3 pixel so you have 3×3 matrix. If you have image 512×512 so you have matrix 512×512 (if your image pixel 8bpp or 1byte. I used 8bpp for example just for simple explanation).

Suppose we have 4×4 pixel image and we need to enlarge to be 8×8 pixel. We can imageine like this


the black square on the middle image are empty pixel those we need to put some value. Okay, next is the method. I think you can guess from the name. Nearest-Neighbor, it means the empty value of pixel will be occupied with the value of the nearest pixel. The most right image is the result after the interpolation process done. Okay simple right?

I tried to implement the method with C++. here is only piece of the C++ program

BYTE* resizePixels(BYTE* pixels, int w1, int h1, int w2, int h2) {
	BYTE * temp;
	temp = new BYTE[w2 * h2];
	double x_ratio = w1 / (double)w2;
	double y_ratio = h1 / (double)h2;
	double px, py;
	for (int i = 0; i<h2; i++) {
		for (int j = 0; j<w2; j++) {
			px = floor(j*x_ratio);
			py = floor(i*y_ratio);
			temp[(i*w2) + j] = pixels[(int)((py*w1) + px)];
	return temp;

pixels is the original pixel values. w1 and h1 is the original size. w2 and h2 is the desire size. And the most important thing is temp. Temp is the desire pixel, or in this case the scaling pixel. Look at the picture below, I think you will understand the code. :)

Result Nearest Neighbor

Here is my experiment using LENA (standard image for testing). I try to enlarge 128×128 pixel to 256×256 pixel.

LENA 128

Lena 128 x 128. 8 bpp

LENA 256 x 256

LENA 256 x 256 8 bpp. Enlarge from 128 x 128 using Nearest Neighbor

Hmm.. not bad for manual image resize. FYI, I used C++ without any image lib such as OpenCV or something else. The intent of this article is just to discuss about how do Nearest-Neighbor work. If you have any question feel free to write down on comment.

Download Code and resources here.
References :

6 thoughts on “Nearest Neighbor Interpolation for Resize Image

  1. Zohir

    I wanted to adopt your code in my application cause I need a quick resize to make my resulting image bigger so that I may continue my work on it but I didn’t find a way out if you could help me I’m working on embarcadero rad studio XE5 I’d be very grateful. Thanks

  2. Harry

    hey good work sir , the code is pretty good
    but can you explain me this code :
    temp[(i*w2) + j] = pixels[(int)((py*w1) + px)];

    thanks for replying..

    1. Jan Kristanto Post author

      resizePixels is the function for Nearest Neighbor Interpolation.

      temp[(i*w2) + j] = pixels[(int)((py*w1) + px)];

      temp is a variable that contents new pixel values.
      i = row counter
      j = col counter
      w2 = new width (desire width)
      py = row counter in original image
      px = col counter in original image

      so temp[(i*w2) + j] = pixels[(int)((py*w1) + px)]; just, copy the appropriate pixels from original image to new array (temp). Finally, temp will be the new image.



Leave a Reply

Your email address will not be published. Required fields are marked *