Ask Your Question
0

Squaring elements of a matrix and summing them

asked 2016-10-16 18:00:48 -0600

Fahad gravatar image

I am new to java and opencv . I have a matrix(destination) and now I want to square every element of it and add all of them together after squaring. how can I do that?

Mat destination = new Mat(footGrayMat.rows(),footGrayMat.cols(),footGrayMat.type());
Mat kernel = new Mat(5,5, CvType.CV_32F);

float[] data = {-1,-3,-4,-3,-1,-3,0,6,0,-3,-4,6,20,6,-4,-3,0,6,0,-3,-1,-3,-4,-3,-1};
kernel.put(0,0,data);

Imgproc.filter2D(footGrayMat, destination, -1, kernel);
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-04-05 09:58:06 -0600

scraph gravatar image

The sum of squared entries is simply the dot-product of a vector with itself.

Scalar s = destination.dot(destination);

OpenCV documentation indicates that this operation, when applied to general matrices, treats them as 1D vectors; i.e., it does what you asked for.

edit flag offensive delete link more
0

answered 2016-10-17 03:43:57 -0600

berak gravatar image

you can use per-element multiplication for the square, like this:

Mat destSquared = destination.mul(destination);

then get the sum

Scalar s = Core.sumElems (destSquared);
double sum = s[0]; // 1st channel
edit flag offensive delete link more

Comments

Yes I need sum of all pixels after squaring them .Can this method( pre element multiplication) be used to take square of each pixel of Mat?

Fahad gravatar imageFahad ( 2016-10-17 06:17:22 -0600 )edit

yes, you can.

berak gravatar imageberak ( 2016-10-17 08:16:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-16 18:00:48 -0600

Seen: 6,127 times

Last updated: Oct 17 '16