title: How to implement convolution Gaussian blur without padding using OpenCV in C++
body: <p>I am implementing convolution Gaussian blur with OpenCV without using OpenCV functions, but my code is programmed with padding.</p>
  <p>Now I want to remove the padding from the matrix so that my code can be executed without the matrix with padding. How can I modify the code below to do so?</p>
  <pre><code>    class Filteration {
      private:
          // member function to pad the image before convolution
          Mat padding(Mat img, int k_width, int k_height, string type)
          {
              Mat scr;
              img.convertTo(scr, CV_64FC1);
              int pad_rows, pad_cols;
              pad_rows = (k_height - 1) / 2;
              pad_cols = (k_width - 1) / 2;
              Mat pad_image(Size(scr.cols + 2 * pad_cols, scr.rows + 2 * pad_rows), CV_64FC1, 
              Scalar(0));
              scr.copyTo(pad_image(Rect(pad_cols, pad_rows, scr.cols, scr.rows)));
              // mirror padding
              if (type == &quot;mirror&quot;)
              {
                  for (int i = 0; i &lt; pad_rows; i++)
                  {
                      scr(Rect(0, pad_rows - i, scr.cols, 1)).copyTo(pad_image(Rect(pad_cols, 
                  i, scr.cols, 1)));
                      scr(Rect(0, (scr.rows - 1) - pad_rows + i, scr.cols, 
                  1)).copyTo(pad_image(Rect(pad_cols,
                          (pad_image.rows - 1) - i, scr.cols, 1)));
                  }
      
                  for (int j = 0; j &lt; pad_cols; j++)
                  {
                      pad_image(Rect(2 * pad_cols - j, 0, 1, 
                      pad_image.rows)).copyTo(pad_image(Rect(j, 0, 1, pad_image.rows)));
                      pad_image(Rect((pad_image.cols - 1) - 2 * pad_cols + j, 0, 1, 
                      pad_image.rows)).
                          copyTo(pad_image(Rect((pad_image.cols - 1) - j, 0, 1, 
                      pad_image.rows)));
                  }
      
                  return pad_image;
              }
              // replicate padding
              else if (type == &quot;replicate&quot;)
              {
                  for (int i = 0; i &lt; pad_rows; i++)
                  {
                      scr(Rect(0, 0, scr.cols, 1)).copyTo(pad_image(Rect(pad_cols, i, scr.cols, 
                      1)));
                      scr(Rect(0, (scr.rows - 1), scr.cols, 1)).copyTo(pad_image(Rect(pad_cols, 
                          (pad_image.rows - 1) - i, scr.cols, 1)));
                  }
      
                  for (int j = 0; j &lt; pad_cols; j++)
                  {
                      pad_image(Rect(pad_cols, 0, 1, pad_image.rows)).copyTo(pad_image(Rect(j, 
                      0, 1, pad_image.rows)));
                      pad_image(Rect((pad_image.cols - 1) - pad_cols, 0, 1, pad_image.rows)).
                          copyTo(pad_image(Rect((pad_image.cols - 1) - j, 0, 1, 
                   pad_image.rows)));
                  }
                  // zero padding
                  return pad_image;
              }
              else
              {
                  return pad_image;
              }
      
          }
          // member function to define kernels for convolution
          Mat define_kernel(int k_width, int k_height, string type)
          {
              // box kernel
              if (type == &quot;box&quot;)
              {
                  Mat kernel(k_height, k_width, CV_64FC1, Scalar(1.0 / (k_width * k_height)));
                  return kernel;
              }
              // gaussian kernel
              else if (type == &quot;gaussian&quot;)
              {
                  // I will assume k = 1 and sigma = 1
                  int pad_rows = (k_height - 1) / 2;
                  int pad_cols = (k_width - 1) / 2;
                  Mat kernel(k_height, k_width, CV_64FC1);
                  for (int i = -pad_rows; i &lt;= pad_rows; i++)
                  {
                      for (int j = -pad_cols; j &lt;= pad_cols; j++)
                      {
                          kernel.at&lt;double&gt;(i + pad_rows, j + pad_cols) = exp(-(i*i + j*j) / 
                  2.0);
                      }
                  }
                  kernel = kernel / sum(kernel);
                  return kernel;
              }
          }
          
      public:
          
          // member function to implement convolution
          void convolve(Mat scr, Mat &amp;dst, int k_w, int k_h, string paddingType, string 
          filterType)
          {
              Mat pad_img, kernel;
              pad_img = padding(scr, k_w, k_h, paddingType);
              kernel = define_kernel(k_w, k_h, filterType);
      
              Mat output = Mat::zeros(scr.size(), CV_64FC1);
      
              for (int i = 0; i &lt; scr.rows; i++)
              {
                  for (int j = 0; j &lt; scr.cols; j++)
                  {
                      output.at&lt;double&gt;(i, j) = sum(kernel.mul(pad_img(Rect(j, i, k_w, 
               k_h)))).val[0];
                  }
              }
              output.convertTo(dst, CV_8UC1); //IplImage: IPL_DEPTH_8U Mat: CV_8UC1, CV_8UC2, 
              CV_8UC3, CV_8UC4
              
          }
      
      };
  
  int main(){
      Mat img, dst;
      // 
  
  Load the image
  img = imread(&quot;mrl.jpg&quot;, 0);   
  
  
  
  Mat kernel;
  int k_w = 5;  // kernel width
  int k_h = 5;  // kernel height
  
  
  
  Filteration F1;
  F1.convolve(img, dst, k_w, k_h, &quot;gaussian&quot;, &quot;box&quot;);
  
  namedWindow(&quot;dst&quot;, WINDOW_AUTOSIZE);
  
  imshow(&quot;dst&quot;, dst);
   
  waitKey(0);
  }
  </code></pre>