Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Image Analyst
Image Analyst le 12 Fév 2022
Modifié(e) : Image Analyst le 12 Fév 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 commentaires
Aero Descerazes
Aero Descerazes le 12 Fév 2022
Modifié(e) : Aero Descerazes le 12 Fév 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.
Image Analyst
Image Analyst le 12 Fév 2022
Sorry. It should be lower case .name.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by