title: OpenCV Mat class: Accessing elements of a multi-channel matrix
body: <p>I currently want to read in some values into a 3-channel, 480 row by 640 column matrix of 8 bit unsigned integer values.  I am initializing the matrix like this:</p>
  
  <p>Declaration:</p>
  
  <pre><code>rgbMatrix = Mat::zeros(480,640,CV_8UC3);
  </code></pre>
  
  <p>When I try to iterate through the entire matrix I am unable to assign/grab values using the following method.  The values simply stay 0.  My code looks like this:</p>
  
  <pre><code>    for (int i = 0; i &lt; rgbMatrix.rows; i++)
      {
          for (int j = 0; j &lt; rgbMatrix.cols; j++)
          {
             (rgbMatrix.data + rgbMatrix.step * i)[j * rgbMatrix.channels() + 0] = *value0*;
             (rgbMatrix.data + rgbMatrix.step * i)[j * rgbMatrix.channels() + 1] = *value1*;
             (rgbMatrix.data + rgbMatrix.step * i)[j * rgbMatrix.channels() + 2] = *value2*;
  
          }
       }
  </code></pre>
  
  <p>However, when I declare three separate 1-channel matrices (also 480 row by 640 column of 8 bit unsigned integer values) and attempt to access elements of those matrices the following code works:</p>
  
  <p>Declaration:</p>
  
  <pre><code>rgbMatrix0 = Mat::zeros(480,640,CV_8UC1);
  rgbMatrix1 = Mat::zeros(480,640,CV_8UC1);
  rgbMatrix2 = Mat::zeros(480,640,CV_8UC1);
  
      for (int i = 0; i &lt; rgbMatrix0.rows; i++)
      {
          for (int j = 0; j &lt; rgbMatrix0.cols; j++)
          {
             (rgbMatrix0.data + rgbMatrix0.step * i)[j] = *value0*;
             (rgbMatrix1.data + rgbMatrix1.step * i)[j] = *value1*;
             (rgbMatrix2.data + rgbMatrix2.step * i)[j] = *value2*;
  
          }
       }
  </code></pre>
  
  <p>Now, I want to use just one matrix for these operations, as having to keep track of three separate variables will get tiresome after a while.  I have a feeling that I am not accessing the right point in memory for the three-channel matrix.  Does anyone know how I can accomplish what I did in the second portion of code but using one three-channel matrix instead of three separate one-channel matrices?</p>
  
  <p>Thanks.</p>