title: efficient way of accessing opencv Mat elements
body: <p>I'm trying to play around with some OpenCV and thought up an interesting little scenario to work on.</p>
  
  <p>Basically, I want to take a pixel, add the colour values from the 3 neighbouring pixels (so (x, y), (x+1, y) (x, y+1) and (x+1, y+1)) and divide the result by 4 to get an average colour value. Then the next set of pixels I process is (x+2, y+2) with it's 3 neighbours.</p>
  
  <p>I then also want to be able to do a similar thing, but with 9 pixels (with the chosen co-ordinate to work from being the centre).</p>
  
  <p>Initially I started with a gaussian blur type masking, but that's not the result I want to acheive. As from those calculations, I just want to get 1 pixel value. So the output image will be 1/4 or a 1/9 of the size. So for now I've got it working where I've literally written out the calculation in a for loop as:</p>
  
  <pre><code>for (int i = 1; i &lt; myImage.rows -1; i++)
  {
      b = 0;
      for (int k = 1; k &lt; myImage.cols -1; k++)
      {
          //9 pixel radius
          Result.at&lt;Vec3b&gt;(a, b)[1] = (myImage.at&lt;Vec3b&gt;(i-1, k-1)[1]+myImage.at&lt;Vec3b&gt;(i-1, k)[1]+myImage.at&lt;Vec3b&gt;(i+1, k)[1] + myImage.at&lt;Vec3b&gt;(i, k)[1]+myImage.at&lt;Vec3b&gt;(i, k-1)[1]+myImage.at&lt;Vec3b&gt;(i, k+1)[1] + myImage.at&lt;Vec3b&gt;(i + 1, k+1)[1] + myImage.at&lt;Vec3b&gt;(i-1, k + 1)[1] + myImage.at&lt;Vec3b&gt;(i + 1, k - 1)[1]) / 9;
          Result.at&lt;Vec3b&gt;(a, b)[2] = (myImage.at&lt;Vec3b&gt;(i-1, k-1)[2]+myImage.at&lt;Vec3b&gt;(i-1, k)[2]+myImage.at&lt;Vec3b&gt;(i+1, k)[2] + myImage.at&lt;Vec3b&gt;(i, k)[2]+myImage.at&lt;Vec3b&gt;(i, k-1)[2]+myImage.at&lt;Vec3b&gt;(i, k+1)[2] + myImage.at&lt;Vec3b&gt;(i + 1, k+1)[2] + myImage.at&lt;Vec3b&gt;(i-1, k + 1)[2] + myImage.at&lt;Vec3b&gt;(i + 1, k - 1)[2]) / 9;
          Result.at&lt;Vec3b&gt;(a, b)[0] = (myImage.at&lt;Vec3b&gt;(i-1, k-1)[0]+myImage.at&lt;Vec3b&gt;(i-1, k)[0]+myImage.at&lt;Vec3b&gt;(i+1, k)[0] + myImage.at&lt;Vec3b&gt;(i, k)[0]+myImage.at&lt;Vec3b&gt;(i, k-1)[0]+myImage.at&lt;Vec3b&gt;(i, k+1)[0] + myImage.at&lt;Vec3b&gt;(i + 1, k+1)[0] + myImage.at&lt;Vec3b&gt;(i-1, k + 1)[0] + myImage.at&lt;Vec3b&gt;(i + 1, k - 1)[0]) / 9;
  
          //4 pixel radius
          //      Result.at&lt;Vec3b&gt;(a, b)[1] = (myImage.at&lt;Vec3b&gt;(i, k)[1] + myImage.at&lt;Vec3b&gt;(i + 1, k)[1] + myImage.at&lt;Vec3b&gt;(i, k + 1)[1] + myImage.at&lt;Vec3b&gt;(i, k - 1)[1] + myImage.at&lt;Vec3b&gt;(i - 1, k)[1]) / 5;
          //      Result.at&lt;Vec3b&gt;(a, b)[2] = (myImage.at&lt;Vec3b&gt;(i, k)[2] + myImage.at&lt;Vec3b&gt;(i + 1, k)[2] + myImage.at&lt;Vec3b&gt;(i, k + 1)[2] + myImage.at&lt;Vec3b&gt;(i, k - 1)[2] + myImage.at&lt;Vec3b&gt;(i - 1, k)[2]) / 5;
          //      Result.at&lt;Vec3b&gt;(a, b)[0] = (myImage.at&lt;Vec3b&gt;(i, k)[0] + myImage.at&lt;Vec3b&gt;(i + 1, k)[0] + myImage.at&lt;Vec3b&gt;(i, k + 1)[0] + myImage.at&lt;Vec3b&gt;(i, k - 1)[0] + myImage.at&lt;Vec3b&gt;(i - 1, k)[0]) / 5;
          b++;
      }
      a++;
  }
  </code></pre>
  
  <p>Obviously, it's possible to setup the two options as different function that is called, but I'm just wondering if there's a more efficient way of achieveing this, that would let the size of the mask be changed.</p>
  
  <p>Thanks for any help!</p>