merge images from diffrent folder

Hi,I need a code that merges 2 folders Jpg images that inside each of the folders 120 images,the format that I need to merge is on by on (1 from folder1 by 1 from folder2, 2 from folder 1 by 2 from folder 2, so on.) and pastes the merged images into a separate folder(new path).
Thank for help.

 Respuesta aceptada

Image Analyst
Image Analyst el 22 de Dic. de 2019
Editada: Image Analyst el 22 de Dic. de 2019
Try this, making adaptations as needed:
% Specify the folder where the files live.
folder1 = 'D:\data\class 1';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(folder1)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder1);
uiwait(warndlg(errorMessage));
return;
end
folder2 = 'D:\data\class 2';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(folder2)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder2);
uiwait(warndlg(errorMessage));
return;
end
% Make an output folder.
outputFolder = 'D:\data\merge';
if ~isfolder(outputFolder)
% If it doesn't exist, create it.
mkdir(outputFolder);
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern1 = fullfile(folder1, '*.jpg'); % Change to whatever pattern you need.
filePattern2 = fullfile(folder2, '*.jpg'); % Change to whatever pattern you need.
theFiles1 = dir(filePattern1);
theFiles2 = dir(filePattern2);
% Go through stitching together all the images.
for k = 1 : length(theFiles1)
baseFileName1 = theFiles1(k).name;
[~, baseFileName1NoExt, ~] = fileparts(baseFileName1);
fullFileName1 = fullfile(folder1, baseFileName1);
fprintf('Now reading %s\n', fullFileName1);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
image1 = imread(fullFileName1);
subplot(1, 3, 1);
imshow(image1); % Display image.
% Now read in image2
baseFileName2 = theFiles2(k).name;
fullFileName2 = fullfile(folder2, baseFileName2);
[~, baseFileName2NoExt, ~] = fileparts(baseFileName2);
fprintf('Now reading %s\n', fullFileName2);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
image2 = imread(fullFileName2);
subplot(1, 3, 2);
imshow(image2); % Display image.
% Stitch the image together vertically:
[rows1, columns1, numberOfColorChannels1] = size(image1);
[rows2, columns2, numberOfColorChannels2] = size(image2);
if columns1 == columns2 && numberOfColorChannels1 == numberOfColorChannels2
tallImage = [image1; image2];
subplot(1, 3, 3);
imshow(tallImage); % Display image.
drawnow; % Force display to update immediately.
fprintf('And I stitched them together.\n\n');
outputBaseFileName = sprintf('%s and %s.png', baseFileName1NoExt, baseFileName1NoExt);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(tallImage, outputFullFileName);
fprintf('And I stitched them together and saved them as\n %s.\n\n', outputFullFileName);
else
% Columns or colors don't match - can't stitch together vertically.
warningMessage = sprintf('Cannot stitch together %s and %s because the columns or number of colors do not match.\n', ...
baseFileName1, baseFileName2);
promptMessage = sprintf('%s\nDo you want to Continue processing,\nor Quit processing?', warningMessage);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
break;
end
end
end

Más respuestas (4)

Image Analyst
Image Analyst el 22 de Dic. de 2019
Editada: Image Analyst el 22 de Dic. de 2019
See the FAQ. It's a simple process to adapt it to have two folders and two filenames. Then you can stitch the files together like this:
tallImage = [image1; image2]; % Must have same number of columns.
wideImage = [image1, image2]; % Must have same number of rows.
You can also use imageDatastore().

6 comentarios

Hamid Ebrahimi
Hamid Ebrahimi el 22 de Dic. de 2019
Thank you for answer but I can't properly understand the site that you mentioned,so can I ask you to give me a code here ?
OK, I adapted the FAQ for you but you still need to put in your actual folders.
% Specify the folder where the files live.
folder1 = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(folder1)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder1);
uiwait(warndlg(errorMessage));
return;
end
folder2 = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(folder2)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder2);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern1 = fullfile(folder1, '*.jpg'); % Change to whatever pattern you need.
filePattern2 = fullfile(folder2, '*.jpg'); % Change to whatever pattern you need.
theFiles1 = dir(filePattern1);
theFiles2 = dir(filePattern2);
% Go through stitching together all the images.
for k = 1 : length(theFiles1)
baseFileName1 = theFiles1(k).name;
fullFileName1 = fullfile(folder1, baseFileName1);
fprintf('Now reading %s\n', fullFileName1);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
image1 = imread(fullFileName1);
subplot(2, 2, 1);
imshow(image1); % Display image.
% Now read in image2
baseFileName2 = theFiles2(k).name;
fullFileName2 = fullfile(folder2, baseFileName2);
fprintf('Now reading %s\n', fullFileName2);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
image2 = imread(fullFileName2);
subplot(2, 2, 2);
imshow(image2); % Display image.
% Stitch the image together horizontally:
[rows1, columns1, numberOfColorChannels1] = size(image1);
[rows2, columns2, numberOfColorChannels2] = size(image2);
if rows1 == rows2 && numberOfColorChannels1 == numberOfColorChannels2
wideImage = [image1, image2];
subplot(2, 2, 3:4);
imshow(wideImage); % Display image.
drawnow; % Force display to update immediately.
fprintf('And I stitched them together.\n\n');
else
% Rows or colors don't match - can't stitch together horizontally
warningMessage = sprintf('Cannot stitch together %s and %s because the rows or number of colors do not match.\n', ...
baseFileName1, baseFileName2);
promptMessage = sprintf('%s\nDo you want to Continue processing,\nor Quit processing?', warningMessage);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
break;
end
end
end
Hamid Ebrahimi
Hamid Ebrahimi el 22 de Dic. de 2019
Editada: Hamid Ebrahimi el 22 de Dic. de 2019
Thank you again for answer, I ran the code but it give me only 1 picture like below image but I have 100 images in one path and 100 images in other path I want to give me 100 merge images that each images has a merge of two images below each other not 4 images and save them to seperate path.
It appears your input images are whole axes, with tick marks and color bars, etc., rather than just the images alone, but that's OK.
There are only 3 images there. The image at bottom is just the upper two stitched together side-by-side into a single image.
To dave the bottom image, call imwrite():
imwrite(wideImage, outputFullFileName);
You'll want to construct a unique filename for each output image using sprintf() and fullfile().
Hamid Ebrahimi
Hamid Ebrahimi el 22 de Dic. de 2019
Can I ask you to give me a full code with this changes and new path to save new merge images please
Thank tou so much for help.
Image Analyst
Image Analyst el 22 de Dic. de 2019
Give me the name of the three folders (two input and one output). And give me some kind of direction for what you'd like to name the output file. And is image1 in folder 1 supposed to be matched up with an image in folder 2 that has the same filename as image 1 had? If not, how are you deciding which to stitch together?

Iniciar sesión para comentar.

Hamid Ebrahimi
Hamid Ebrahimi el 22 de Dic. de 2019

0 votos

folder 1= has 100 images from 1 to 100
folder 2= has 100 images from 1 to1 00
path of folder 1= D:\data\class 1
path of folder 2= D:\data\class 2
path of folder 3= D:\data\merge (the output path of merge images form folder 1 and folder 2 one by one merge that should be has 100 images at end in output path taht has 100 images(1 with 1 merge 2 with 2 merge and...)
I will show you an example image that I want merge like this image.
image.jpeg
Hamid Ebrahimi
Hamid Ebrahimi el 22 de Dic. de 2019

0 votos

Your code worked and produced the following image, but they are not completely stuck together and are a little bit apart, is this the right distance between the images or not?
Thank you for your time and response.
It was so nice of you.
I have another question about showing the pre-processed signal and the raw signal and comparing them, should I open another post or ask here?
1 (1) and 1 (1).png

1 comentario

Image Analyst
Image Analyst el 22 de Dic. de 2019
They are completely touching on the edges. Like I said, your original images are not the images alone but the figures, so they have tick marks, axss labels, a title, a colorbar, and white space around them. Just look at one by itself in some other programto verify, so you can see that stuff really IS in the image - it's not something MATLAB added. You told it to save those images with all that extra stuff in them. If you don't want that, save them with imwrite(), not saveas().
I'd ask your image comparison question in a new question.

Iniciar sesión para comentar.

Hamid Ebrahimi
Hamid Ebrahimi el 18 de En. de 2020

0 votos

Hi again, for this problem I need merge images in only one image like below image and not two images in top and button and only one image.
stft.png

13 comentarios

Hamid, I'm not sure what an image is there. How many are there in that screenshot? Three? Do you have them in separate arrays? If so, simply stitch them together with the semicolon
tallMatrix = [image1; image2; image3];
Hamid Ebrahimi
Hamid Ebrahimi el 18 de En. de 2020
Editada: Hamid Ebrahimi el 18 de En. de 2020
I want to this image
come inside below image
like this
not like below image
Image Analyst
Image Analyst el 18 de En. de 2020
Yes, the code I gave should do that.
Hamid Ebrahimi
Hamid Ebrahimi el 18 de En. de 2020
thank you and how should I plot this tallmatix ?
Image Analyst
Image Analyst el 19 de En. de 2020
You don't "plot" images. You use imshow().
Hamid Ebrahimi
Hamid Ebrahimi el 19 de En. de 2020
Editada: Hamid Ebrahimi el 19 de En. de 2020
I use imshow() but gave me images like below, so I dont want like this I want two scalgram only in one images for example from 0to10 frequency one scalogram from 20to30 frequency one scalogram , not two scalogram images in one image
like below images that from 6to30 c3 and 6to30 cz and ... in one image(frequency-time).
stft.png
Image Analyst
Image Analyst el 19 de En. de 2020
Hamid, It seems that the problem you think you have is the white space (padding) around the axes. It looks like you're saving a whole figure as an image. You need to save the image, not the figure. Try using getimage() to get the image out of the axes, then use imwrite() or export_fig(), instead of saveas() or however you were saving the figure.
Hamid Ebrahimi
Hamid Ebrahimi el 19 de En. de 2020
I have one eeg signal and one nirs signal from 10s trial, I transform these signals to scalogram that for eeg(8-30 Hz and 10s) and for nirs (0.01-0.5 Hz and 10s) separately so I want to put each scalogram result under the other one into the image form. 0.01 to 0.5 nirs and 8 to 30 eeg in Y axis and 10s in X axis, I have to get one image by putting each one under another.
Image Analyst
Image Analyst el 19 de En. de 2020
So did you try getimage() and imwrite() or not?
Image Analyst
Image Analyst el 19 de En. de 2020
Please format the code properly (one line of code per line) with the code icon on the tool ribbon. I can't figure out how to split up the lines and what's in a comment and what's not. Also append the rest of your code so I can get it all in one click of the "Copy" link (which you'll get after you click the code icon).
Image Analyst
Image Analyst el 19 de En. de 2020
You're not making it easy, are you:
Unrecognized function or variable 'data1EEG'.
Error in test3 (line 7)
for i=1:size(data1EEG,3)
Hamid Ebrahimi
Hamid Ebrahimi el 19 de En. de 2020
Editada: Hamid Ebrahimi el 19 de En. de 2020
data1EEG is the preprocessing eeg data that should be transforme to scalogram image, so I want only a code that merge two scalogram in one image not two scalogram images in one image that previously you made, I want to merge two scalogram eeg signal and nirs sigbal in one image that has only one Y axis and one X axis not two images with two axis
Image Analyst
Image Analyst el 20 de En. de 2020
Well, I'm going to give up now. You're not turning over your data1EEG data for us to reproduce the scalograms. Plus you're calling cwtfilterbank() which is a function in the Wavelet Toolbox, which I don't have.
I suggest you
  1. start a new question/thread and
  2. specify that you're using the Wavelet Toolbox, and
  3. specify what release of MATLAB you're using, and
  4. attach your m-file, and
  5. attach your data file(s), and
  6. read this link
and maybe/hopefully someone with the Wavelet Toolbox can give you code for what you want to do.

Iniciar sesión para comentar.

Categorías

Más información sobre Introduction to Installation and Licensing en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Dic. de 2019

Comentada:

el 20 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by