title: average pooling C++ error
body: <pre><code>#include "opencv2/core/core.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include &lt;math.h&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  using namespace cv;
  using namespace std;
  #define ATD at&lt;double&gt;
  
  Mat average_pooling2x2(Mat mat, int padding_mathed)
  {
  int width_remain = mat.cols % 2;
  int high_remain = mat.rows % 2;
  
  Mat mat_new;
  if (width_remain == 0 &amp;&amp; high_remain == 0)
      mat.copyTo(mat_new);
  else
  {
      if (padding_mathed == 1)//valid
      {
          Rect roi = Rect(0, 0, mat.cols - width_remain, mat.rows - high_remain);
          mat(roi).copyTo(mat_new);
      }
      else //same
      {
          mat.copyTo(mat_new);
          if (high_remain != 0)
          {
              Mat row_add = cv::Mat::zeros(high_remain, mat_new.cols,mat_new.type());
              mat_new.push_back(row_add);
          }
          if (width_remain != 0)
          {
              Mat col_add = cv::Mat::zeros(width_remain, mat_new.rows, mat_new.type());
              mat_new = mat_new.t();
              mat_new.push_back(col_add);
              mat_new = mat_new.t();
          }
      }
  }
  
  Mat res(mat_new.cols / 2, mat_new.rows / 2, mat_new.type(), Scalar::all(0));
  
  
  if (mat_new.channels() ==3)
  {  
  
  
      for (int i = 0; i &lt; res.rows; i++)//this is where error happened
      {
          uchar *data_res = res.ptr&lt;uchar&gt;(i);
          uchar * data = mat_new.ptr&lt;uchar&gt;(2*i);
          uchar * data1 = mat_new.ptr&lt;uchar&gt;(2*i+1);
          for (int j = 0; j &lt; res.cols*res.channels(); j = j + 3)
          {
              data_res[j] = (data[j*2] + data[j*2+3] + data1[j*2] + data1[j*2+3]) / 4;
              data_res[j + 1] = (data[j*2+1] + data[j*2+4] + data1[j*2+1] + data1[j*2+4]) / 4;
              data_res[j + 2] = (data[j*2+2] + data[j*2+5] + data1[j*2+2] + data1[j*2+5]) / 4;
  
          }
      }
  
  }
  
  else
  {
      for (int i = 0; i&lt;res.rows; i++)
      {
          for (int j = 0; j&lt;res.cols; j++)
          {
              Mat temp;
              Rect roi = Rect(j * 2, i * 2, 2, 2);
              mat_new(roi).copyTo(temp);
              double val;
              val = sum(temp)[0] / (2 * 2);
              res.ATD(i, j) = val;
          }
      }
  
  }
  
  return res;
  
  
  }
  
  
  int main(int argc, char** argv)
  {
      Mat image = imread("C://Users//Administrator//Desktop//11.jpg");
      imshow("???", image);
      Mat pooling_image;
      average_pooling2x2(image, 2).copyTo(pooling_image);
      imshow("???", pooling_image);
      waitKey();
      return 0;
  }
  </code></pre>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (y == 0 || (data &amp;&amp; dims >= 1 &amp;&amp; (unsigned)y &lt; (unsigned)size.p[0])) in cv::Mat::ptr, file d:\opencv\build\include\opencv2\core\mat.inl.hpp, line 827</p>
  </blockquote>
  
  <p>reccently I try to implement the average pooling using C++, this is the error when I run the code, it seems that maybe the ptr pointer is out of range. but I just can not figure out where is the problem. Really need some help</p>