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