title: What is the memory structure of OpenCV's cvMat?
body: <p>Imagine I have the following:</p>
  
  <pre><code>CvMat* mat = cvCreateMat(3,3,CV_16SC3)
  </code></pre>
  
  <p>This is a 3x3 matrix of integers of channel 3.</p>
  
  <p>Now if you look at OpenCV documentation you will find the following as the deceleration for cvMat:</p>
  
  <pre><code>typedef struct CvMat {
  
  int type;
  int step;
  
  int* refcount;
  
  union
  {
      uchar* ptr;
      short* s;
      int* i;
      float* fl;
      double* db;
  } data;
  
  union
  {
      int rows;
      int height;
  };
  
  union
  {
      int cols;
      int width;
  };
  } CvMat;
  </code></pre>
  
  <p>Now, I want to play around with the data.ptr, which is the pointer to the data stored in cvMat. However, I'm having a hard time understanding how the memory is layed out. If I have a 3 channel matrix, how does this work? For one channel its simple because it's just
  a simple matrix of MxN where M is rows and N is cols. However for 3 channel, are there 3 of
  these MxN matrix's?? Can someone show me how I would go about initalizing a 3 channel matrix via data.ptr and how to access these values please? Thank you.</p>