Reading sequence of JPEG images from current directory
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to read JPEG images from current directory. Images are named sequentially like image000, image001 and so on. I gave it a try and ended up with following working code:
clc; clear;
str = '1820'; filename = 'image1820';
X11 = zeros(27648,150);
for i = 1:150
% read, convert to grayscale and resize the image
jpegimg = imread(filename,'jpeg');
img2d = rgb2gray(jpegimg); img2d = imresize(img2d,[144 192]);
% store image in an array
X11(:,i) = img2d(:);
% update string 'str'
str = str2double(str) + 1; str = num2str(str);
noofzeros = 4 - length(str); zerostr = '';
for j = 1:noofzeros
zerostr = strcat(zerostr,'0');
end
str = strcat(zerostr,str);
% update filename to be the next file in the folder
filename = strcat('image',str);
end
But this code look very crude. I believe there is more elegant and perhaps faster way of doing this. Can you please tell me that 'more elegant and faster' way?
I could have done this in the following way, but 'dir' doesn't seem to work with JPEG images:
fileFolder = fullfile('D:','My Documents');
dirOutput = dir(fullfile(fileFolder,'image*','jpeg'));
fileNames = {dirOutput.name}';
numFrames = numel(fileNames);
I = imread(fileNames{1});
% Preallocate the array
sequence = zeros([size(I) numFrames],class(I));
sequence(:,:,1) = I;
% Create image sequence array
for p = 2:numFrames
sequence(:,:,p) = imread(fileNames{p});
end
I get the following error when i run this code:
??? Index exceeds matrix dimensions.
Error in ==> image_to_vector at 18
I = imread(fileNames{1});
>>
which is due to the fact that 'dirOutput' is an empty structure:
>> dirOutput
dirOutput =
0x1 struct array with fields:
name
date
bytes
isdir
datenum
>>
Thanks!
0 个评论
采纳的回答
更多回答(2 个)
Image Analyst
2011-12-21
Try 'image*.jpg' in your dir function. It's probably looking for files with no extension whatsoever so if your files have extensions, it won't retrieve those.
And I don't think either of those codes is particularly elegant or robust. Bad variable names, changing the class of str, not checking file sizes before concatenating, using weird string constructs instead of the simpler sprintf() like Walter suggested, not checking on dirOutput before proceeding, etc. However you did okay at adding comments, which most people don't.
2 个评论
Swapna Tekale
2014-7-2
编辑:Swapna Tekale
2014-7-2
I have the same problem for image sequence. It is working for *.tif file but not for .jpg. Please help me.
Delowar Hossain
2015-10-19
编辑:Walter Roberson
2015-10-19
You can try this code
srcFiles = dir('C:\Users\coil-20-proc\*.jpeg'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\coil-20-proc\',srcFiles(i).name);
I = imread(filename);
figure, imshow(I);
end
3 个评论
Nav Desh
2017-7-18
srcFiles take the images in a manner as image1,image10,image100 likewise.it does take the images in sequencelike image1,image2,image3.. how to change that sequence? Thanks in advance.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!