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):
Code:
resize(_inputImage, outImage, Size(256,256),INTER_NEAREST);
imshow(_windowName, outImage);
Expected result (256 x 256 pixels):
Actual result (256 x 256 pixels):
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.
I haven't got any problem using opencv 3.0 with your program and this one too :
but you must use parameter 6 for interpolation choice
@LBerger@StevenPuttemans IMHO the source image intended on the question must be as below
@sturkmen I'm sorry but I haven't got result :
imho expected result is not realistic for 2x2 pixel image. i tried to resize it with photoshop. almost same result.
@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 I have learned something new. thank you.