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

100 vues (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 28 Avr 2023
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 le 27 Sep 2023
Thank you. This code is working.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Oct 2017
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 commentaires
Walter Roberson
Walter Roberson le 19 Jan 2022
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 le 19 Jan 2022
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

Connectez-vous pour commenter.

Plus de réponses (3)

KSSV
KSSV le 31 Oct 2017
Files=[dir('*.jpg');dir('*.png')]
specify all possible image extensions.
  7 commentaires
Thilak Babu Sayila
Thilak Babu Sayila le 21 Mar 2019
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 le 21 Mar 2019
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.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 19 Jan 2022
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 le 28 Avr 2023
Modifié(e) : DGM le 28 Avr 2023
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.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by