title: C++ calculate 2 3x3 Covariance matrices for each region in image
body: <p>I have an image <code>img</code> in c++ of size <code>mxn</code> of type <code>CV_8UC3</code> in OpenCV.
  I have another vector <code>b</code> of size <code>1xn</code> splitting <code>img</code> "horizontally" into two parts:</p>
  
  <p><code>upper_part = {(row,col)|1&lt;=col&lt;=n, 1&lt;=row&lt;=b(1,col)}</code></p>
  
  <p><code>lower_part = {(row,col)|1&lt;=col&lt;=n, b(1,col)&lt;row&lt;=m}</code>,</p>
  
  <p>where 1&lt;=b(1,col)&lt;=m (1&lt;=col&lt;=n).</p>
  
  <p>For these two parts of the image I'd like to have the covariance matrices <code>M_u</code> and <code>M_l</code> sort of "per" channel. This means the resulting matrices should have size 3x3 and should be derived like:</p>
  
  <p><code>M_u = 1/(N_u-1) * sum_{(row,col)\in upper_part} (img(row,col)-mu_u)*(img(row,col)-mu_u)^T</code>,</p>
  
  <p>where N_u is the number of elements in the upper part, mu_u a 3x1 vector describing the average RGB values in the upper part and <code>img(row,col)</code> is a 3x1 vector with the RGB values of <code>img</code> at position <code>(row,col)</code> . <code>M_l</code> is calculated equivalently with <code>N_l</code> and <code>mu_l</code> considering the <code>lower_part</code>.</p>
  
  <p>Furthermore, I also (sometimes) have to calculate the covariance for an <code>CV_8UC1</code> image. Of course then the matrix is just a scalar.</p>
  
  <p>Is there a solution primarily for the <code>CV_8UC3</code> type and if yes is there a solution which also works for the <code>CV_8UC1</code> type image?</p>
  
  <p>My current solution is to iterate over each pixel and calculate it by getting the values with <code>img.at&lt;Vec3b&gt;(row,col)</code> or <code>img.at&lt;unsigned char&gt;(row,col)</code> respectively (first for the mean, then for the covariance, thus two loops over all pixels), but what I've heard and now see is that this function is quite inefficient/slow. As I've to do this process of calculating <code>M_u</code> and <code>M_l</code> within a loop I'd like to derive the covariances efficiently.
  Any ideas?</p>
  
  <p>Thank you.</p>
  
  <p>PS: <code>m~1280</code> and <code>n~960</code>.</p>