title: OpenCV Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at
body: <p>I'm trying to do the smoothing of an RGB image using OpenCV. I'm using spatial correlation formula:</p>
  <p><a href="https://i.stack.imgur.com/f5orO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f5orO.png" alt="Spatial Correlation formula" /></a></p>
  <p>Here's the code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;iostream&gt;
  
  using namespace cv;
  using namespace std;
  
  int main(int argc, char** argv) {
      if (argc &lt; 2) {
          cout &lt;&lt; &quot;usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; image_name&quot; &lt;&lt; endl;
          exit(0);
      }
  
      String imageName = argv[1];
  
      Mat image;
  
      image = imread(imageName, IMREAD_COLOR);
  
      if (image.empty()) {
          cout &lt;&lt; &quot;Could not open or find the image&quot; &lt;&lt; std::endl;
          return -1;
      }
  
      int padding = 2;
      int padding2 = padding / 2;
  
      Mat copy = Mat::zeros(image.rows + padding, image.cols + padding, image.type());
  
      image.copyTo(copy(Rect(padding2, padding2, image.cols, image.rows)));
      //image.copyTo(copy(Range(padding2,image.rows+padding2),Range(padding2,image.cols+padding2)));
  
      cout &lt;&lt; &quot;rows: &quot; &lt;&lt; image.rows &lt;&lt; &quot; cols: &quot; &lt;&lt; image.cols &lt;&lt; endl;
      for (int i = 0; i &lt; image.rows + 1; i++) {
          for (int j = 0; j &lt; image.cols + 1; j++) {
              int sumB = 0;
              int sumG = 0;
              int sumR = 0;
              for (int r = -1; r &lt;= 1; r++) {
                  for (int c = -1; c &lt;= 1; c++) {
                      sumB += image.at&lt;Vec3b&gt;(i + 1 + r, j + 1 + c)[0];
                      sumG += image.at&lt;Vec3b&gt;(i + 1 + r, j + 1 + c)[1];
                      sumR += image.at&lt;Vec3b&gt;(i + 1 + r, j + 1 + c)[2];
                  }
              }
              cout &lt;&lt; &quot;i: &quot; &lt;&lt; i &lt;&lt; &quot; j: &quot; &lt;&lt; j &lt;&lt; endl;
              copy.at&lt;Vec3b&gt;(i + 1, j + 1)[0] = sumB / 9;
              copy.at&lt;Vec3b&gt;(i + 1, j + 1)[1] = sumG / 9;
              copy.at&lt;Vec3b&gt;(i + 1, j + 1)[2] = sumR / 9;
          }
      }
  
      imshow(&quot;Original&quot;, image);
      imshow(&quot;Copy&quot;, copy);
      waitKey(0);
      return 0;
  }
  </code></pre>
  <p>When I'm trying to execute it, I get this error:</p>
  <pre><code>OpenCV(3.4.9) Error: Assertion failed ((unsigned)(i1 * DataType&lt;_Tp&gt;::channels) &lt; (unsigned)(size.p[1] * channels())) in cv::Mat::at, file C:\opencv\build\include\opencv2\core\mat.inl.hpp, line 1179 
  </code></pre>
  <p>I'm using OpenCV 3.4.9 with Visual Studio 2022 in Windows 11.</p>