I have a matrix M of size m x n
m and n may or may not be equal - M can be rectangular or square matrix - where m and n are less than 150,
I wanted to resize M to 150 x 150 by padding border with zeros, such that the m x n will come in the center
M_resized = padarray(M,[x y],0,'both');
how to compute value for x and y such that i can use the same line of code for any matrix,
or is there any another way to do?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 13 Mai 2021

1 Stimme

You can use the 'pre' and 'post' options to pad each side with the desired number of zeros.
Or use this "trick"
g = ones(150, 150); % Original matrix is 150 x 150
[rows, columns] = size(g);
m = 153
n = 152;
rowsPre = floor((m - rows)/2)
collsPre = floor((n - columns)/2)
% Pad it out with 334 rows top and bottom,
% and 281 columns left and right.
P = padarray(g, [rowsPre, collsPre], 0, 'both');
whos P % Originally 152x152 padded with zeros all around.
% Not the size we want yet. We want 153x152
% Here comes the "trick" to expand out the lower right corner with zeros.
P(m, n) = 0;
whos P % Now 153 x 152

3 Kommentare

Hamza Mehmood
Hamza Mehmood am 27 Aug. 2022
I have a similar problem.
I am trying to apply ssim index between a 25x25x25 Nifti and a 256x256x299 Nifti. Nifti is a 3D image.
For some context, I am trying to see whether infromation within a 25x25x25 ROI, extracted from a PET body scan of dimensions 256x256x299, is being activated within intermediate filter activations of 256x256x299. These intermediate filter activations were extracted from a pre trained 3D CNN model. No filter activation will result in a zero ssim value.
The ssim function cannot be applied between matrix of different dimensions so I decided instead to pad the 25x25x25 image with zeros and get it to the same dimension.
I was succesful in converting the 25x25x25 into 255x255x299 which is almost the same dimension. I then used the above trick to get the 255 to 256 and I was able to achive 256x255x299. No matter what I tried, that middle 255 did not turn 256.
I suspect the problem here lies in the last line of the code.
% Here comes the "trick" to expand out the lower right corner with zeros.
P(m, n) = 0;
I want the 25x25x25 to be padded to 256x256x299. How do I do it and what adjustments need to be made.
Many thanks !
For a 3-D array you might be able to use the same trick.
P(256, 256, 299) = 0;
Or, if that doesn't work, you'd just have to use padarray on each plane and then reassemble into a 3-D array.
Hamza Mehmood
Hamza Mehmood am 27 Aug. 2022
Yeah that worked and results prove that padding the ROI to the feature activation dimension may not be the best way to compare using ssim.
Ill be instead reversing the process, cropping the 3D feature activation matrix down to 25x25x25 to ROI 3D coordinates and then compare using ssim.
Thank You !
Much Appreciated.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Hill
David Hill am 13 Mai 2021

1 Stimme

assuming m and n are both even.
M_resized=padarray(M,[(150-size(M,1))/2 (150-size(M,2))/2],0,'both');

1 Kommentar

Elysi Cochin
Elysi Cochin am 13 Mai 2021
Bearbeitet: Elysi Cochin am 13 Mai 2021
what if both are odd or any one value is odd
say 71 x 100
or 97 x 97
or 63 x 89

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by