stretching or compressing part of a matrix

2 次查看(过去 30 天)
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.

采纳的回答

Sandeep Mishra
Sandeep Mishra 2024-10-4
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by