garak garak - 28 days ago 7
C++ Question

Subtract Matrices row-wise in OpenCV

Is there any built-in method in OpenCV (C++ API) to subtract two matrices row-wise.

I have the following matrices:

Mat A(10,2, CV_64F);
Mat B(1,2, CV_64F);
Mat C(10,2, CV_64F);

C = A - B;

// B is a 1 x 2 matrix, A is a 10 x 2 matrix, and C is a 10 x 2 matrix.


Each row in A has to be subtracted from B and stored as a row in C

Answer

I solved it the following way:

Mat A(10,2, CV_64F);
Mat B(1,2, CV_64F);
Mat C(0,2, CV_64F);
Mat D(0,2, CV_64F);

for(int i=0;i<A.rows;i++)
{
C=(A.row(i)-B.row(0));
D.push_back(C.row(0));
}

cout<<"A\n"<<A<<endl;
cout<<"B\n"<<B<<endl;
cout<<"C\n"<<C<<endl;
cout<<"D\n"<<D<<endl;
Comments