When using opencv's resize
img = cv2.imread('fname.png', 0 )
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
cv2.imwrite('scaled_cv2.png',res)
and matlab's imresize
I = imread('fname.png');
J = imresize(I,2, 'Antialiasing', false, 'Method', 'bicubic');
imwrite(J,'scaled_matlab.png')
and comparing with imagemagick's compare with
compare -metric PSNR fname.png scaled_cv2.png diff_cv2.png
compare -metric PSNR fname.png scaled_matlab.png diff_matlab.png
I get completely different PSNR values What are they doing different?
From Matlab's doc:
'bicubic'
Bicubic interpolation (the default); the output pixel value is a weighted average of pixels in the nearest 4-by-4 neighborhood
And from OpenCV's doc:
INTER_CUBIC
- a bicubic interpolation over 4x4 pixel neighborhood
So the only explanation for this is that they use different weighting strategy to get the average value.
From Matlab imresize.m source, you can find that the kernel constant A (see Bicubic interpolation on Wikipedia) is set to -0.5, whereas it is set to -0.75 in OpenCV (see imgproc/src/imgwarp.cpp, function interpolateCubic() on github for instance).
This gives different kernel shapes for the convolution :
Therefore, you will end with slightly different results in the final interpolated image; typically more ringing artifacts and overshoot for OpenCV, but also sharper edges and a better PSNR compared with the "true" underlying high-resolution image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With