title: Segmentation fault (core dumped) error while finding convolution of images in opencv c++
body: <p>I am new to opencv. I am trying to do convolution of an image using kernel having same size as image in opencv c++. I am getting an error 'Segmentation fault (core dumped)'. I checked for intialisation of variables and for loop. But I am not able to sort out exactly where the problem is coming. Can anybody please help me in finding out the problem. My code is given below:</p>
  
  <pre><code>#include&lt;opencv2/highgui/highgui.hpp&gt;
  #include "opencv2/imgproc/imgproc.hpp"
  #include&lt;stdio.h&gt;
  #include&lt;iostream&gt;
  #include&lt;math.h&gt;
  #include&lt;cv.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  Mat img;
  Mat kernel, gd, dest;
  int c = 120;
  double mysum = 0.0, mysum1 = 0.0, k = 0;
  int cent=0,radius=0;
  enum ConvolutionType {   
      /* Return the full convolution, including border */
      CONVOLUTION_FULL, 
  
      /* Return only the part that corresponds to the original image */
      CONVOLUTION_SAME,
  
      /* Return only the submatrix containing elements that were not influenced by the       
      border       
      */
      CONVOLUTION_VALID
  };
  
  void conv2(const Mat &amp;img, const Mat&amp; kernel, ConvolutionType type,Mat&amp; dest)
  {
  
      Mat source = img;
      if(CONVOLUTION_FULL == type) 
      {
          source = Mat();
          const int additionalRows = kernel.rows - 1, additionalCols = kernel.cols - 1;
          copyMakeBorder(img, source, (additionalRows + 1) / 2, additionalRows / 2, 
              (additionalCols + 1) / 2, additionalCols / 2, BORDER_CONSTANT, Scalar(0));
      }
  
      flip(kernel, kernel, -1);
      Point anchor(kernel.cols - kernel.cols / 2 - 1, kernel.rows - kernel.rows / 2 - 1);
      int borderMode = BORDER_CONSTANT;
      filter2D(source, dest, img.depth(), kernel, anchor, 0, borderMode);
  
      if(CONVOLUTION_VALID == type)
      {
          dest = dest.colRange((kernel.cols - 1) / 2, dest.cols - kernel.cols / 2).rowRange((kernel.rows - 1) / 2, dest.rows - kernel.rows / 2);
      }
  } 
  
  int main()
  {  
      img = imread("building1.jpg", CV_LOAD_IMAGE_COLOR);
      dest.create(img.size(), img.type());
      gd.create(img.size(), img.type());
  
      for(int j = 0; j &lt; img.rows; j++)
      {
          for(int i = 0; i &lt; img.cols; i++)
          {
              radius = ((cent - i)^2 + (cent - j)^2);
              gd.at&lt;float&gt;(j, i) = exp((-(radius) / c^2));
  
              mysum = mysum + gd.at&lt;float&gt;(j, i);
          }
          mysum1 = mysum1 + mysum; 
      }
  
      k=1/mysum1;
      cout&lt;&lt;endl&lt;&lt;k&lt;&lt;"\n"&lt;&lt;endl;
  
      for(int j = 0; j &lt; img.rows; j++)
      {
          for(int i = 0; i &lt; img.cols; i++)
          {
              gd.at&lt;float&gt;(j, i) = k * gd.at&lt;float&gt;(j, i);
          }
      }  
  
      conv2(img, gd, CONVOLUTION_FULL, dest);
      imshow("conv", dest);
      waitKey(0);
      return 0;
  }
  </code></pre>