Batch processing images from 2 folders and saving them to another folder

2 views (last 30 days)
Hello, I have two folders, Folder A and Folder B with unit8 binary images of same corresponding file names.
The folder structure is as follows:
Folder A
Image 1.bmp
Image 2.bmp
Image 3.bmp
...
Folder B
Image 1.bmp
Image 2.bmp
Image 3.bmp
...
I want to perform a simple operation imsubtract between the same filenames in the two folders. For example, (Image 1 from Folder A) - (Image 1 from Folder B). Similary (Image 2 from Folder A) - (Image 2 from Folder B), and so on. I want to batch process these, and save it to a different folder (e.g. Folder C). How would I go about doing this?
I have 400 images in Folder A and 400 images in Folder B with the same corresponding filenames. I want to batch export calculated 400 files (in for e.g. Folder C). Any help is very appreciated.

Accepted Answer

Image Analyst
Image Analyst on 12 Feb 2022
Edited: Image Analyst on 12 Feb 2022
Do you want floating point differences? Positive and negative values allowed? Or do you want the absolute difference of the subtraction, like you'd get with imabsdiff(). I'll show the later
foldera = 'C:/imagesa';
folderb = 'C:/imagesb';
folderc = 'C:/imagesc';
if ~isfolder(folderc)
mkdir(folderc);
end
fileListA = dir(fullfile(foldera, '*.bmp'))
fileListB = dir(fullfile(folderb, '*.bmp'))
for k = 1 : length(fileListB)
% Build file names.
fileNameA = fullfile(foldera, fileListA(k).name);
fileNameB = fullfile(folderb, fileListB(k).name);
% Read them in.
imageA = imread(fileNameA);
imageB = imread(fileNameB);
% Compute difference
diffImage = imabsdiff(imageA, imageB);
imshow(diffImage, []);
drawnow;
% Write out result to result folder: same base file name, different folder.
fileNameC = fullfile(folderc, fileListA(k).name);
imwrite(diffImage, fileNameC);
end
  2 Comments
Aero Descerazes
Aero Descerazes on 12 Feb 2022
Edited: Aero Descerazes on 12 Feb 2022
@Image Analyst Thanks for the response. Absolute values are fine. On line 11, it gave me an error: Unrecognized field name "Name". How do I fix this? I need the exported filename same as the input images.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!