Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to rewrite cv::Mat::at in a pointer way?

I have this function:

bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{         
   // input size (-1 for the safe bilinear interpolation)
   const int width = im.cols-1;
   const int height = im.rows-1;
   // output size
   const int halfWidth  = res.cols >> 1;
   const int halfHeight = res.rows >> 1;
   float *out = res.ptr<float>(0);
   const float *imptr  = im.ptr<float>(0);
   const float *resptr = im.ptr<float>(0);
   for (int j=-halfHeight; j<=halfHeight; ++j)
   {
      //...
      for(int i=-halfWidth; i<=halfWidth; ++i, out++)
      {
         const int x = (int) //something;
         const int y = (int) //something;
         if (x >= 0 && y >= 0 && x < width && y < height)
         {
            std::cout<<"im(y,x)="<<im.at<float>(y,x)<<" imptr[y*width+x]="<<imptr[y*width+x]<<std::endl;    
         }
      }
   }
   return ret;
}

Where I want to use const float *imptr instead of const cv::Mat img for better efficiency. However, I can't understand why, but imptr[y*width+x]!=im.at<float>(y,x). Why?