필터 지우기
필터 지우기

How to use different file format in MATLAB

조회 수: 10 (최근 30일)
Siam
Siam 2014년 10월 21일
댓글: Siam 2014년 11월 11일
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.

채택된 답변

Guillaume
Guillaume 2014년 10월 21일
Just get all the files in the directory and keep only the ones with the extensions you want. You could even get the list of all valid image extension from imformats:
%option 1:
extensions = {'bmp', 'tif', jpg'}; %define allowed extensions yourself
%option 2:
registry = imformats; %get all image type supported by matlab
extensions = [registry.ext]; %and get their extensions
allfiles = dir(fullfile(somepath, '*.*'));
isimage = arrayfun(@(f) ~f.isdir && any(strcmp(regexp(f.name, '\.(.*)$', 'tokens', 'once'), extensions')), allfiles);
imagefiles = allfiles(isimage);
for imagefile = imagefiles'
Im = imread(fullfile(somepath, imagefile.name));
...
end
  댓글 수: 1
Siam
Siam 2014년 10월 21일
편집: Siam 2014년 10월 21일
Now I can process using the second option for my images and it is working.
Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 21일
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
Image Analyst
Image Analyst 2014년 10월 24일
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
Siam 2014년 11월 11일
Sorry for the delay Image Analyst. This one also works. Great help. Ty.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by