6

I define an array of 2 values, and try to use the imgproc module's resize function to resize it to 10 elements with linear interpolation as interpolation method.

cv::Mat input = cv::Mat(1, 2, CV_32F);
input.at<float>(0, 0) = 0.f;
input.at<float>(0, 1) = 1.f;
cv::Mat output = cv::Mat(1, 11, CV_32F);
cv::resize(input, output, output.size(), 0, 0, cv::INTER_LINEAR);
for(int i=0; i<11; ++i)
{
    std::cout<< output.at<float>(0, i) << " ";
}

The output I would have expected is:

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

What I get however is:

0 0 0 0.136364 0.318182 0.5 0.681818 0.863636 1 1 1

Clearly, my understanding of how resize works is wrong at a fundamental level. Can someone please tell me what I am doing wrong? Admittedly, OpenCV is an overkill for such simple linear interpolation, but please do help me with what is wrong here.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
balajeerc
  • 3,998
  • 7
  • 35
  • 51

1 Answers1

5

It's really simple. OpenCV is an image processing library. So you should remember that we are working on images.

Take a look at the output when we have only 8 pixels in destination image

0 0 0.125 0.375 0.625 0.875 1 1

If you take a look at this image it's very straightforward to understand resize behaviour

Example

As you can see in this link you're using a Image transformation library: "The functions in this section perform various geometrical transformations of 2D images"

You want this results

enter image description here

but it will not interpolate properly the original 2 pixels image

Nicola Pezzotti
  • 2,317
  • 1
  • 16
  • 26
  • Either you have restated my question with an image or I am afraid I don't find the behaviour straightforward to understand despite your explanation. The question is: why isn't the result (in your case): [0 0.167 0.334 0.501 0.668 0.835 1] for linear interpolation? – balajeerc Aug 08 '13 at 11:45
  • Because you have to consider the blue dots as the center of the original pixels. So the interpolation works only between the two original dots. Only half of the new range will contains interpolated values. You've got to think in pixels. – Nicola Pezzotti Aug 08 '13 at 12:19
  • I've explained a littele more :) – Nicola Pezzotti Aug 08 '13 at 12:28
  • So is there any way in OpenCV to get what balajeerc originally wanted? I have a similar problem, and haven't been able to fudge the resize parameters in the general case to get that. I saw similar issues with the remap and warpAffine functions. – jshouchin Feb 03 '15 at 14:53