title: convolution implementation in c++
body: <p>I want to implement 2D convolution function in C++ by myself, without using filter2D(). I'm trying to iterate all pixels of input image and kernel, then, assign new value to each pixel of dst.
  However, I got this error.</p>
  
  <blockquote>
    <p>Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)</p>
  </blockquote>
  
  <p>I found that this error tells I'm accessing nullptr, but I could not solve the problem. Here is my c++ code. </p>
  
  <pre><code>cv::Mat_&lt;float&gt; spatialConvolution(const cv::Mat_&lt;float&gt;&amp; src, const cv::Mat_&lt;float&gt;&amp; kernel)
  {
  //    declare variables
      Mat_&lt;float&gt; dst;
      Mat_&lt;float&gt; flipped_kernel;
      float tmp = 0.0;
  
  //    flip kernel
      flip(kernel, flipped_kernel, -1);
  
  //    multiply and integrate
  // input rows
      for(int i=0;i&lt;src.rows;i++){
  // input columns
          for(int j=0;j&lt;src.cols;j++){
  // kernel rows
              for(int k=0;k&lt;flipped_kernel.rows;k++){
  // kernel columns
                  for(int l=0;l&lt;flipped_kernel.cols;l++){
                      tmp += src.at&lt;float&gt;(i,j) * flipped_kernel.at&lt;float&gt;(k,l);
                  }
              }
              dst.at&lt;float&gt;(i,j) = tmp;
          }
      }
         return dst.clone();
  } 
  </code></pre>