Ask Your Question
0

How to divide a mat matrix by integer

asked 2014-10-28 04:37:15 -0600

Deepak Kumar gravatar image

updated 2014-10-28 05:06:55 -0600

berak gravatar image

hi, i want to divide a mat variable of 5x5 ones matrix by integer 25 in android opencv. i have done this in c++. the code is as follows :-

c++ code :-

float kernel[5][5] = {{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1}};
Mat ker = Mat(5, 5, CV_32F, &kernel); // float data == CV_32F Mat
ker=ker/25;

android code :-

Mat kernel = new MatOfInt(
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1,
                1, 1, 1, 1, 1
                );

        Core.divide(25, kernel, kernel);

both of them gives different result. as i want result achieved from c++ code.

thanks !!

edit retag flag offensive close merge delete

Comments

now, what happens, if you divide an integer by 25 ?

berak gravatar imageberak ( 2014-10-28 04:40:44 -0600 )edit

it show error

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-28 04:48:50 -0600 )edit

please learn to format the code in the questions (mark it with the mouse, and press the 10101 button)

berak gravatar imageberak ( 2014-10-28 04:52:57 -0600 )edit

what is 10101 button in keybord

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-28 07:34:42 -0600 )edit

when you make a question, / edit it, you have some buttons there ..

(on this webpage, not on your keyboard, sorry)

berak gravatar imageberak ( 2014-10-28 08:02:02 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-10-28 04:50:36 -0600

berak gravatar image

updated 2014-10-28 04:57:07 -0600

what you want is simply:

Mat kernel = new Mat( 5,5, CvType.CV_32F, new Scalar(1.0/25));

or, more complicated:

Mat kernel = Mat.ones(5,5, CvType.CV_32F);
Core.divide(1.0/25, kernel, kernel);

there's a couple of things wrong with your previous approach:

  • Core.divide takes a scale factor, so it should be: 1.0/25, not 25 (yea, call it a misnomer)
  • dividing integers by something larger will lead to truncation, 1.0/25==0.0399, 1/25==0 !
  • a MatOfSomething is a 1d array, what you wanted was a 5x5 2d Matrix.
edit flag offensive delete link more

Comments

yes it works........thanks

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-28 06:58:54 -0600 )edit

Question Tools

Stats

Asked: 2014-10-28 04:37:15 -0600

Seen: 5,180 times

Last updated: Oct 28 '14