I have the following MATLAB code which I want to transport into C++
Assume Gr
is 2d matrix and 1/newscale == 0.5
Gr = imresize(Gr, 1 / newScale);
B = imresize(A, scale) returns image B that is scale times the size of A. The input image A can be a grayscale, RGB, or binary image. If scale is between 0 and 1.0, B is smaller than A. If scale is greater than 1.0, B is larger than A.
So this means I will get a 2D matrix == matrix_width/2 and matrix_height/2
How do I calculate the values? The default according to the docs are coming from cubic interpolation for nearest 4X4.
I can't find a sample code for C++ that does the same. Can you please provide a link to such code?
I also found this OpenCV function, resize
.
Does it do the same as the MATLAB one?
In OpenCV, the call would be:
You might then have to do some smoothing/blurring to emulate the anti-aliasing which MATLAB also performs by default (see @chappjc's answer)
Yes, just be aware that MATLAB's
imresize
has anti-aliasing enabled by default:vs. what you would get with
cv::resize()
, which does not have anti-aliasing:And as Amro mentioned, the default in MATLAB is
bicubic
, so be sure to specify.Bilinear
No code modifications are necessary to get matching results with bilinear interpolation.
Example OpenCV snippet:
Output (OpenCV)
MATLAB
Note that the results are the same with anti-aliasing turned off.
Bicubic Difference
However, between
'bicubic'
andINTER_CUBIC
, the results are different on account of the weighting scheme! See here for details on the mathematical difference. The issue is in theinterpolateCubic()
function that computes the cubic interpolant's coefficients, where a constant ofa = -0.75
is used rather thana = -0.5
like in MATLAB. However, if you edit imgwarp.cpp and change the code :to:
and rebuild OpenCV (tip: disable CUDA and the gpu module for short compile time), then you get the same results:
MATLAB
OpenCV
More about cubic HERE.