I'm currently working on porting some code from Matlab to C++ and having some difficulty with image resizing. I'm using OpenCV to resize in C++, however the results do not match. I know about the anti-aliasing option in Matlab but in this case, I'm scaling up so Matlab doesn't use that option.
Matlab code:
imresize(a,1.3, 'bilinear')
C++ code:
double scale = 1.3;
cv::Mat a = (cv::Mat_<double>(5, 5) << 0.7430835, 0.2263354, 0.8372651, 0.4305077, 0.0060997,
0.1850839, 0.4359681, 0.6224524, 0.3951009, 0.3335419,
0.5295836, 0.0202813, 0.8555994, 0.3494351, 0.6370482,
0.3154180, 0.4335316, 0.6281235, 0.3844186, 0.9898034,
0.9576555, 0.0212430, 0.3106115, 0.2677145, 0.1495867);
cv::Mat b;
cv::Mat c;
cv::resize(a, b, cv::Size(), scale, scale, CV_INTER_LINEAR);
cv::resize(a, c, cv::Size(ceil(scale * 5), ceil(scale * 5)), 0, 0, CV_INTER_LINEAR);
I noticed that matrix b gave me the wrong dimensions, so in c I specified the dimensions instead of scale factor. Even though the dimensions is correct, it does not provide the same result:
Matlab:
a=
0.7430835 0.2263354 0.8372651 0.4305077 0.0060997
0.1850839 0.4359681 0.6224524 0.3951009 0.3335419
0.5295836 0.0202813 0.8555994 0.3494351 0.6370482
0.3154180 0.4335316 0.6281235 0.3844186 0.9898034
0.9576555 0.0212430 0.3106115 0.2677145 0.1495867
ans =
0.7430835 0.3985848 0.4299787 0.8372651 0.5660935 0.2890383 0.0060997
0.3710838 0.3677549 0.4754126 0.6940566 0.5026210 0.3460670 0.2243945
0.2999171 0.2982429 0.4316599 0.7001681 0.4866420 0.3981562 0.4347107
0.5295836 0.1900488 0.2987207 0.8555994 0.5181565 0.4453061 0.6370482
0.3868066 0.3261232 0.4318373 0.7039488 0.4831546 0.5392444 0.8722183
0.5294972 0.3739004 0.3714968 0.5222862 0.4044402 0.4669219 0.7097312
0.9576555 0.3333805 0.1176992 0.3106115 0.2820135 0.2283386 0.1495867
C++:
a:
0.743084 0.226335 0.837265 0.430508 0.0060997
0.185084 0.435968 0.622452 0.395101 0.333542
0.529584 0.0202813 0.855599 0.349435 0.637048
0.315418 0.433532 0.628123 0.384419 0.989803
0.957655 0.021243 0.310611 0.267715 0.149587
b:
0.743084 0.40521 0.484806 0.759043 0.446152 0.120363
0.378238 0.368538 0.50446 0.641146 0.41849 0.270586
0.330834 0.284585 0.455135 0.654686 0.389062 0.438749
0.488398 0.234284 0.401026 0.724221 0.373689 0.610999
0.323655 0.385105 0.510391 0.588065 0.392835 0.816538
0.784745 0.35811 0.243873 0.377449 0.302864 0.355159
c:
0.743084 0.447799 0.400887 0.837265 0.546724 0.248619 0.0060997
0.424227 0.379597 0.45138 0.714515 0.497201 0.317247 0.19321
0.283512 0.302763 0.423448 0.689066 0.469771 0.398427 0.420258
0.529584 0.238554 0.258944 0.855599 0.494054 0.472698 0.637048
0.376608 0.341666 0.423362 0.693117 0.465479 0.594963 0.889016
0.590663 0.399905 0.324039 0.492047 0.379444 0.460963 0.62971
0.957655 0.422563 0.10392 0.310612 0.279971 0.217088 0.149587
Unfortunately, I can not edit the Matlab code, so all the changes need to be done in C++. Does anyone know how to make OpenCV provide the same result as Matlab, or do I need to create my own function?
Best regards Sondre
Related SO questions:
MATLAB vs C++ vs OpenCV - imresize
why OpenCV cv2.resize gives different answer than MATLAB imresize?