Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between opencv's and matlab's bicubic algorithm?

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?

like image 881
dodol Avatar asked Feb 28 '14 10:02

dodol


1 Answers

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 : difference between Matlab (a=-0.5) and OpenCV (a=-0.75) Kernels for bicubic interpolation

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.

like image 69
herohuyongtao Avatar answered Sep 30 '22 14:09

herohuyongtao