- 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!