필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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.

채택된 답변

Image Analyst
Image Analyst 2022년 2월 12일
편집: Image Analyst 2022년 2월 12일
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
Aero Descerazes
Aero Descerazes 2022년 2월 12일
편집: Aero Descerazes 2022년 2월 12일
@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 2022년 2월 12일
Sorry. It should be lower case .name.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by