ha9u63a7
ha9u63a7

Reputation: 6862

Matrix Multiplication in OpenCV - Combining cv::Mat and cv::Scalar Objects

I am trying to implement different cross-correlation algorithms in C++ using some OpenCV library functions. At some point, I need to take the sum of the rows and columns of my matrix. I am using cv::Mat as my matrix class and I am comfortable as a beginner to do some basic matrix operations in with cv::Mat. My only problem is I am experienced in MATLAB, which is really good in this kind of minor things. So, I will put my requirements in terms of MATLAB code (apologies in advance if someone doesn't understand MATLAB). What I am after is this:

IN MATLAB, For example

gg = [1 1 1; 2 2 2; 3 3 3; 4 4 4]; % is a 4x3 matrix

sumGrows = (gg,1); % Sum of all rows - will be [10 10 10]; 1 means DIMENSION=ROWS
sumGcols = (gg,2); % Sum of all columns - [3; 6; 9; 12]; 2 means DIMENSION=COLUMNS

FYI, In MATLAB, a comment is noted with a %

How can I do that in OpenCV? I know that there is a function called "sum" in OpenCV, but that doesn't return a cv::Mat object. Do I need to do some static/dynamic casting then? AFAIK, the return type of sum() is Scalar. Please point me to the right direction.

FYI, I am not a student! I am professional (used to be in Java-based Development and Systems Engineering). But currently taking over a project where I have to do C++ based implementation.

TIA

Upvotes: 1

Views: 1651

Answers (1)

Andrey Kamaev
Andrey Kamaev

Reputation: 30152

There is a cv::reduce for summing rows or cols.

See also this question: Column sum of Opencv Matrix elements

Upvotes: 2

Related Questions