# resize images with zero padding

17 views (last 30 days)
Joseph Tabbah on 23 Nov 2022
Edited: DGM on 23 Nov 2022
i'd like to make the second image the same size as the first with zero padding, i know how to do it in the other ways, if you can plz help
imshow(RGB)
imshow(RGB2)
I1 = rgb2gray(RGB);% we convert images to greyscale
figure
imshow(I1)
I2 = rgb2gray(RGB2);% we convert images to greyscale
figure
imshow(I2)
subplot(2, 1, 2);
F=fft2(I1);
S1=fftshift(log(1+abs(F)));% we convert first image to frequency domain
imshow(S1,[]);
%we found the centred specturm and applied the log transform in the same
%step
%this could also be done in 2 steps as follows:
%F1 = fft(I1);
% centered spectrum
%S= fftshift(F!);
% log transform
%S1 = log(1+abs(S));
subplot(2, 1, 2);
F2=fft2(I2);
S2=fftshift(log(1+abs(F2)));%convert second image to frequency domain
imshow(S2,[]);
%we can resize the image using imresize function or using zero padding
% I2 = imresize(I2, size(I1));

Image Analyst on 23 Nov 2022
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

DGM on 23 Nov 2022
Edited: DGM on 23 Nov 2022
There are some things to consider when using padarray() to match image geometries.
A = imread('cameraman.tif'); % 256x256x1 (even)
B = imread('cell.tif'); % 159x191x1 (odd)
% get geometry of both images (only the first 2 dims!)
sza = size(A); sza = sza(1:2);
szb = size(B); szb = szb(1:2);
% this example assumes that B is smaller than A in both dimensions!
% for general use, you'll need to check which dimensions are larger/smaller
% and resize/crop/pad either (or both) images as needed
padsz = (sza-szb)/2 % note that this is not necessarily integer-valued!
48.5000 32.5000
ans = 1×2
256 256
Let's say you had these three images you wanted to match. Note that no single image is largest. One solution would be to find the union of the geometries and pad all the images to fit that.
% get geometry of the images (only the first 2 dims!)
sza = size(A); sza = sza(1:2);
szb = size(B); szb = szb(1:2);
szc = size(C); szc = szc(1:2);
% assume we want to pad to the union of geometries
outsz = max([sza; szb; szc],[],1);
If you were using MIMT, you could do the same thing in one line.
% ST is a 4D image containing all three frames, padded to the union of geometries
% it can be addressed directly or tiled into a single frame
% ... or split into separate images as before

### Categories

Find more on Convert Image Type 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!