How does imresize() calculate the weights and indices for interpolation?
1 view (last 30 days)
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);.
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.