0

I'm have this function taken from here:

bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{         
   bool ret = false;
   // 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;
   int dim = res.rows * res.cols;
   float *out = res.ptr<float>(0);
   for (int j=-halfHeight; j<=halfHeight; ++j)
   {
      const float rx = ofsx + j * a12;
      const float ry = ofsy + j * a22;
      #pragma omp simd
      for(int i=-halfWidth; i<=halfWidth; ++i)
      {
         float wx = rx + i * a11;
         float wy = ry + i * a21;
         const int x = (int) floor(wx);
         const int y = (int) floor(wy);
         if (x >= 0 && y >= 0 && x < width && y < height)
         {
            // compute weights
            wx -= x; wy -= y;
            // bilinear interpolation
            *out++ = 
               (1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x)   + wx * im.at<float>(y,x+1)) +
               (       wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
         } else {
            *out++ = 0;
            ret =  true; // touching boundary of the input            
         }
      }
   }
   return ret;
}

I don't know what interpolation is in details, but looking at this opencv page, it seems that it's a bilinear interpolation using INTER_LINEAR. The point is that I don't know how to call an equivalent opencv function for the code above.

This function is called in two different points here.

justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • Why are you using interpolation when you don't know what it is? Interpolation is this: You got some surface, in your case a 2d rectangular image, and you want values on it. You have the values at certain points, in your case at the four corners. You want to compute the values in between in some way. Bilinear interpolation take your relative position within the image wrt the x- and y-axis and use it as linear weights (wx, wy) on the value of the corners. – Aziuth Mar 31 '17 at 09:36
  • @Aziuth Thanks for your comment and your explanation. The point is that I'm trying to optimize this code (with vectorization and parallel processors) and this function is time consuming and difficult to vectorize (because of the if statement). Using a different implementation (e.g. OpenCV interpolation) may be much faster. What I'm wondering is if I can do the same with some OpenCV function that I don't know. – justHelloWorld Mar 31 '17 at 09:57

1 Answers1

1

You can't solely apply simple an interpolation using openCV, it has to be part of image processing operation, e.g warp or resize operation.

I think you are trying to vectorize a warp affine, the easiest way and most efficient code on Intel platform would be to use IPP. Otherwise, I'd let Opencv Warp affine do the job.

TripleS
  • 1,216
  • 1
  • 23
  • 39