Sorry, this content is no longer available

Ask Your Question
0

How to divide a mat matrix by integer

asked Oct 28 '14

Deepak Kumar gravatar image

updated Oct 28 '14

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 !!

Preview: (hide)

Comments

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

berak gravatar imageberak (Oct 28 '14)edit

it show error

Deepak Kumar gravatar imageDeepak Kumar (Oct 28 '14)edit

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

berak gravatar imageberak (Oct 28 '14)edit

what is 10101 button in keybord

Deepak Kumar gravatar imageDeepak Kumar (Oct 28 '14)edit

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

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

berak gravatar imageberak (Oct 28 '14)edit

1 answer

Sort by » oldest newest most voted
2

answered Oct 28 '14

berak gravatar image

updated Oct 28 '14

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.
Preview: (hide)

Comments

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

Deepak Kumar gravatar imageDeepak Kumar (Oct 28 '14)edit

Question Tools

Stats

Asked: Oct 28 '14

Seen: 5,489 times

Last updated: Oct 28 '14