stretching or compressing part of a matrix

조회 수: 2 (최근 30일)
Mohammed Qahosh
Mohammed Qahosh 2023년 10월 19일
답변: Sandeep Mishra 2024년 10월 4일
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