title: OpenCV Mat data member access
body: <p>I've seen a lot of OpenCV code which accesses the data member of a <code>cv::Mat</code> directly. <code>cv::Mat</code> stores the pointer to the data in a <code>unsigned char* data</code> member. Access to the data member looks like:</p>
  
  <pre><code>cv::Mat matUC(3,3,CV_8U)
  int rowIdx = 1;
  int colIdx = 1;
  
  unsigned char val = matUC.data[ rowIdx * matUC.cols + colIdx]
  </code></pre>
  
  <p>I'm wondering if this works for a <code>cv::Mat</code> with pixel type other than <code>unsigned char</code>.</p>
  
  <pre><code>cv::Mat matF(3,3,CV_32F)
  int rowIdx = 1;
  int colIdx = 1;
  
  float val = matF.data[ rowIdx * matF.cols + colIdx];
  </code></pre>
  
  <p>My understanding would be that a typecast is needed to access the elements correctly. Something like:</p>
  
  <pre><code>float val = ((float*)matF.data)[ rowIdx * matF.cols + colIdx];
  </code></pre>
  
  <p>I've seen a lot of code which doesn't use a typecast. So my question is: Is the typecast mandatory to access the correct element?</p>