How can I label DICOM images for classification based on folder name

Cameron-Harris · Jun 3, 2021 · 2.3K views
Question
I want to carry out transfer learning using  AlexNet for a set of 128x128 DICOM images. It looks like on of the most efficient methods of storing and labeling images is using imageDatastore, however I don't think this supports DICOM formats, I can create the store with no errors using:     images = imageDatastore(fullfile(pwd,'Project_Segmentations'),... 'IncludeSubfolders',true,'FileExtensions','.dcm',... 'LabelSource','foldernames'); This correctly labels the DICOMs based on the name of the folder they are in, however, when I try to use this data for training I'm met with the error: Error using matlab.io.datastore.ImageDatastore/readimage (line 32) Error using ReadFcn @readDatastoreImage function handle for file Which makes sense given imread doesn't support DICOM images.   Ideally I want to keep the DICOM file format, is there anyway I can achieve this data labeling for DICOM images?
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Nov 20, 2025
Labeling DICOM images for classification based on folder names is a straightforward process. Here’s a high-level overview of how you can achieve this:
 
 
 
for k = 1 : numImages
    thisImage = dicomread(....
    outputFileName = strrep(lower(inputFileName), '.dcm', '.webp');
    imwrite(thisImage, outputFileName);
end
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!