How can I stack images from multiple folders?

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.

 Respuesta aceptada

DGM
DGM el 11 de Mzo. de 2022
Editada: DGM el 11 de Mzo. de 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 comentarios

MS
MS el 11 de Mzo. de 2022
Editada: MS el 11 de Mzo. de 2022
@DGM Thank you so much for the great code. is there a way to create loop with mutiple back grounds green and white seprately? one image wiht white back ground and another image withe blue back gound and so on. please make changes for creating images wiht mutiple back grounds.
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)
pause(0.2)
end
end
end
This will create a composition for every combination of expression,body, and background. I attached an expanded set of source images to demonstrate.
You'll have to decide what you want to do with the outputs. The current code just displays them and overwrites the prior result.
You can store them in a 4D array or a cell array, or you can write the results to disk.
MS
MS el 11 de Mzo. de 2022
Thanks a bunch. @DGM i want to save the results in the disk. Please change the line if possible. Also, i saw you changed the colur of the body and expressions. Please let me know how you chnaged the colur of the body and expressions with the code.
DGM
DGM el 11 de Mzo. de 2022
Editada: DGM el 11 de Mzo. de 2022
You'll have to decide how you want toformat the filenames.
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
I didn't use MATLAB to adjust the demo images. I just threw them in GIMP and adjusted them. If you need to adjust them in MATLAB, it's possible. MATLAB doesn't have any simple tools for adjusting color. You'll have to decompose the image and do a constrained hue rotation and recompose it again.
[body,~,bodya] = imread('Body/maroon.png');
bodyhsv = rgb2hsv(body);
H = bodyhsv(:,:,1);
H = mod(H-0.28,1);
bodyhsv(:,:,1) = H;
bodypurple = hsv2rgb(bodyhsv);
imshow(bodypurple)
imwrite(bodypurple,'purple.png','alpha',bodya)
Again, MIMT has tools that can make this trivial:
[body,~,bodya] = imread('Body/maroon.png');
bodypurple = imtweak(body,'hsl',[-0.28 1 1]);
imshow(bodypurple)
imwrite(bodypurple,'purple.png','alpha',bodya)
... but unless you need to do it programmatically, it's easier to just use an image editing program.
MS
MS el 11 de Mzo. de 2022
Editada: MS el 11 de Mzo. de 2022
@DGM Thank you for the suggestions. i will try to use image editing program for changing the colour. is it possible to create all the possible images (2*3*2 = 12) in the everthing folder in single code? Please disregard if it is complicated. Sorry for troubling you.
DGM
DGM el 11 de Mzo. de 2022
The prior example does create all (2x3x2 = 12) images. If you want them in a specific folder, you can modify the outputfolder variable.
MS
MS el 11 de Mzo. de 2022
Editada: MS el 11 de Mzo. de 2022
@DGM . Sorry, i was using the old data. i got all 12 images with your data.
@DGM i tried to add additional features to the image. i am getting an error for some reason. i am attaching the code and added data here for your refernce. Please change the error. Thanks in advance
outputfolder = './';
Sbg = dir('Background/*.png');
Sbdy = dir('Body/*.png');
Sexp = dir('Expressions/*.png');
Shand = dir('Hands/*.png');
Shead = dir('Head Gear/*.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));
for khand= 1:numel(Shand)
[hand,~,handa] = imread(fullfile(Shand(khand).folder,Shand(khand).name));
for khead = 1:numel(Shead)
[head,~,heada] = imread(fullfile(Shead(khead).folder,Shead(khead).name));
% convert to floating point for simplicity
head = im2double(head);
heada= im2double(heada);
hand = im2double(hand);
handa = im2double(handa);
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);
outpict = hand.*handa + outpict.*(1-handa);
outpict = head.*heada + outpict.*(1-heada);
% 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
end
end
DGM
DGM el 12 de Mzo. de 2022
What error are you getting? It runs for me, but the hat image has no alpha, so the composition is all messed up. I added alpha to the hat image and attached it.
Mind that as you add more parameters to the process, you'll have to adapt the output file naming scheme accordingly, otherwise you'll start overwriting files.
@DGM thank you for all the efforts. sorry i am not aware that i messed with the alpha values. is this the code to change the alpha value of an image transperent?
is it possible to add mutiple colurs(100,1000 or infinite) randomly for an image in matlab? i am creating a diffrent question in the community. Please help if possible.
alpha(filename,0)
DGM
DGM el 13 de Mzo. de 2022
Editada: DGM el 13 de Mzo. de 2022
I don't know where the image came from. I'm assuming maybe it had alpha at one time, but was later rewritten without the alpha data. That might happen if you opened it and wrote it using imread() without supplying an 'alpha' argument. Imread() and imwrite() don't pass alpha content unless explicitly told to do so.
I just recreated it in GIMP.
It depends what you mean by random colors and how you want to add them. That could mean incorporating colored shapes or noise of different types. Or maybe you're talking about creating multiple versions of one image by (e.g.) adjusting the hue/saturation.
MS
MS el 13 de Mzo. de 2022
Editada: MS el 13 de Mzo. de 2022
@DGM yes, i made changes in the Hat and forgot about alpha.
i would like to find the suitable/best colour for the squirrel. i am just curious to create mutiple versions of one image by adding color randomly in the loop. let's say, we are able to create 100 diffrent colors for the body and 100 diffrent colurs for the hand and so on. Then, we will create all possible squirrels (100*100*..). But, I have no idea how to code. i request you to help me with the code if possible. Thanks.
My new question in the community can be found here,
https://www.mathworks.com/matlabcentral/answers/1670304-is-it-possible-to-add-paint-multiple-colors-more-than-100-up-to-10000-to-an-rgba-image-in-matlab?s_tid=answers_rc1-2_p2_MLT
DGM
DGM el 13 de Mzo. de 2022
Regarding generating a number of modified images, I added some answers to your other post. If you want to create one particular color modification (you mentioned trying to find the best color), then you might try to manually do that either using immodify() or an image editing program.
If you want control over different regions of the image, it may get more complicated. For instance, you may want to rotate the hue only on the ears, but not the body. Something like that would require an extra masking step
MS
MS el 14 de Mzo. de 2022
Editada: MS el 14 de Mzo. de 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 el 14 de Mzo. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Benjamin Thompson
Benjamin Thompson el 11 de Mzo. de 2022

1 voto

montage or imtile should work for what you want.

1 comentario

DGM
DGM el 11 de Mzo. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

MS
el 11 de Mzo. de 2022

Comentada:

DGM
el 14 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by