Reading sequence of JPEG images from current directory

4 visualizaciones (últimos 30 días)
Ashish Bhatt
Ashish Bhatt el 21 de Dic. de 2011
Comentada: Image Analyst el 18 de Jul. de 2017
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!

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Dic. de 2011

Más respuestas (2)

Image Analyst
Image Analyst el 21 de Dic. de 2011
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 comentarios
Ashish Bhatt
Ashish Bhatt el 21 de Dic. de 2011
After making the changes as below:
dirOutput = dir(fullfile(fileFolder,'image*.jpeg'));
i still get the same error. However, if I input a .gif file, program works fine:
dirOutput = dir(fullfile(fileFolder,'image*.gif'));
Swapna Tekale
Swapna Tekale el 2 de Jul. de 2014
Editada: Swapna Tekale el 2 de Jul. de 2014
I have the same problem for image sequence. It is working for *.tif file but not for .jpg. Please help me.

Iniciar sesión para comentar.


Delowar Hossain
Delowar Hossain el 19 de Oct. de 2015
Editada: Walter Roberson el 19 de Oct. de 2015
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 comentarios
Nav Desh
Nav Desh el 18 de Jul. de 2017
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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by