- use DIR, FILEPARTS, SPRINTF, and FULLFILE, or
- generate all filenames using SPRINTF and FULLFILE ?
How to input add in file names manually?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2021-8-26
"How to input add in file names manually?"
Why do you want to do this manually? Wouldn't it be easier to just:
采纳的回答
Image Analyst
2021-8-26
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
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Language Support 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!