Different behavior of resize function with python and C++ versions

Hi, I am new to the forum and need some help.
I found that the cv2.resize function in python generate different results when applied to the same image.
For example, I tried to upsample a matrix by a factor of 2 using bicubic interpolation. my python code is like below:

import cv2
import numpy as np

im = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13, 14, 15, 16]]).astype(np.float32)
print('original:')
print(im)
im_1 = cv2.resize(im, (8, 8), interpolation=cv2.INTER_CUBIC)
print('resize x2:')
for j in range(8):
    for k in range(8):
        print('%.3f\t'%im_1[j,k],end="")
    print("")

and the output is:

original:
[[ 1.  2.  3.  4.]
 [ 5.  6.  7.  8.]
 [ 9. 10. 11. 12.]
 [13. 14. 15. 16.]]
resize x2:
0.473	0.770	1.246	1.875	2.281	2.910	3.387	3.684	
1.660	1.957	2.434	3.062	3.469	4.098	4.574	4.871	
3.566	3.863	4.340	4.969	5.375	6.004	6.480	6.777	
6.082	6.379	6.855	7.484	7.891	8.520	8.996	9.293	
7.707	8.004	8.480	9.109	9.516	10.145	10.621	10.918	
10.223	10.520	10.996	11.625	12.031	12.660	13.137	13.434	
12.129	12.426	12.902	13.531	13.938	14.566	15.043	15.340	
13.316	13.613	14.090	14.719	15.125	15.754	16.230	16.527	

Process finished with exit code 0

And below is my C code with does the same thing:

#include<opencv2/opencv.hpp>
#include<iostream>

// Namespace to nullify use of cv::function(); syntax
using namespace std;
using namespace cv;

int main()
{
	float I[4][4] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
	Mat image(4,4,CV_32F, I);
	
	Mat resized_up;
	resize(image, resized_up, Size(8,8), INTER_CUBIC);

	for (int j = 0; j <= 7; j++) {
		for (int k = 0; k <= 7; k++) {
			printf("%.2f\t", resized_up.at<float>(j,k));
		}
		printf("\n");
	}
	return 0;
}

but it gives me

It seems that the python version align the original pixels with the centers of correspoinding upscaled pixels, while the C version aligns the corner pixels. I digged a bit with Google and found that the python version should be the right behavior of opencv resize function.
I am using both opencv 3.4 for python and C platforms. And the behavior of python cv2.resize function does not change between different opencv versions.
Could anyone give me a hint? Did I do something wrong?
Thank you!

there is no named parameter in C++11. Use :
resize(image, resized_up, Size(8, 8),0,0,INTER_CUBIC);

resize doc

1 Like

the fourth parameter is not the interpolation parameter. INTER_CUBIC has the value 2 here, and you passed that for the fx argument, which the resize() call ignored, because you supplied a non-empty dsize argument already.

pay attention to the function signature. you must provide all preceding arguments, as Laurent showed you.

1 Like

Thank you so much! I got it.

Thank you very much for the information! My problem is fixed.