在OpenCV中逐行减去矩阵

时间:2012-03-14 23:59:51

标签: c++ opencv

OpenCV中是否有内置方法 C ++ API 逐行减去两个矩阵

我有以下矩阵:

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.

A中的每一行都必须从B中减去并存储为C中的一行

3 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

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;

答案 1 :(得分:0)

根据OpenCV文档,您可以使用方法subtract()。在你的情况下,它是

subtract(A.row(0), B.row(0), tempMat);

然后将tempMat推送到C或CopyTo。

以下是参考: OpenCV Array Operation

答案 2 :(得分:0)

使用cv::repeat() http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#repeat

cv::Mat A = cv::Mat::ones(10, 2, CV_64F) * 5.0;
cv::Mat B = cv::Mat::ones(1, 2, CV_64F) * 3.0;
cv::Mat BRepeat = cv::repeat(B, A.rows, 1);
cv::Mat C = A - BRepeat;

cout << endl;
cout << "A" << endl << A << endl << endl;
cout << "B" << endl << B << endl << endl;
cout << "BRepeat" << endl << BRepeat << endl << endl;
cout << "C" << endl << C << endl << endl;