Why do I observe oscillations when using the "imresize" function with bilinear interpolation?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 16 Apr 2025
Risposto: MathWorks Support Team
il 17 Apr 2025
When I try to downsample my linear function using the "imresize" function and set the "method" attribute to "bilinear", I observe oscillations in the resultant downsampled data. I expected the downsampled data to remain linear, as the "bilinear" method should not alter the data's nature. I also observe oscillations with other interpolation methods in the "imresize" function.
The following example code illustrates the issue:
% Create a 2D linear test function with values ranging from 1 to 200
exampleTest = repmat(1:200, [200 1]);
% Downsample the test function to a 150x150 matrix using 'bilinear' interpolation
downsampledMatrix = imresize(exampleTest, [150 150], 'bilinear');
% Extract a row from the downsampled result
downsampledRow = downsampledMatrix(80, :);
% Visualize the downsampled data
figure;
plot(downsampledRow, '.');
title('Downsampled Row Data');
% Compute the difference between consecutive elements in the extracted row
% Expect a constant difference, but observe oscillations instead
rowDifferences = diff(downsampledRow);
% Plot the differences to visualize the oscillations
figure;
plot(rowDifferences, '.');
title('Difference Between Consecutive Elements (Oscillations)');
What could cause this issue?
Risposta accettata
MathWorks Support Team
il 17 Apr 2025
When an image is downsized using the "imresize" function, it doesn't just use simple bilinear interpolation. It also applies an "Anti-Aliasing" filter to smooth out the image grid before resizing. "Antialiasing" is a Name-Value Pair Argument for the "imresize" function.
If you set the "Antialiasing" name-value argument to "false", the "imresize" function should behave more like simple bilinear interpolation.
The following example illustrates how the "imresize" function works with the "Antialiasing" attribute set to "false":
% Create a 2D linear test function with values ranging from 1 to 200
exampleTest = repmat(1:200, [200 1]);
% Downsample the test function to a 150x150 matrix using 'bilinear' interpolation
% and set 'Antialiasing' to false for a more direct bilinear effect
downsampledMatrix = imresize(exampleTest, [150 150], 'bilinear', 'Antialiasing', false);
% Extract a row from the downsampled result
downsampledRow = downsampledMatrix(80, :);
% Visualize the downsampled data
figure;
plot(downsampledRow, '.');
title('Downsampled Row Data');
% Compute the difference between consecutive elements in the extracted row
% Precision is expected to be in the order of 10^-14 for this example
rowDifferences = diff(downsampledRow);
% Visualize the precision and potential oscillations
figure;
plot(rowDifferences, '.');
title('Differences Between Consecutive Elements');
% Uncomment the line of code below to set the y-axis limit. Once uncommented,
% run the code again to observe a (nearly) constant line.
% ylim([0 2.5]);
When you set the "Antialiasing" attribute to "false" in this case, each output pixel is formed as a linear combination of one left-hand neighbor pixel and one right-hand neighbor pixel.
When the "Antialiasing" attribute is set to "true" here, the interpolation kernel is 3 pixels wide, meaning that each output pixel is a weighted combination of the 3 nearest neighbor pixels. This means that some output pixels will be formed using 2 left-hand neighbors and 1 right-hand neighbor or 2 right-hand neighbors and 1 left-hand neighbor, depending on where the output pixel lies relative to its neighbors.
The cycling you see between two different value levels in the output difference corresponds to whether one more weighted sample is used on the left-hand side or the right-hand side with the interpolation half width of 1.5.
To summarize, the oscillations should be a result of the interpolation kernel's width and the way it distributes weights among the input pixels.
Even though the original function is linear, the wider kernel can introduce slight variations in the output due to the different combinations of input pixels used for each output pixel.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Multirate Signal Processing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!