# How to divide a mat matrix by integer

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 = {{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 close merge delete

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

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

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

Sort by » oldest newest most voted

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.
more

Official site

GitHub

Wiki

Documentation