title: Problem with Operation on Border pixels of an image
body: <p>I am trying to implement a demosaicing algorithm (interpolation) for a raw image with the Bayer pattern <strong>GRBG</strong>. The program logic was to use the neighboring pixels to assign the values to R,G and B channels(I have attached the code). I am having a problem for this logic at the border pixels. For example let <strong><em>i</em></strong> be the pixel at <em>(0,0)</em>, I need the value of <strong><em>i-1</em></strong> which is not present in the image. My question is there a possibility to work around this like masking i-1 and the others as 0 without adding an new border of zeros to my existing image.</p>
  
  <p>Any suggestions will be helpful.
  thanks.</p>
  
  <pre><code>    int rows = 256;
      int cols = 512;
      Mat raw_img(rows, cols, CV_8U);                 //////////////////////
      Mat image(rows, cols, CV_8UC3);                 //  BAYER PATTERN   //
      cvtColor(image, image, COLOR_BGR2RGB);          //      G R         //  
      for (int i = 0; i &lt; raw_img.rows; i++) {        //      B G         //
          for (int j = 0; j &lt; raw_img.cols; j++) {    //////////////////////
              if ((i % 2 == 0) &amp;&amp; (j % 2 == 0))//top green
              {
                  image.at&lt;Vec3b&gt;(i, j)[0] = (raw_img.at&lt;uchar&gt;(i - 1, j) + 
                                              raw_img.at&lt;uchar&gt;(i + 1, j)) / 2; //red
  
                  image.at&lt;Vec3b&gt;(i, j)[1] = (raw_img.at&lt;uchar&gt;(i, j) * 2); //blue
  
                  image.at&lt;Vec3b&gt;(i, j)[2] = (raw_img.at&lt;uchar&gt;(i, j - 1) +
                                              raw_img.at&lt;uchar&gt;(i, j + 1)) / 2;  //green
              }
              else if ((i % 2 == 0) &amp;&amp; (j % 2 == 1))//red
              {
                  image.at&lt;Vec3b&gt;(i, j)[0] = (raw_img.at&lt;uchar&gt;(i, j));        //red
  
                  image.at&lt;Vec3b&gt;(i, j)[1] = (raw_img.at&lt;uchar&gt;(i - 1, j) + 
                                              raw_img.at&lt;uchar&gt;(i + 1, j) +
                                              raw_img.at&lt;uchar&gt;(i, j - 1) + 
                                              raw_img.at&lt;uchar&gt;(i, j + 1)) / 2;//green
  
                  image.at&lt;Vec3b&gt;(i, j)[2] = (raw_img.at&lt;uchar&gt;(i + 1, j - 1) +
                                              raw_img.at&lt;uchar&gt;(i - 1, j + 1) +
                                              raw_img.at&lt;uchar&gt;(i + 1, j + 1) + 
                                              raw_img.at&lt;uchar&gt;(i - 1, j - 1)) / 4;//blue
  
              }
              else if ((i % 2 == 1) &amp;&amp; (j % 2 == 0))//blue
              {
                  image.at&lt;Vec3b&gt;(i, j)[0] = (raw_img.at&lt;uchar&gt;(i + 1, j - 1) + 
                                              raw_img.at&lt;uchar&gt;(i - 1, j + 1) +
                                              raw_img.at&lt;uchar&gt;(i + 1, j + 1) + 
                                              raw_img.at&lt;uchar&gt;(i - 1, j - 1)) / 4;//red
  
                  image.at&lt;Vec3b&gt;(i, j)[1] = (raw_img.at&lt;uchar&gt;(i + 1, j) + 
                                              raw_img.at&lt;uchar&gt;(i, j + 1) +
                                              raw_img.at&lt;uchar&gt;(i - 1, j) + 
                                              raw_img.at&lt;uchar&gt;(i, j + 1)) / 2;//green
  
                  image.at&lt;Vec3b&gt;(i, j)[0] = (raw_img.at&lt;uchar&gt;(i, j));//blue
              }
              else // bottom green
              {
                  image.at&lt;Vec3b&gt;(i, j)[0] = (raw_img.at&lt;uchar&gt;(i, j - 1) + 
                                              raw_img.at&lt;uchar&gt;(i, j + 1)) / 2;//red
  
                  image.at&lt;Vec3b&gt;(i, j)[1] = (raw_img.at&lt;uchar&gt;(i, j) * 2);//blue
  
                  image.at&lt;Vec3b&gt;(i, j)[2] = (raw_img.at&lt;uchar&gt;(i - 1, j) + 
                                              raw_img.at&lt;uchar&gt;(i + 1, j)) / 2;//green
              }
          }
      }
  </code></pre>