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

Deadly 提交于 2019-12-23 09:57:29

问题


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?


回答1:


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.




回答2:


It is probably due to different affine transformations from the output pixels to the input pixels. Check this post on 'blinear' counter-intuition example. Besides, in another post, a hypothesis on the interpolation domain ([1,n] vs [0,n]) is verified on Mathematica, also with 'bilinear' method. So I guess it is the similar reason that causes such a difference between Matlab and OpenCV.



来源:https://stackoverflow.com/questions/22092744/what-is-the-difference-between-opencvs-and-matlabs-bicubic-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!