【转】opencv 矩阵行列求和
转自:https://blog.****.net/lcgwust/article/details/70800177
函数: reduce();
官方文档:
Reduces a matrix to a vector.
C++:
void reduce
(InputArray src, OutputArray dst, int dim, int rtype, int dtype=-1 )¶
Python:
cv2.
reduce
(src, dim, rtype[, dst[, dtype]]) → dst
C:
void cvReduce
(const CvArr* src, CvArr* dst, int dim=-1, int op=CV_REDUCE_SUM)
Python:
cv.
Reduce
(src, dst, dim=-1, op=CV_REDUCE_SUM) → None
Parameters: |
|
---|
The function reduce
reduces the matrix to a vector by treating the matrix rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of a raster image. In case of CV_REDUCE_SUM
and CV_REDUCE_AVG
, the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes.
总结一句话:参数的选择要对应
double a[5][4] =
{
{ 4, 0, 2, 5 },
{ 1, 1, 0, 7 },
{ 0, 5, 2, 0 },
{ 0, 3, 4, 0 },
{ 8, 0, 1, 2 }
};
Mat ma(5, 4, CV_64FC1, a);//如果ma为图像数据的话,须用convertTo(newImage, CV_64FC1, 1/255.0);函数转化
Mat mb(5, 1, CV_64FC1, Scalar(0));
Mat mc(1, 4, CV_64FC1, Scalar(0));
cout << "原矩阵:" << endl;
cout << ma << endl;
reduce(ma, mb, 1, CV_REDUCE_SUM);
cout << "列向量" << endl;
cout << mb << endl;
reduce(ma, mc, 0, CV_REDUCE_SUM);
cout << "行向量" << endl;
cout << mc << endl;