MATLAB: Merge images from diffrent folder

faqImage Processing Toolboxmerge images

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.

Best Answer

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