- use DIR, FILEPARTS, SPRINTF, and FULLFILE, or
- generate all filenames using SPRINTF and FULLFILE ?
How to input add in file names manually?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Tawsif Mostafiz
el 26 de Ag. de 2021
Comentada: Tawsif Mostafiz
el 27 de Ag. de 2021
I am using the following code to input all images from a folder:
% Specify the folder where the files live.
myFolder = 'C:\Users\user\Desktop\matlab\test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
imageArray = imread(fullFileName);
end
Now suppose, the image names are img1, img2, img3 and so on. I am running a segmentation algorithm, so I want to compare my segmeted masks (img1,img2,img3...) with the original masks (Like the image below) given in the database. I want to use DICE score for this.
I have the original masks given in the dataset in a different folder('C:\Users\user\Desktop\matlab\masks'). They are named like this, img1_mask, img2_mask, img3_mask and so on for each images. They look like this:
Now, I want to find DICE score for img1 and img1_mask, img2 and img2_mask and so on. How do I do this?
1 comentario
Stephen23
el 26 de Ag. de 2021
"How to input add in file names manually?"
Why do you want to do this manually? Wouldn't it be easier to just:
Respuesta aceptada
Image Analyst
el 26 de Ag. de 2021
Try this:
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
refFolder = 'C:\Users\TawsifMostafiz\Desktop\masks';
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
% Read in the test mask.
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
testMask = imread(fullFileName);
% Get the reference mask for this image.
[~, baseFileNameNoExt, ~] = fileparts(baseFileName);
refBaseFileName = sprintf('%s_mask.png', baseFileNameNoExt);
refFullFileName = fullfile(refFolder, refBaseFileName)
if ~isfile(refFullFileName)
errorMessage = sprintf('The mask for this image does not exist:\n%s', refFullFileName);
uiwait(errordlg(errorMessage));
continue; % Skip analysis of it because mask is missing.
end
refMask = imread(refFullFileName);
% Now compute dice
diceCoefficient = dice(testMask, refMask)
end
Más respuestas (1)
Ver también
Categorías
Más información sobre Language Support en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!