stretching or compressing part of a matrix
2 views (last 30 days)
Show older comments
Mohammed Qahosh
on 19 Oct 2023
Answered: Sandeep Mishra
on 4 Oct 2024
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.
Accepted Answer
Sandeep Mishra
on 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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!