简体   繁体   English

openCV中的矩阵运算

[英]Matrix operations in openCV

I'm using OpenCV in C as a image processing tool in my latest project for performance reasons . 由于性能原因,我在最新项目中使用C语言中的OpenCV作为图像处理工具。 While using Open CV , I found out that Open CV has less library support for matrix operations . 使用Open CV时,我发现Open CV对矩阵运算的库支持较少。 For example if wanted to add a column vector to every column in a matrix , I will have to write a custom function to do this . 例如,如果要向矩阵的每一列添加列向量,则必须编写一个自定义函数来执行此操作。 There are many more operations that seemed trivial in Matlab, missing in Open CV - like norm of each column , column wise min/max etc .In short all Column wise operations on a matrix seems to be missing in Open CV (I would be surprised if I didn't find more).The matrix manipulation support for library is very minimal. 在Matlab中还有很多看似微不足道的操作,在Open CV中缺少类似每列的规范,列明智的最小/最大值等。简而言之,在Open CV中似乎缺少矩阵上所有列明智的操作(我会感到惊讶如果找不到更多内容)。对库的矩阵处理支持非常少。 Is this a design decision for the library or is there some sort of an extension that can help me through this . 这是库的设计决定,还是某种扩展可以帮助我完成此任务。 I believe there must have been others who observed and did something about the lack of support.Any pointers ? 我相信一定会有其他人观察到缺乏支持的情况并有所作为。

OpenCV may not be a complete replacement for MatLab, but its matrix support is still quite good. OpenCV可能无法完全替代MatLab,但其矩阵支持仍然相当不错。 You may find that some of the features you are looking for are there but simply have different names. 您可能会发现您正在寻找的某些功能就在其中,只是名称不同。

For example if wanted to add a column vector to every column in a matrix , I will have to write a custom function to do this. 例如,如果要向矩阵的每一列添加列向量,则必须编写一个自定义函数来执行此操作。

You can do this a few ways; 您可以通过以下几种方法进行操作: probably the easiest is with ranges. 可能最简单的是使用范围。 See below for one solution. 请参阅下面的一种解决方案。

like norm of each column 像每列的规范

Use a matrix range to select each column in a loop: 使用矩阵范围来选择循环中的每一列:

cv::Mat m;

// ...

for (unsigned c = 0; c < m.cols(); c++)
{
    cv::Mat col(m, cv::Range::all(), cv::Range(c, c+1));
    double n = cv::norm(col, NORM_L2);
}

column wise min/max etc 列明智的最小/最大等

The cv::reduce function provides all these sorts of features: cv::reduce函数提供所有这些功能:

cv::reduce(InputArray src, OutputArray dst, int dim, int rtype);
// where rtype = CV_REDUCE_MIN, CV_REDUCE_MAX, etc

In short all Column wise operations on a matrix seems to be missing in Open CV (I would be surprised if I didn't find more). 简而言之,Open CV中似乎缺少矩阵上所有按列的操作(如果找不到更多内容,我会感到惊讶)。

cv::reduce() also performs sum and average. cv::reduce()也执行求和和平均值。 You can choose to perform column-wise or row-wise. 您可以选择按列或按行执行。 If these operations aren't sufficient for your needs, you may actually have to write your own functions. 如果这些操作不足以满足您的需要,则实际上您可能必须编写自己的函数。

The online documentation is pretty good: 联机文档非常好:

The tutorial has more info on memory management and matrices, in particular explaining about ranges and how this can share memory: 本教程提供了有关内存管理和矩阵的更多信息,特别是说明了范围以及如何共享内存:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM