What is the difference between opencv and matlab bicubic?

When using opencv 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 imresize

 I = imread('fname.png'); J = imresize(I,2, 'Antialiasing', false, 'Method', 'bicubic'); imwrite(J,'scaled_matlab.png') 

and comparison with imagemagick is compared 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?

+3
source share
2 answers

From Matlab Document :

'bicubic'

Bicubic interpolation (default); the output pixel value is the weighted average of the pixels in the immediate vicinity of 4 by 4.

And from the OpenCV doc :

INTER_CUBIC - bicubic interpolation over a neighborhood of 4x4 pixels

Thus, the only explanation for this is that a weighting strategy is used to obtain the average value.

From the source of Matlab imresize.m you can find that the kernel constant A (see Bicubic interpolation on Wikipedia ) is set to -0.5 , while in OpenCV it is set to -0.75 (see imgproc / src / imgwarp.cpp , function interpolateCubic () on github for example ).

This gives various forms of kernel for convolution: difference between Matlab (a = -0.5) and OpenCV (a = -0.75) Kernels for bicubic interpolation

This way you end up with slightly different results in the final interpolated image; usually more ringtones and overshoot for OpenCV, but also sharper edges and better PSNR compared to the "true" high definition base image.

+4
source

This is likely due to various affine transformations from output pixels to input pixels. Check out this post on the example of 'blinear' counter-intuition. In addition, in another post , the interpolation domain hypothesis ( [1,n] vs [0,n] ) is tested on Mathematica, also using the 'bilinear' method. Therefore, I think that a similar reason causes such a difference between Matlab and OpenCV.

+1
source

Source: https://habr.com/ru/post/1206540/


All Articles