Search code examples
c++opencvpass-by-reference

how to pass opencv mat by reference


i have 1 matrix (3channel) -> cv::Mat channels[3];

& 3 matrix (1channel) -> cpu_filter0,cpu_filter90,cpu_filter120

previously I did something like this,

cv::Mat cpu_filter0,cpu_filter90,cpu_filter120
cv::Mat channels[] = {cpu_filter0,cpu_filter90,cpu_filter120}

but now I want to do like this

cv::Mat cpu_filter0,cpu_filter90,cpu_filter120
cv::Mat channels3[3];
channels[0] = &cpu_filter0, channels[1] = &cpu_filter90, channels[0] = &cpu_filter120;

or by,

cv::Mat channels3[] = {&cpu_filter0_0,&cpu_filter120_120,&cpu_filter240_240};

want to know what is the right way to do this??


Solution

  • Are you saying you used cv::Mat channels[3]; to create a multi-channel Matrix? Because that is not what this does, this creates a c-style Array with 3 matrices.

    First, make sure that you know the language well (C++) and how (C-style) arrays work (I found this tutorial, but I haven't read it).

    Then, read the paragraph "Detailed Description" here.

    To sum it up, there are different ways to create a cv::Mat, for example:

    cv::Mat m(10, 10, CV_32FC3);
    

    That creates a 10x10 matrix with 3 channels of 32-Bit floating point numbers. Other values can be found here. Just add "C" for your desired number of channels.

    Now, how to pass them as a reference? Simply as always:

    void my_func(cv::Mat &param) {
        // do stuff
    }
    
    // in another function
    cv::Mat m(10, 10, CV_32FC3);
    my_func(m);