How can I stack images from multiple folders?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
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.
@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.
@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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
