Ask Your Question

Is there any OpenCV or IPP equivalent for this function?

asked 2017-05-03 07:20:57 -0500

lovaj gravatar image

updated 2017-05-03 08:47:29 -0500

I have this interpolate function taken from this code which I want to optimize:

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);
   const float *imptr  = im.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) * imptr[rowOffset+x]   + wx * imptr[rowOffset+x+1]) +
            (       wy) * ((1.0f - wx) * imptr[rowOffset1+x] + wx * imptr[rowOffset1+x+1]);
        } else {
            *out++ = 0;
            ret =  true; // touching boundary of the input            
   return ret;

I'm not the image processing guy and I'm really struggling to perform the equivalent using the opencv implementation. This is crucial for my project since the function above is incredibly time consuming.

Please help.

edit retag flag offensive close merge delete


It seems you can seriously speedup your own code by replacing all the constructions as<float>(x,y) by the pointers arithmetics equivalent.

pi-null-mezon gravatar imagepi-null-mezon ( 2017-05-03 08:35:07 -0500 )edit

@pi-null-mezon I'm sorry, it was already optimized on that point, I updated the question. Can you help me in some other way? I'm seriously stucked on this, using an equivalent function could help me so much

lovaj gravatar imagelovaj ( 2017-05-03 08:48:38 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-03 22:02:24 -0500

Tetragramm gravatar image

This looks like it's shrinking the image by a factor of 2, and a sub-pixel offset.

Just create an affine matrix using getRotationMatrix2D and use warpAffine to do the actual warping. The results of the interpolation won't be exactly the same, but it should be essentially the same.

edit flag offensive delete link more


@Tetragramm thanks for you help. I kinda understand the process, but I'm a little bit confused by how to use the code...and you know, just a little mistake and you get 0 results in image processing! Could you PLEASE help me writing the correct code?

lovaj gravatar imagelovaj ( 2017-05-04 03:34:36 -0500 )edit

Hmm, on further examination, that's not what it's doing. Unfortunately, I can't tell what it is doing. Is this just a version of the warpAffine function?

I assume you have this in a state that runs? Can you set a breakpoint, save the input parameters and the input and output images?

Tetragramm gravatar imageTetragramm ( 2017-05-04 20:53:28 -0500 )edit

@Tetragramm thanks again for your help. I'll try to do In this question you can see a little more details.

lovaj gravatar imagelovaj ( 2017-05-06 08:17:51 -0500 )edit

@Tetragramm I'll post the images very soon as you asked btw ;)

lovaj gravatar imagelovaj ( 2017-05-06 08:18:43 -0500 )edit

@Tetragram I've opened this question about the input and output images that you asked, please help!

lovaj gravatar imagelovaj ( 2017-05-20 04:37:33 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-05-03 07:20:57 -0500

Seen: 116 times

Last updated: May 03 '17