Ask Your Question
1

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

asked 2019-11-27 10:46:52 -0600

Naman Gupta gravatar image

updated 2019-11-27 11:07:15 -0600

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2019-11-27 10:53:27 -0600

berak gravatar image

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;
edit flag offensive delete link more

Comments

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

Naman Gupta gravatar imageNaman Gupta ( 2019-11-27 10:57:50 -0600 )edit

then you should edit your question, and add resp. code.

berak gravatar imageberak ( 2019-11-27 10:59:30 -0600 )edit
2

Edited Please check

Naman Gupta gravatar imageNaman Gupta ( 2019-11-27 11:06:49 -0600 )edit

your python code is broken ;)

(you need float32 , not int64 for dtype)

berak gravatar imageberak ( 2019-11-27 11:11:59 -0600 )edit
2

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

Naman Gupta gravatar imageNaman Gupta ( 2019-11-27 11:18:06 -0600 )edit
1

read docs

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

berak gravatar imageberak ( 2019-11-27 11:29:48 -0600 )edit

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

berak gravatar imageberak ( 2019-11-27 11:54:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-27 10:46:52 -0600

Seen: 963 times

Last updated: Nov 27 '19