How to input add in file names manually?

1 vue (au cours des 30 derniers jours)
Tawsif Mostafiz
Tawsif Mostafiz le 26 Août 2021
Commenté : Tawsif Mostafiz le 27 Août 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 commentaire
Stephen23
Stephen23 le 26 Août 2021
"How to input add in file names manually?"
Why do you want to do this manually? Wouldn't it be easier to just:
  • use DIR, FILEPARTS, SPRINTF, and FULLFILE, or
  • generate all filenames using SPRINTF and FULLFILE ?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 26 Août 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

Plus de réponses (1)

Tawsif Mostafiz
Tawsif Mostafiz le 26 Août 2021
@Stephen can you please elaborate?
If the masks ( the binary images of segmentations) are in folder C:\Users\TawsifMostafiz\Desktop\masks, I want to do in a way where, when img1.png is taken as input, it will also take input from the following location:
C:\Users\TawsifMostafiz\Desktop\masks\img1_mask.png

Catégories

En savoir plus sur Language Support dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by