Ask Your Question

cv::resize() incorrect interpolation with INTER_NEAREST

asked 2015-08-13 04:05:57 -0600

foundry gravatar image

updated 2015-08-13 14:53:53 -0600

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


  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?

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.

edit retag flag offensive close merge delete


I haven't got any problem using opencv 3.0 with your program and this one too :

Mat mire = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/14394562979689909.png",CV_LOAD_IMAGE_GRAYSCALE);
Mat mire2;

 resize(mire, mire2,  Size(256,256),0,0,INTER_NEAREST);
imshow("resize", mire2);

but you must use parameter 6 for interpolation choice

LBerger gravatar imageLBerger ( 2015-08-13 04:34:14 -0600 )edit

@LBerger@StevenPuttemans IMHO the source image intended on the question must be as below

Mat image(2,2,0);<uchar>(0,1)=255;<uchar>(1,0)=255;
sturkmen gravatar imagesturkmen ( 2015-08-13 08:04:35 -0600 )edit

@sturkmen I'm sorry but I haven't got result :

Mat image(2,2,CV_8UC1),image2;<uchar>(0,0)=0;<uchar>(1,1)=0;<uchar>(0,1)=255;<uchar>(1,0)=255;
 resize(image, image2,  Size(256,256),0,0,INTER_NEAREST);
imshow("resize", image2);
LBerger gravatar imageLBerger ( 2015-08-13 08:12:14 -0600 )edit

imho expected result is not realistic for 2x2 pixel image. i tried to resize it with photoshop. almost same result.

image description

sturkmen gravatar imagesturkmen ( 2015-08-13 08:22:16 -0600 )edit

@sturkmen - in photoshop, you get the expected result if you set the "Resample Image" param to "Nearest Neighbour". In any case, thanks to the answers and comments here, I have found the problem - not calling with sufficient paramenters.

foundry gravatar imagefoundry ( 2015-08-13 14:36:03 -0600 )edit

@foundry I have learned something new. thank you.

sturkmen gravatar imagesturkmen ( 2015-08-13 15:09:32 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2015-08-13 08:31:34 -0600

LBerger gravatar image

updated 2015-08-13 08:32:01 -0600

That's not really an answer bu with this code I have got your result :<uchar>(0,0)=0;<uchar>(1,1)=0;<uchar>(0,1)=255;<uchar>(1,0)=255;
 resize(image, image2,  Size(256,256),0,0,INTER_LINEAR);
imshow("resize", image2);

May be you should check carrefully your parameter

image description

edit flag offensive delete link more


Thanks, as you suggested I checked my parameters and found them lacking. I had omitted the fx and fy params, so INTER_NEAREST was being interpreted as fx=0fy was defaulting to 0 and interpolation was defaulting to INTER_LINEAR as you show here. Correct call is resize(_inputImage, outImage, Size(256,256),0,0,INTER_NEAREST);

foundry gravatar imagefoundry ( 2015-08-13 14:40:20 -0600 )edit

answered 2015-08-13 05:56:13 -0600

updated 2015-08-13 05:57:09 -0600

Like stated by @LBerger, if I apply this to the input image you give:

#include <iostream>

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
    Mat image = imread("/home/spu/Desktop/test.png");
    Mat outImage;
    resize(image, outImage,  Size(256,256),INTER_NEAREST);

    imshow("orig", image);
    imshow("resized", outImage);


    return 0;

Then this is the output result and it is thus working perfectly fine. What is your configuration?

image description

My guess, you are using OpenCV2.4.x with OpenCV3.0 flags? This code above is on OpenCV3.x ...

edit flag offensive delete link more


You could be right. On OpenCV 2.4.11 - and I was missing two params. The correct call is resize(_inputImage, outImage, Size(256,256),0,0,INTER_NEAREST). Looking at the OpenCV 3 docs, they seem to require the same params?

foundry gravatar imagefoundry ( 2015-08-13 14:45:15 -0600 )edit

Now you say it, it should indeed be with two extra parameters. But this one worked... probably just giving a small offset to the code.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-14 03:21:21 -0600 )edit

Also due to using your input image presented here, the difference is not noticable, and thus I did not see the problem with the different methods, but it is if you make the matrix like @LBerger does.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-14 03:24:07 -0600 )edit

Question Tools

1 follower


Asked: 2015-08-13 04:05:57 -0600

Seen: 21,438 times

Last updated: Aug 13 '15