How to make the label of an image inside an imageDatastore, the file name?

조회 수: 10 (최근 30일)
Fergus O'Halloran
Fergus O'Halloran 2021년 5월 14일
답변: T.Nikhil kumar 2023년 10월 12일
Instead of having each image in a seperate folder to get the label name, I want to saave the file name as the label name. or just label each 1->number of images?
How can I do this?
Thanks

답변 (1개)

T.Nikhil kumar
T.Nikhil kumar 2023년 10월 12일
Hello Fergus,
As per my understanding you want to name each image in your ‘imageDatastore’ object with its file name without putting each image in a separate folder and setting the ‘LabelSource’ input argument as “foldernames”.
Assuming you have an image datastore imds containing the image files, you can use the split function to extract the file name from the full file path using a suitable delimiter. Then, you can assign the obtained file name as the label for each image.
The procedure for the task is as follows:
1. Take the full path of each image as the string to be split using the ‘split’ function.
2. Consider the “\” as delimiter and get the string split into a string array of smaller strings. We can choose the last string in that string array as file name and assign it as a label for the image Datastore using ‘Labels’ property. Refer to the following code for more understanding of the method:
%%Creating a cell array of each full file path split into smaller strings
allStringArray=split(imdsTrain.Files,"\");
%% Choosing the last string of such split strings as file name of the image
labelArray=allStringArray(:,end);
%% Assigning the labelArray to the Labels property of the image Datastore
imds.Labels=labelArray;
You can refer to the following documentation to know about the usage of ‘split’ function
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by