Opencv in memory Mat representation

前端 未结 3 2049
情话喂你
情话喂你 2021-02-09 01:20

I know that in memory opencv represents Mat objects as one big array. So if I have 3 channels mat of dimension 200x200 then In memory it will store this mat in an array of size

3条回答
  •  广开言路
    2021-02-09 01:57

    I found other answers a bit confusing: mat.step is the size of a row in bytes, not in (double) elements, and it does already take into consideration the number of channels. To access val you should use:

    double* array = (double*) mat.data; // was (double) mat.data in the question
    double value = array[ ((mat.step)/mat.elemSize1())*c+mat.channels()*r+ch]; // (mat.step)/mat.elemSize1() is the actual row length in (double) elements
    

    You can verify this and other approaches comparing them with the .at<> operator as follows:

    #include 
    #include 
    
    using namespace cv;
    using namespace std;
    
    int main()
    {
    const int w0=5;
    const int h=3;
    const int w=4;
    double data[w0*h*3];
    for (int y=0; y(r,c);
    cout<<"the 3 channels at row="<
                                                            
提交回复
热议问题