title: why does changing pointer to data with cv::Mat (opencv) have problems with cv::filter2D?
body: <p>I want to migrate to using opencv for custom imaging detector data. Datasets can be quite huge (sometime 100's of thousands of frames), and I load them with the boost memory mapping. When testing out behavior, I create a cv::mat of appropriate size (for a single frame) and change the pointer to data of the frame I am looking at. This works fine for displaying the data (cv::imshow) or applying a color map, but fails when I use something like cv::filter2D. If I clone the data or use some copy, it works, but I don't want to start copying/cloning because I think this will slow performance (maybe I'm wrong).</p>
  
  <p>So - what am I doing wrong? Why doesn't cv::filter2D work here, and is there a better way?</p>
  
  <p>While running (using Windows 10 right now) I get the following in the terminal:</p>
  
  <pre><code>OpenCV(4.0.1) Error: Assertion failed (data == datastart + ofs.y*step[0] + ofs.x*esz) in cv::Mat::locateROI, file c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix.cpp, line 767
  OpenCV: terminate handler is called! The last OpenCV error is:
  OpenCV(4.0.1) Error: Assertion failed (data == datastart + ofs.y*step[0] + ofs.x*esz) in cv::Mat::locateROI, file c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix.cpp, line 767
  </code></pre>
  
  <p>This only happens when filter2D is used with a matrix constructed from the memory mapped array.</p>
  
  <p>I ran the program with the clone version un-commented and replaced the std::cout line in the loop with:</p>
  
  <pre><code>  std::cout&lt;&lt;"Is it continuous: " &lt;&lt; img.isContinuous() &lt;&lt; std::endl;
  </code></pre>
  
  <p>And the cv::Mat are indeed continuous.</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include&lt;opencv2/opencv.hpp&gt;
  #include&lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  
  
  #include&lt;iostream&gt;
  #include &lt;boost/iostreams/device/mapped_file.hpp&gt;
  
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
      boost::iostreams::mapped_file_source mem_ifile;
  
      mem_ifile.open(std::string("someimage.raw"));
      std::cout&lt;&lt;"file size: "&lt;&lt;mem_ifile.size()/(396*266*sizeof (float))&lt;&lt;std::endl;
      cv::Mat adjMap;
      cv::Mat img(266,396,CV_32FC1);
      cv::Mat falseColorsMap;
      cv::Mat b_hist;
      cv::Mat kernel;
      cv::Mat filsMap;
      kernel = cv::Mat::ones( 2, 2,CV_32FC1)/double(4.0);
      cv::namedWindow("image", cv::WINDOW_NORMAL);
      cv::namedWindow("false color", cv::WINDOW_NORMAL);
      cv::namedWindow("filtered", cv::WINDOW_NORMAL);
  
      for(unsigned long long i = 0;i&lt; mem_ifile.size()/(396*266*sizeof (float)); i++)
      {
          img.data = (reinterpret_cast&lt;uchar *&gt;(const_cast&lt;char *&gt;(mem_ifile.data()))+(i*396*266*sizeof (float)));
          cv::convertScaleAbs(img, adjMap, 255 / 500.0);
          applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_JET);
  
  // PROBLEM HERE - FIRST TWO WORK, LAST ONE DOESN'T
          //cv::filter2D(adjMap,filsMap,-1,kernel);           // works
          //cv::filter2D(img.clone(),filsMap,-1,kernel);      // works
          cv::filter2D(img,filsMap,-1,kernel);                // doesn't work
  
  
          std::cout&lt;&lt;"mean of adjmap: " &lt;&lt; cv::mean(adjMap) &lt;&lt; std::endl;
          cv::imshow("image", adjMap);
          cv::imshow("false color",falseColorsMap);
          cv::imshow("filtered", filsMap);
          cv::waitKey(20);
      }
      mem_ifile.close();
      return a.exec();
  }
  
  </code></pre>
  
  <p>Working code based on Beaker's suggestion:</p>
  
  <pre><code>
  #include &lt;QCoreApplication&gt;
  #include&lt;opencv2/opencv.hpp&gt;
  #include&lt;opencv2/highgui.hpp&gt;
  #include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  
  #include&lt;iostream&gt;
  #include &lt;boost/iostreams/device/mapped_file.hpp&gt;
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
      boost::iostreams::mapped_file_source mem_ifile;
  
      mem_ifile.open(std::string("/home/hugh/Documents/APS2017April/Ti7_nr2_e_x36000.raw"));
      std::cout&lt;&lt;"file size: "&lt;&lt;mem_ifile.size()/(396*266*sizeof (float))&lt;&lt;std::endl;
      cv::Mat adjMap;
      cv::Mat *img;
      cv::Mat falseColorsMap;
      cv::Mat b_hist;
      cv::Mat kernel;
      cv::Mat filsMap;
      kernel = cv::Mat::ones( 2, 2,CV_32FC1)/double(4.0);
      cv::namedWindow("image", cv::WINDOW_NORMAL);
      cv::namedWindow("false color", cv::WINDOW_NORMAL);
      cv::namedWindow("filtered", cv::WINDOW_NORMAL);
  
      for(unsigned long long i = 0;i&lt; mem_ifile.size()/(396*266*sizeof (float)); i++)
      {
          //img.data = (reinterpret_cast&lt;uchar *&gt;(const_cast&lt;char *&gt;(mem_ifile.data()))+(i*396*266*sizeof (float)));
          img = new cv::Mat(266,396,CV_32FC1,(reinterpret_cast&lt;uchar *&gt;(const_cast&lt;char *&gt;(mem_ifile.data()))+(i*396*266*sizeof (float))));
  
          cv::convertScaleAbs(*img, adjMap, 255 / 500.0);
          applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_JET);
  
          //cv::filter2D(adjMap,filsMap,-1,kernel);           // works
          //cv::filter2D(img-&gt;clone(),filsMap,-1,kernel);      // works
          cv::filter2D(*img,filsMap,-1,kernel);                // works
  
          std::cout&lt;&lt;"Is it continuous: " &lt;&lt; img-&gt;isContinuous() &lt;&lt; std::endl;
          cv::imshow("image", adjMap);
          cv::imshow("false color",falseColorsMap);
          cv::imshow("filtered", filsMap);
          cv::waitKey(20);
          delete img;
      }
      mem_ifile.close();
      return a.exec();
  }
  
  </code></pre>