How to use different file format in MATLAB
Show older comments
Hi there,
I was using same file format (.tif) as input while processing with my following code. However, I have got new images with other formats like jpg, bmp.
I was previously using the below line and every time I need to change the extension manually in
M = dir(fullfile(imgPath,'*.tif'));
to work with different extension.
In most cases in one folder of the file extensions are different it only works with .tif and ignore others.
Any advice will be highly appreciated.
Accepted Answer
More Answers (1)
Image Analyst
on 21 Oct 2014
Try this snippet:
ImageFiles = dir([handles.imageFolder '\*.*']);
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder, name, ext] = fileparts(baseFileName);
ext = upper(ext);
if strcmpi(name, 'contours')
continue;
end
% Allow only tif, JPG, PNG, or BMP
if strcmpi(ext, '.TIF') == 1 || strcmpi(ext, '.JPG') == 1 || strcmpi(ext, '.TIF') == 1 if strcmp(ext, '.PNG') == 1
% if strcmp(baseFileName, '.') == 0 && strcmp(baseFileName, '..') == 0 && strcmp(baseFileName, 'Thumbs.db') == 0
ListOfImageNames = [ListOfImageNames baseFileName];
end
end
5 Comments
Siam
on 21 Oct 2014
Image Analyst
on 21 Oct 2014
handles.imageFolder is what you called imgPath. Look up fullfile in the help.
Guillaume and I both gave basically the same answer: get them all and keep the ones you want. You accepted his answer so I assume everything is working now. Alternatively you can just all dir a bunch of times and append them all.
% Determine where demo folder is (works with all versions).
folder = fileparts(which('cameraman.tif'));
tiffFiles = dir(fullfile(folder, '*.tif'))
pngFiles = dir(fullfile(folder, '*.png'))
jpgFiles = dir(fullfile(folder, '*.jpg'))
bmpFiles = dir(fullfile(folder, '*.bmp'))
allImageFiles = [tiffFiles; pngFiles; jpgFiles; bmpFiles]
Obviously if you already have a folder in variable imgPath, then you can just use imgPath everywhere I used folder, and skip the first line of the code above.
Siam
on 23 Oct 2014
Image Analyst
on 24 Oct 2014
You're welcome. As you figured out folder is just your directory, which you called imgPath, so it's
tiffFiles = dir(fullfile(imgPath, '*.tif'))
pngFiles = dir(fullfile(imgPath, '*.png'))
jpgFiles = dir(fullfile(imgPath, '*.jpg'))
bmpFiles = dir(fullfile(imgPath, '*.bmp'))
allImageFiles = [tiffFiles; pngFiles; jpgFiles; bmpFiles]
If you like the code, you can "Vote" for my answer.
Siam
on 11 Nov 2014
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!