3

I try to resize an image with MATLAB and there was something strange to understand directly about imresize function with bilinear mode.

I will give you examples to make sure what's the problem. Say, the followings are 1D-matrix.

A: [0 1 2]
B: [1 2 0 5]

the linear interpolation of scaling A to [1,5] in 1D array, so far as I know, makes

A: [0, 0.5, 1, 1.5, 2]

However, from MATLAB,

imresize(A,[1,5],'bilinear')

shows

[0, 0.4, 1, 1.6, 2.0]

Also,

imresize(B,[1,5],'bilinear')
imresize(B,[1,10],'bilinear')

each, shows

[1.0 1.7 1.0 1.5 2.0]
[1.0 1.1667 1.16111 1.8889 1.0000 0.1111 1.9444 4.1667 5.0000]

I found a lot of questions and answers from different forums and none of them makes me be satisfied, in terms of generality.

However, I found the answer from a line of code in 'imresize.m',

u = x/scale + 0.5 * (1-1/scale)

where u determines the index of output matrix. From the above, I realize how the strange outputs are made by imresize with bilinear

But the question is, I don't understand the meaning of 0.5 * (1-1/scale).

Without 0.5 * (1-1/scale), u = x/scale shows the original algorithm which can output

A: [0, 0.5, 1, 1.5, 2], which is truly linear.

Then, why do we need the term 0.5 * (1-1/scale)? What is the purpose and meaning?

Mark Yoon
  • 330
  • 4
  • 17
  • In the code, the comment just above that line tells you why (in my version: R2016b): `Input-space coordinates. Calculate the inverse mapping such that 0.5 in output space maps to 0.5 in input space, and 0.5+scale in output space maps to 1.5 in input space.`. In fact, this section of the code where that line appears is well documented. Each line is commented. Read the comments and see if they make sense. – rayryeng Feb 03 '17 at 09:10
  • 1
    In addition, I wrote a bilinear interpolation function from first principles **without** `imresize`. See if this function achieves what you are expecting in practice... which it should: http://stackoverflow.com/questions/26142288/resize-an-image-with-bilinear-interpolation-without-imresize/26143655#26143655. – rayryeng Feb 03 '17 at 09:11
  • Thank you for answer! I read the comments actually, but why 0.5? I still cannot get it. – Mark Yoon Feb 03 '17 at 09:33

1 Answers1

2

Assume that your image is A = [ 0 1 2]; so we can visualize the structure of image pixels as

     _________ _________ _________ 
    |         |         |         |
    |    0    |    1    |    2    |
    |_________|_________|_________|

    0   0.5   1   1.5   2   2.5   3

That its x coordinates ranges from 0 to 3 and position of value of the pixel is assumed in the center of it.

When we want to resize the image to 5 pixels we should find where the values should be extracted from the original image. For it we multiply [0:5] by 3/5.

     _____ _____ _____ _____ _____ 
    |     |     |     |     |     |
    |     |     |     |     |     |
    |_____|_____|_____|_____|_____|

    0    3/5   6/5   9/5   12/5   3

To find position of center of pixels we multiply ([0:4] + .5) by 3/5

((0:4) + .5) * 3/5

ans = 
    0.3   0.9   1.5   2.1   2.7

So for example to find value of the second pixel in the scaled image we should refer to position 0.9 in the orginal imaged and extract(interpolate) value of the pixel which is 0.4.

     _____ _____ _____ _____ _____ 
    |     |     |     |     |     |
    |     | 0.4 |  1  | 1.6 |     |
    |_____|_____|_____|_____|_____|

      0.3   0.9   1.5   2.1   2.7  

Value of the first and the last pixels(and generally those pixels that have positions beyond [0.5-2.5]) are set the same as the first and the last pixels of the original image respectively .

     _____ _____ _____ _____ _____ 
    |     |     |     |     |     |       
    |  0  | 0.4 |  1  | 1.6 |  2  |
    |_____|_____|_____|_____|_____|
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • 2
    Great help with clear illustration! Thank you. I can understand the equation, 'u = x/scale + 0.5 * (1-1/scale)'. In your example, A[0]=0 starts form an index 0.5, but when we consider it starting from idx 1, then we should subtract the 0.5*1/scale from the index of the right side (x/scale + 0.5) of each boxes in the figure. – Mark Yoon Feb 06 '17 at 11:10