How does imresize() calculate the weights and indices for interpolation?

1 view (last 30 days)
Yunhan Wang
Yunhan Wang on 10 May 2022
Edited: Yunhan Wang on 10 May 2022
In imresize() function, it calls matlab.images.internal.resize.contributions(), which calculates the weights and indices vectors for a dimension to be resized. I am trying to understand what's the logic behind this contribution() function, especially the step of computing the indices of the input pixels involved in computing the k-th output pixel are in row k of the indices matrix, indices = bsxfun(@plus, left, 0:P-1);.
Thanks.

Answers (1)

DGM
DGM on 10 May 2022
Edited: DGM on 10 May 2022
If I recall, this is essentially a windowed operation on a vector at this point, and you're finding the indices associated with the window for each step along the vector. The variable left is the leftmost index being considered for a given window position, and it's a column vector. P is effectively the window width (scalar).
So in order to do arithmetic operations on left (a column vector), and 0:P-1 (a row vector), you'd need to use bsxfun(). Since R2016b, this operation can be done without it, but using bsxfun() is the legacy way to do this sort of array expansion.
Either way, the result is a 2D array of indices into the input vector.
Similarly with the weighting array; u is the input coordinate vector (column) and the index matrix is subtracted from it, giving indices into the kernel itself (which were represented by dim2 of the prior result). The kernel is then essentially used as a lookup table, and the entire list of weights can be calculated.
I forget the details of the context in which contributions() is used, so this explanation may be a bit fuzzy. It's been a long time since I dug through these files, and I never was intent on wrapping my head around the exact methods used.
If I wanted to really intuit what was going on, I'd copy and paste the code into a new file and modify it tor run as a script with some simple and easily understandable inputs. I'd then just run it with the hood open, so to speak. Observe inputs, intermediate calculations, and outputs. It's a short enough file that it should be fairly easy to do.
  1 Comment
Yunhan Wang
Yunhan Wang on 10 May 2022
Thanks for the reply. I was thinking the same thing. I thought indicies matrix was some sort of windowing operation to locate the near pixels for the interpolation, but I was curious about the math behind it on how these indicises are found.
For example, if I want to resize an arbitary 3x3 matrix to 2x4, the first dimension it will resize is row, so 3x3 -> 2x3. In this case, I could understand why u = [1.25, 2.75], but I don't get it why left-most pixel is [-2, -1]'.
And for the second dimension, it will go 2x3 -> 2x4, and in this case u = [0.8725, 1.625, 2.375, 3.125], and [-2, -1, 0, 1]'. So my question is what's the general idea behind this leftmost index as well as the arithmetic operations on left (a column vector), and 0:P-1 (a row vector).

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!