How to read all types images from the folder regardless of file extension ?

조회 수: 99 (최근 30일)
The below is my code
clear all
close all
clc
Directory = 'Images_folder\';
% Read images from Images folder
Imgs = dir(fullfile(Directory,'*.bmp'));
for j=1:length(Imgs)
Img = imread(fullfile(Directory,Imgs(j).name)); % Read image
Im = Img(:,:,1);
figure
imshow(Im)
end
From this code I can read only .bmp images.But I want to read all types of images from the folder regardless of the file extension.
  댓글 수: 6
Walter Roberson
Walter Roberson 2023년 4월 28일
img_dir = '\New folder (2)\';
That is an absolute directory name relative to the "current" drive (probably C:) .
It is likely that C:\New folder (2) does not exist.
pavani
pavani 2023년 9월 27일
Thank you. This code is working.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 31일
Directory = 'Images_folder';
% Read images from Images folder
Imgs = dir(Directory);
for j=1:length(Imgs)
thisname = Imgs(j).name;
thisfile = fullfile(Directory, thisname);
try
Img = imread(thisfile); % try to read image
Im = Img(:,:,1);
figure
imshow(Im)
title(thisname);
catch
end
end
Remember, image files are defined by their content, not by their extension, so the only sure way to determine if any given file can be read as an image is to try reading it as an image.
Myself, I would at the very least filter out directories before making the attempt, but I do not know Windows well enough to be certain that it would refuse to read a directory as an image.
This code is deficient in that it can only read the image types handled by MATLAB, not all types of images as you had asked for.
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 1월 19일
For multiple folders you can use
Directory = 'Images_parent_folder';
% Read images from Images parent folder
Imgs = dir( fullfile(Directory, '*') );
for j=1:length(Imgs)
thisname = Imgs(j).name;
thisfile = fullfile(Imgs(folder), thisname);
try
Img = imread(thisfile); % try to read image
Im = Img(:,:,1); % was so in original code
figure
imshow(Im)
title(thisname);
catch
end
end
Image Analyst
Image Analyst 2022년 1월 19일
Try this for multiple files and multiple folders under a top level folder.
topLevelFolder = pwd; %'C:\Images_parent_folder';
% Read images from top level folder and also subfolders of it.
Imgs = dir( fullfile(topLevelFolder, '**\*') ); % Use ** - special wildcard added a few years ago.
fprintf('Found %d files.\n', length(Imgs))
% Throw out directories
itsAFolder = [Imgs.isdir];
Imgs(itsAFolder) = [];
fprintf('After throwing out folders, there are now %d files.\n', length(Imgs))
for k = 1 : length(Imgs)
thisBaseFileName = Imgs(k).name;
thisFolder = Imgs(k).folder;
fullFileName = fullfile(thisFolder, thisBaseFileName);
try
fprintf('Reading %s\n', fullFileName)
% Img = imread(thisfile); % try to read image
% Im = Img(:,:,1); % was so in original code
% figure
% imshow(Im)
% title(thisname);
% drawnow;
catch
end
end

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

추가 답변 (3개)

KSSV
KSSV 2017년 10월 31일
Files=[dir('*.jpg');dir('*.png')]
specify all possible image extensions.
  댓글 수: 7
Thilak Babu Sayila
Thilak Babu Sayila 2019년 3월 21일
actually, total 20 images are in my folder, after executing the code 'Imgs' contains 22 contents and extra two are dummy (nothing is present). but i need exactly 20 images of various formats.
Walter Roberson
Walter Roberson 2019년 3월 21일
In my code, Imgs (capital I) will contain directory information for everything in the folder, including the . and .. folders. It is the loop with the try/catch that determines whether each entry is an image file that MATLAB is able to deal with, so you should record the filename information for those cases.
It is easier when there are a list of specific extensions you want to be able to handle, instead of wanting "everything that MATLAB turns out to be able to process" no matter how obscure.

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


Image Analyst
Image Analyst 2022년 1월 19일
For multiple file extensions in a single folder, try this:
fileList = [...
dir('*.jpg');
dir('*.jpeg');
dir('*.png');
dir('*.bmp');
dir('*.tif');
dir('*.dcm');
]
fileNames = sort({fileList.name}')

DGM
DGM 2023년 4월 28일
편집: DGM 2023년 4월 28일
If you just want to read everything into memory at once, then MIMT mimread() makes it simple.
% read all the images in my giant pile of forum garbage
% includes JPG, GIF, TIFF, PNG, BMP, ICO, MPO
% amongst hundreds of non-image files
% includes I, IA, RGB, RGBA, logical, indexed, and multiframe images
ST = mimread('forum_junk/*','quiet'); % one line
ST is a cell array containing all of the images.
  • If a JPG image has EXIF orientation data, it will be rectified.
  • Multiframe GIFs will be read as a multiframe 4D RGB image
  • Multi-image TIFF/CUR/ICO files will also be read in whole
  • If an image is indexed color, it is converted to RGB.
  • If an image has transparent content, it will have an attached alpha channel (IA/RGBA).
Is it a good idea to blindly load perhaps hundreds of images into memory simultaneously? Possibly not, but since the original question is about how to create an unmanageable forest of figure windows, I figured that good ideas weren't a requirement.
Jokes aside, mimread() isn't the most flexible thing to use if you want to have a lot of control over managing the path expression or if you have a bunch of elaborate TIFFs or something. It's also not useful if you want to incrementally process the files one at a time. It's intended to be a simple-to-use tool for ad-hoc use. You just point it at a directory or a bunch of easily-isolated filenames and grab them.

카테고리

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