can we create a 5*5 kernel np array in c++

I have made a 5*5 kernel using np array in python. how should I create it in c++?

kernel_sharpen_3 = np.array([[-1, -1, -1, -1, -1],
[-1, 2, 2, 2, -1],
[-1, 2, 2, 2, -1],
[-1, 2, 2, 2, -1],
[-1, -1, -1, -1, -1]]) / 8
rescaled_image = cv2.filter2D(rescaled_image, -1, kernel_sharpen_3)


Edit: Python output

C++ code:

float k[] = { -1, -1, -1, -1, -1,
-1, 2, 2, 2, -1,
-1, 2, 2, 2, -1,
-1, 2, 2, 2, -1,
-1, -1, -1, -1, -1 };

Mat kernel_sharp(5, 5, CV_32F, k);

kernel /= 8;

filter2D(rescaled_image, rescaled_image, -1,kernel_sharp);


C++ Output:

edit retag close merge delete

Sort by ยป oldest newest most voted

something like this:

float kdata[] = { -1, -1, -1, -1, -1,
-1, 2, 2, 2, -1,
-1, 2, 2, 2, -1,
-1, 2, 2, 2, -1,
-1, -1, -1, -1, -1};

Mat kernel(5, 5, CV_32F, kdata);

kernel /= 8;

more

Tried this already but output comes different in python and c++

( 2019-11-27 10:57:50 -0500 )edit

( 2019-11-27 10:59:30 -0500 )edit
2

( 2019-11-27 11:06:49 -0500 )edit

your python code is broken ;)

(you need float32 , not int64 for dtype)

( 2019-11-27 11:11:59 -0500 )edit
2

Please, can I ask how & from where should I learn these basics?

( 2019-11-27 11:18:06 -0500 )edit
1

and apply common sense: int64(2) / 8 == ?

( 2019-11-27 11:29:48 -0500 )edit

np.array([.....], np.float32) / 8

( 2019-11-27 11:54:56 -0500 )edit