How can I stack images from multiple folders?

3 views (last 30 days)
I got three images in three diffrent folders attached(1, Background. 2,Body 3, Expressions). I help stacking the images together from attached three diffrent folders. i would appreciate any help with the code.
imfuse will work to combine the imgaes from single folder. But, i need help pick the images from diffrent folders and stack it together as shwon here.

Accepted Answer

DGM
DGM on 11 Mar 2022
Edited: DGM on 11 Mar 2022
As far as I know, nothing in MATLAB or IPT directly supports images with alpha, though you can certainly tote alpha around as a separate mask image. Even at that, imfuse() isn't a composition tool and has no means to utilize a mask.
% read images and alpha
[expr,~,expra] = imread('Expressions/standard.png');
[body,~,bodya] = imread('Body/maroon.png');
BG = imread('Background/blue.png');
% convert to floating point for simplicity
expr = im2double(expr);
expra = im2double(expra);
body = im2double(body);
bodya = im2double(bodya);
BG = im2double(BG);
% compose the image as weighted sums, working from the bottom up
outpict = body.*bodya + BG.*(1-bodya);
outpict = expr.*expra + outpict.*(1-expra);
% convert back to original class
outpict = im2uint8(outpict);
imshow(outpict)
MIMT has tools that can do this more succinctly, either using the alpha as a separate mask:
[expr,~,expra] = imread('Expressions/standard.png');
[body,~,bodya] = imread('Body/maroon.png');
BG = imread('Background/blue.png');
outpict = replacepixels(body,BG,bodya);
outpict = replacepixels(expr,outpict,expra);
imshow(outpict)
... or by using alpha combined with the image (RGBA)
[expr,~,expra] = imread('Expressions/standard.png');
[body,~,bodya] = imread('Body/maroon.png');
BG = imread('Background/blue.png');
% these are now RGBA images
expr = cat(3,expr,expra);
body = cat(3,body,bodya);
% imblend and imshow2 handle RGBA images just fine
outpict = imblend(body,BG,1,'normal');
outpict = imblend(expr,outpict,1,'normal');
imshow2(outpict)
... or the images can be combined as a layer stack and composed in a single pass:
[expr,~,expra] = imread('Expressions/standard.png');
[body,~,bodya] = imread('Body/maroon.png');
BG = imread('Background/blue.png');
% these are now RGBA images
expr = cat(3,expr,expra);
body = cat(3,body,bodya);
% pad and stack on dim4
stack = imstacker({expr,body,BG});
% compose the entire stack in one shot
outpict = mergedown(stack,1,'normal');
imshow2(outpict)
Though the latter two cases only makes sense if an RGBA workflow is convenient for other reasons. As I said, nothing in MATLAB or IPT knows what to do with an RGBA image, so you'd constantly have to work around that.
  15 Comments
MS
MS on 14 Mar 2022
Edited: MS on 14 Mar 2022
@DGM The ouputs are saving as an image in the code below. i need to save the output as a movie. how to save the output as a movie/video? Please help me to change the code to create a movie.
outputfolder = './';
Sbg = dir('Background/*.png');
Sbdy = dir('Body/*.png');
Sexp = dir('Expressions/*.png');
for kbg = 1:numel(Sbg)
BG = imread(fullfile(Sbg(kbg).folder,Sbg(kbg).name));
for kbdy = 1:numel(Sbdy)
[body,~,bodya] = imread(fullfile(Sbdy(kbdy).folder,Sbdy(kbdy).name));
for kexp = 1:numel(Sexp)
[expr,~,expra] = imread(fullfile(Sexp(kexp).folder,Sexp(kexp).name));
% convert to floating point for simplicity
expr = im2double(expr);
expra = im2double(expra);
body = im2double(body);
bodya = im2double(bodya);
BG = im2double(BG);
% compose the image as weighted sums, working from the bottom up
outpict = body.*bodya + BG.*(1-bodya);
outpict = expr.*expra + outpict.*(1-expra);
% convert back to original class
outpict = im2uint8(outpict);
imshow(outpict)
newfn = {strrep(Sexp(kexp).name,'.png',''), ...
strrep(Sbdy(kbdy).name,'.png',''), ...
strrep(Sbg(kbg).name,'.png','')};
newfn = sprintf('%s + %s + %s.png',newfn{:});
imwrite(outpict,fullfile(outputfolder,newfn));
pause(0.2)
end
end
end
DGM
DGM on 14 Mar 2022
I'm not going to be much help with that, since the video encoding tools don't work in my environment. There are some examples in the documentation, and Image Analyst has posted a similar demo before:
The general workflow would be
Generate image sequence -> Convert image sequence to video

Sign in to comment.

More Answers (1)

Benjamin Thompson
Benjamin Thompson on 11 Mar 2022
montage or imtile should work for what you want.
  1 Comment
DGM
DGM on 11 Mar 2022
Neither imfuse(), montage(), nor imtile() support RGBA images or RGB images with a mask of any form.
Despite the wording of the documentation, I don't think it's reasonable to call these composition tools. The closest that any of these come to image composition is imfuse() with a 'blend' option, but that's only supported for I/RGB images, and it only supports 50% opacity with no mask. It's more of a crude comparison tool than anything.

Sign in to comment.

Categories

Find more on Convert Image Type 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!