问题
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?
回答1:
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 |
|_____|_____|_____|_____|_____|
来源:https://stackoverflow.com/questions/42020435/imresize-bilinear-matlab