stretching or compressing part of a matrix

2 vues (au cours des 30 derniers jours)
Mohammed Qahosh
Mohammed Qahosh le 19 Oct 2023
I have 400 by 400 pixels data set as in the attached txt file. When plotting it, the final figure is having a trapezoidal shape.
Is it possible in matlab to stretch the lower part of the figure or to compress the uppper part to have a square final shape.
Thanks for help.

Réponse acceptée

Sandeep Mishra
Sandeep Mishra le 4 Oct 2024
Hi Mohammed,
I see that you are trying to modify a trapezoidal part of image into a square shape.
To achieve this, you can utilize the 'imresize' function to incrementally increase the width of the image. This process involves gradually scaling the top part with a smaller factor and the bottom part with a larger one.
Refer to the following code snippet:
% Reading the text file
M = readmatrix('Evsky.txt');
% Size of matrix
[rows, columns] = size(M);
% Scaling factors
topScale = 1.05;
bottomScale = 1.3;
% Initializing stretched image
stretchedImage = zeros(rows, columns * bottomScale);
% Scaling factors for each row
scalingFactors = linspace(topScale, bottomScale, rows);
for r = 1:rows
% new width for each row
newWidth = round(columns * scalingFactors(r));
% Resizing row
resizedRow = imresize(M(r, :), [1 newWidth]);
% Streching row
startIndex = round((columns * bottomScale - newWidth) / 2) + 1;
% Place the resized row in the new matrix
stretchedImage(r, startIndex:startIndex + newWidth - 1) = resizedRow;
end
% Stretched image
imshow(stretchedImage, []);
Please refer to the following MathWorks Documentation to learn more about ‘imresize’ function in MATLAB: https://www.mathworks.com/help/releases/R2024b/matlab/ref/imresize.html
I hope this helps you in resolving the query.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Math dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by