简体   繁体   中英

Matrix operations in openCV

I'm using OpenCV in C as a image processing tool in my latest project for performance reasons . While using Open CV , I found out that Open CV has less library support for matrix operations . 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. 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. 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(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).

cv::reduce() also performs sum and average. 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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