Reading Multiple Images from Folder

조회 수: 1 (최근 30일)
Chandra Shekhar
Chandra Shekhar 2013년 1월 25일
편집: Stephen23 2021년 4월 18일
I have a folder named 'ImageSet1',it consist of 20 images named as a 1.jpg,2.jpg...20.jpg.
Here i want to read images from 1 to 10 and then from 11 to 20 separately. it means, when i read image 1,immediately i have to read 11th image and when i read image 2,immediately i have to read 12th image in a loop and so on.. Here is the my code
sdirectory = 'ImageSet1';
jpegfiles = dir([sdirectory '/*.jpg']);
for k = 1:length(jpegfiles)/2
filename = [sdirectory '/' jpegfiles(k).name];
I = imread(filename);
figure;imshow(I)
filename1=[sdirectory '/' jpegfiles(10+k).name];
I1=imread(filename1);
figure;imshow(I1)
end
This code is not reading in order,like 1 and 11th image,2 and 12th image...
Does any one know please correct this code or any other method..?
  댓글 수: 2
Stephen23
Stephen23 2015년 5월 27일
편집: Stephen23 2015년 5월 27일
A simple solution is to use my FEX submission natsortfiles
It sorts according to any numeric values in the strings, and also sorts the file extensions separately:
B = {'test2.m'; 'test10-old.m'; 'test.m'; 'test10.m'; 'test1.m'};
sort(B) % wrong numeric order!
ans = {
'test.m'
'test1.m'
'test10-old.m'
'test10.m'
'test2.m'}
natsortfiles(B) % correct numeric order and shortest first:
ans = {
'test.m'
'test1.m'
'test2.m'
'test10.m'
'test10-old.m'}
Stephen23
Stephen23 2015년 5월 27일
편집: Stephen23 2021년 4월 18일
A simple solution is to use my FEX submission natsortfiles
>> S = dir('*.txt');
>> S.name
ans =
'1.txt'
ans =
'10.txt'
ans =
'2.txt'
>> S = natsortfiles(S); % alphanumeric sort by filename
>> S.name
ans =
'1.txt'
ans =
'2.txt'
ans =
'10.txt'

댓글을 달려면 로그인하십시오.

답변 (2개)

Evgeny Pr
Evgeny Pr 2013년 1월 25일
편집: Evgeny Pr 2013년 1월 25일
Use natural sorting for image filenames.
Listing = dir(fullfile(directoryPath, '*.jpg'));
names = {Listing.name}';
names = sort_nat(names);
fullNames = cellfun(@(x) fullfile(directoryPath, x), names, 'UniformOutput', 0);

azizullah khan
azizullah khan 2013년 11월 29일
dear sir kindly explain me names={listing.name} what does it means

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by