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

asked 2017-04-30 04:16:19 -0600

lovaj gravatar image

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?

edit retag flag offensive close merge delete

Comments

It is not imptr[y * width+x] but imptr[y * im.cols+x] (or imptr[y * (width+1)+x])and you have to check too if im.IsContinuous()

LBerger gravatar imageLBerger ( 2017-04-30 04:38:15 -0600 )edit

@LBerger daaaaaamn in the whole project width=im.cols, this was an hell of a joke! Thanks so much for noticing it!

lovaj gravatar imagelovaj ( 2017-04-30 04:57:05 -0600 )edit