zero padding on several images

8 Ansichten (letzte 30 Tage)
forgood2
forgood2 am 7 Mai 2021
Bearbeitet: DGM am 9 Mai 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 Kommentare
forgood2
forgood2 am 8 Mai 2021
This is the way I read the Images
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.png');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);ad
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
Something I have to mention:
All the images have different sizes because every image was cropped different.
Suppose the image with the biggest size is (524x74x3). Now I want to do zero padding with all images that they have the size (524x524x3) at the end.
Image Analyst
Image Analyst am 8 Mai 2021
Well that's fine, but what about the answers below???

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 9 Mai 2021
Bearbeitet: DGM am 9 Mai 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.

Weitere Antworten (2)

Image Analyst
Image Analyst am 8 Mai 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 am 7 Mai 2021
Bearbeitet: Jonas am 7 Mai 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 Kommentar
DGM
DGM am 8 Mai 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by