MATLAB Answers

zero padding on several images

2 views (last 30 days)
forgood2
forgood2 on 7 May 2021
Edited: DGM on 9 May 2021
Hello,
I have several images with several sizes (155x75x3,512x74x3...) and I want to do a zero-padding that all images have the same size in order to colvolute them with a 3x3 filter. I know padarray() but I dont know how to do that all images will have the same size at the end.
Any help would be great!
  3 Comments
Image Analyst
Image Analyst on 8 May 2021
Well that's fine, but what about the answers below???

Sign in to comment.

Accepted Answer

DGM
DGM on 9 May 2021
Edited: DGM on 9 May 2021
The sensible answers have already been given. I'll just add the MIMT way of doing this.
instack = mimread('sources/birds*.jpg');
instack = imstacker(instack,'size',[500 500],'gravity','c','padding',0);
The result is a multiframe stack of all images, with each image centered and padded with black to fit a fixed geometry. You can use imfilter directly on this 4D array with whatever kernel you want:
filteredpict = imfilter(instack,fkgen('ring',20),'replicate');
Though like Image Analyst already pointed out, imfilter() already handles edge padding in a number of selectable ways, so manual padding isn't even necessary unless you need the images to share geometry for some other purpose.
Since you're reading PNG files (despite calling them jpegFiles), I'll note that unlike imread(), mimread() actually tries to preserve alpha content on import, and imstacker is greedy about array expansion, so you're likely to get an RGBA image when you're done. You can just strip off the fourth channel, or if you want to work with RGBA, you could set padding to [0 1] to specify that the padding has solid alpha. Imshow() doesn't know what to do with an RGBA or multiframe image, but imshow2() from MIMT does.
MIMT is a FEX thing:
This isn't exactly a serious suggestion, but I might as well throw it out there.

More Answers (2)

Image Analyst
Image Analyst on 8 May 2021
Why not simply use imfilter() and not worry about resizing? There are several edge handling options with imfilter() -- more than with conv2().

Jonas
Jonas on 7 May 2021
Edited: Jonas on 7 May 2021
if you want to use padarray, then use something like
currSize=size(currImg);
paddedImg=padarray(currImg,(aimSize-currSize)/2);
Watch out for differences in size that are not dividable by 2
  1 Comment
DGM
DGM on 8 May 2021
This is part of why I don't like dealing with padarray
currSize=size(currImg);
paddedImg=padarray(currImg,floor((aimSize-currSize)/2),0,'pre');
paddedImg=padarray(paddedImg,ceil((aimSize-currSize)/2),0,'post');
Most practical uses tend to require repeated calls for something so simple.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!