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.


採用された回答
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 件のコメント
@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.
DGM
2022 年 3 月 11 日
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.
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)
[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.
@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
2022 年 3 月 11 日
The prior example does create all (2x3x2 = 12) images. If you want them in a specific folder, you can modify the outputfolder variable.
MS
2022 年 3 月 12 日
@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
2022 年 3 月 12 日
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.
MS
2022 年 3 月 13 日
@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)
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.
@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
2022 年 3 月 13 日
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
@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
2022 年 3 月 14 日
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
その他の回答 (1 件)
Benjamin Thompson
2022 年 3 月 11 日
montage or imtile should work for what you want.
1 件のコメント
DGM
2022 年 3 月 11 日
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.
カテゴリ
ヘルプ センター および File Exchange で Convert Image Type についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
