title: A beginner's attempt on image filtering
body: <p>My question might sound stupid but please understand i don't study in a university/college where a professor could give me decent help. I am self learning and its hardly been 3-4 months of coding for me. Hence i request patience from SO Users.</p>
  
  <p>I am trying to code a simple filter function where i define a <code>kernel</code>, a 3x3 2D Array. I create another matrix <code>load</code>, where i would like to store the pixel values of my Image array. I face 2 Major problems. For the 1st problem i am completely confused and bewildered. I read this brilliant answer about <a href="https://stackoverflow.com/questions/1696113/how-do-i-gaussian-blur-an-image-without-using-any-in-built-gaussian-functions?answertab=active#tab-top">How do I gaussian blur an image without using any in-built gaussian functions?</a>, it mentions:</p>
  
  <blockquote>
    <p>For pixel 11 you would need to load pixels 0, 1, 2, 10, 11, 12, 20,
    21, 22.</p>
    
    <p>you would then multiply pixel 0 by the upper left portion of the 3x3
    blur filter. Pixel 1 by the top middle, pixel 2, pixel 3 by top right,
    pixel 10 by middle left and so on.</p>
  </blockquote>
  
  <p>I would like to know for an <code>IplImage</code> with 3 channels how do i store the corresponding pixels [as mentioned in the link above] in my <code>load</code> matrix because what i am confused about is there are 3 values [R G B], so i am supposed to multiply what with what??</p>
  
  <p>Also how to ensure that the Pixels do not go out of bounds? Since once we are near the edges of the image the pixel value might go out of bounds.</p>
  
  <pre><code>void filter(const IplImage *img) 
  {
      unsigned char kernel[][3]  = { 1,  2,  1, 
                                     2,  1,  2, 
                                     1,  2,  1 , };
      unsigned char load[][3] = { 0 };
      int rows=img-&gt;height,cols=img-&gt;width,row,col;
      uchar* temp_ptr=0 ;
  
      for( row = 0; row &lt; rows; ++row) 
      {
  
              for ( col = 0; col &lt; cols; ++col) 
              {
                  CvPoint pt = {row,col};
                  temp_ptr  = &amp;((uchar*)(img-&gt;imageData + (img-&gt;widthStep*row)))[col*3];
  
              }
      }
  
  }
  </code></pre>