zero padding on several images
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (2 件)
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().
0 件のコメント
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
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 Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!