例如,我们在Matlab中使用rdivide表达式:
B = bsxfun(@rdivide, A, A(4,:));
我们如何为opencv编写等价表达式?
Opencv具有divide功能,但似乎无法用于不同维度的矩阵:
Mat t1= Mat::ones(2,3,CV_64FC1);
Mat t2= Mat::ones(1,3,CV_64FC1);
Mat dst;
divide(t1,t2,dst);
这不起作用,所以我们需要将一行复制到矩阵以匹配t1的维度或使用除以1行的循环。
我的opencv解决方案(A modified inplace):
for(int i=0;i<A.rows;++i)
{
divide(A.row(i),A.row(3),A.row(i));
}
有没有更简单的方法?
答案 0 :(得分:7)
答案 1 :(得分:4)
除@sgarizvi解决方案外,您可能会发现Matlab rdivide
的这个包装有用:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat rdivide(const Mat& A, const Mat& B)
{
int nx = A.cols / B.cols;
int ny = A.rows / B.rows;
return A / repeat(B, ny, nx);
}
Mat rdivide(const Mat& A, double d)
{
return A / d;
}
int main()
{
Mat1f A = (Mat1f(3, 5) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
Mat B = rdivide(A, A.row(2)); // Divide by matrix, works also for cols: e.g. A.col(2)
Mat C = rdivide(A, 2); // Divide by scalar
cout << "A: " << endl << A << endl << endl;
cout << "B: " << endl << B << endl << endl;
cout << "C: " << endl << C << endl << endl;
return 0;
}