resize images with zero padding

69 views (last 30 days)
Joseph Tabbah
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
RGB = imread('tree.jpg');%read first image
imshow(RGB)
RGB2 = imread('white horse.jpg');%read second image
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));
%using zero padding

Answers (2)

DGM
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!
padsz = 1×2
48.5000 32.5000
% pad leading and trailing edges separately to handle cases where pads must be unequal
Bpadded = padarray(B,ceil(padsz),0,'pre');
Bpadded = padarray(Bpadded,floor(padsz),0,'post');
size(Bpadded) % 256x256x1
ans = 1×2
256 256
imshow(Bpadded)
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.
A = imread('cameraman.tif'); % 256x256x1
B = imread('cell.tif'); % 159x191x1
C = imread('coins.png'); % 246x300x1
% 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);
padsza = (outsz-sza)/2;
padszb = (outsz-szb)/2;
padszc = (outsz-szc)/2;
% pad leading and trailing edges separately to handle cases where pads must be unequal
Apadded = padarray(A,ceil(padsza),0,'pre');
Apadded = padarray(Apadded,floor(padsza),0,'post');
Bpadded = padarray(B,ceil(padszb),0,'pre');
Bpadded = padarray(Bpadded,floor(padszb),0,'post');
Cpadded = padarray(C,ceil(padszc),0,'pre');
Cpadded = padarray(Cpadded,floor(padszc),0,'post');
montage({Apadded Bpadded Cpadded},'size',[1 3])
If you were using MIMT, you could do the same thing in one line.
A = imread('cameraman.tif'); % 256x256x1
B = imread('cell.tif'); % 159x191x1
C = imread('coins.png'); % 246x300x1
% ST is a 4D image containing all three frames, padded to the union of geometries
ST = imstacker({A B C},'padding',0);
% it can be addressed directly or tiled into a single frame
% ... or split into separate images as before
Apadded = ST(:,:,:,1);
Bpadded = ST(:,:,:,2);
Cpadded = ST(:,:,:,3);

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

Categories

Find more on Read, Write, and Modify Image 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!