Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::resize() incorrect interpolation with INTER_NEAREST

I am trying to 'enlarge' pixels - i.e. apply resize() to increase the dimensions of an image with nearest neighbour interpolation. However I am not getting expected results.

Input image (2 x 2 pixels):

image description

Code:

  resize(_inputImage, outImage,  Size(256,256),INTER_NEAREST);
  imshow(_windowName, outImage);

Expected result (256 x 256 pixels):

image description

Actual result (256 x 256 pixels):

image description

What am I doing wrong?

cv::resize() incorrect interpolation with INTER_NEAREST

I am trying to 'enlarge' pixels - i.e. apply resize() to increase the dimensions of an image with nearest neighbour interpolation. However I am not getting expected results.

Input image (2 x 2 pixels):

image description

Code:

  resize(_inputImage, outImage,  Size(256,256),INTER_NEAREST);
  imshow(_windowName, outImage);

Expected result (256 x 256 pixels):

image description

Actual result (256 x 256 pixels):

image description

What am I doing wrong?

_update_
Thanks to the numerous comments and answers, it transpires that I had omitted some params. The correct call is:

      resize(_inputImage, outImage,  Size(256,256),0,0,INTER_NEAREST);

The 4th, 5th and 6th params are optional with defaults. I was omitting #4 (fx) and #5 (fy), so INTER_NEAREST was getting interpreted as fx (int 0), fy was defaulting to 0, and interpolation was defaulting to INTER_LINEAR( bilinear interpolation) which gave the unexpected result.