title: OpenCV fast mat element and neighbour access
body: <p>I use OpenCV (C++) Mat for my matrix and want to acces single Mat elements as fast as possible. From OpenCV tutorial, I found code for efficient acces:</p>
  
  <pre><code>for( i = 0; i &lt; nRows; ++i)
      {
          p = I.ptr&lt;uchar&gt;(i);
          for ( j = 0; j &lt; nCols; ++j)
          {
              p[j] = table[p[j]];
          }
      }
  </code></pre>
  
  <p>For my problem, I need to access a Mat element and its neighbours (i-1,j-1) for a calculation. How can I adapt the given code to acces a single mat element AND its surrounding elements? Since speed matters, I want to avoid <code>Mat.at&lt;&gt;()</code>.
  What is the most efficient way to acces a Mat value and its neighbour values?</p>