How to read all types images from the folder regardless of file extension ?
65 次查看(过去 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
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.
采纳的回答
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
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
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
2017-10-31
Files=[dir('*.jpg');dir('*.png')]
specify all possible image extensions.
7 个评论
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
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
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}')
0 个评论
DGM
2023-4-28
编辑:DGM
2024-7-5
% read all the images in my giant pile of forum garbage
% includes JPG, GIF, TIFF, PNG, BMP, ICO, MPO, WEBP
% 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
- Single-frame WEBP images are supported
- 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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!