zero padding on several images

조회 수: 8 (최근 30일)
forgood2
forgood2 2021년 5월 7일
편집: DGM 2021년 5월 9일
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
forgood2
forgood2 2021년 5월 8일
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 2021년 5월 8일
Well that's fine, but what about the answers below???

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 5월 9일
편집: DGM 2021년 5월 9일
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.

추가 답변 (2개)

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

Jonas
Jonas 2021년 5월 7일
편집: Jonas 2021년 5월 7일
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
DGM
DGM 2021년 5월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by