Reading sequence of JPEG images from current directory

조회 수: 3 (최근 30일)
Ashish Bhatt
Ashish Bhatt 2011년 12월 21일
댓글: Image Analyst 2017년 7월 18일
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!

채택된 답변

Walter Roberson
Walter Roberson 2011년 12월 21일
Together with:
filename = sprintf('image%03d.jpeg', K);

추가 답변 (2개)

Image Analyst
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
Ashish Bhatt
Ashish Bhatt 2011년 12월 21일
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 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
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
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!

Translated by