Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sort multiple images

Subject: sort multiple images

From: Rajesh

Date: 16 Aug, 2010 05:39:05

Message: 1 of 8

Hey,
I have a set of images in a folder which i want to sort in ascending order so that i can read them one by one in the new order.

lets say directory= the path of directory where the images are located.

images=dir(fullfile(directory,'*.tif'));
[names index]=sortrows({images.name}');

after sorting the names in the images structure i get the following sequence

image1, image10, image100, image101, image102...image109, image11, image111, image112...image119, image12, image121.... etc
(I have 180 images in the directory. )

Question1: why is this happening when normal sorting in windows gives proper order of files image1,image2,image3,image4...etc.

secondly, If i do

[names index]=sortrows({images.date}');
(because the images are taken one after another)

still doesnt give desired sequence. This time i get
image1,image10,image11,image12,...image19,image2,image20,...image29,image3,image30...etc.

(this may be because i have copied this directory from its original location to my PC and now when i sort it using the 'date' field, it is taking the 'date of modification' and not that of 'creation'. thus when the date (and time) is the same it further sorts by name by default. )

Question2: Can i use the 'date of creation' instead of 'date of modification' as my sorting criteria? this date is available from windows.

thanks and regards...

Subject: sort multiple images

From: Malcolm McLean

Date: 16 Aug, 2010 07:07:08

Message: 2 of 8

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <i4aitp$gl2$1@fred.mathworks.com>...
>
> image1, image10, image100, image101, image102...image109, image11, image111, image112...image119, image12, image121.... etc
> (I have 180 images in the directory. )
>
> Question1: why is this happening when normal sorting in windows gives proper order of files image1,image2,image3,image4...etc.

You need a special routine to compare strings with embedded numbers. The Matlab routine simply compares blindly by ascii values.
My routine qsort() allows you to sort, in matlab, with an arbitrary comparision function. I'm also author of a C routine, compnumeric, which is available at Programmers' Heaven, and performs the comparision you want. Unfortunately I haven't transalted it to matlab.

Subject: sort multiple images

From: Rajesh

Date: 16 Aug, 2010 08:23:04

Message: 3 of 8

Hey MM
This looks like a great piece of work and it might help me but i have not been able to use qsort. Can you help.
[sorted, index]=qsort(vector,compfunc);

I am sending a structure as a 'vector' . for above problem it is 'image.name'.
(is it right?)
what is compfunc supposed to be. It is just to be sorted in ascending/descending order.
once again thanks a lot

Subject: sort multiple images

From: Shanmugam Kannappan

Date: 16 Aug, 2010 08:46:04

Message: 4 of 8

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <i4ash8$27h$1@fred.mathworks.com>...
> Hey MM
> This looks like a great piece of work and it might help me but i have not been able to use qsort. Can you help.
> [sorted, index]=qsort(vector,compfunc);
>
> I am sending a structure as a 'vector' . for above problem it is 'image.name'.
> (is it right?)
> what is compfunc supposed to be. It is just to be sorted in ascending/descending order.
> once again thanks a lot
Hi,
I understand the problem.
Btw,What's your aim?
If you want to read the file one by one,It seems the image names are uniform.
You know that how many images are present.
why dont you Just form the image name in loop & read it.
like:-
for i=1:180
imgName = ['image' num2str(i)]
I = imread(imgName);
% Process further
end
Its just a work around...
Shan!

Subject: sort multiple images

From: Rajesh

Date: 16 Aug, 2010 09:09:05

Message: 5 of 8

Shan,
the problem is i know only the directory location and not the file names. the name can be anything, one given here is just an example. I should have mentioned it before.
like file001, file002.....file180 OR object01, object02, ....object0180 etc.
thanks anyway.

Subject: sort multiple images

From: Malcolm McLean

Date: 16 Aug, 2010 13:55:12

Message: 6 of 8

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <i4ash8$27h$1@fred.mathworks.com>...
>
> I am sending a structure as a 'vector' . for above problem it is 'image.name'.
> (is it right?)
> what is compfunc supposed to be. It is just to be sorted in ascending/descending order.
> once again thanks a lot
>
Pass in in structure as a vector, compfunc is the handle to a routine that returns the comparison

answer = function comparestructures(a, b)
   % compare a.name and b.name by length
   if(length(a.name) < length(b.name))
     answer = -1;
  elseif(length(a.name) > length(b.name))
   answer = 1;
  else
    answer = 0;
end

sorted = qsort( structurevector, @comparestructures)


When you have a structure, there's no right way to compare. You might want to sort on one field, on a combination of fields, or on a field given by a foreign key, or even by something odder, as in my example, where ypu sort on the length of the name field.
Use of a comparision function allows you to use any rule for specifying order.

Subject: sort multiple images

From: Alan B

Date: 16 Aug, 2010 14:09:06

Message: 7 of 8

"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message <i4bg00$35j$1@fred.mathworks.com>...
> "Rajesh " <rv_acharya@rediffmail.com> wrote in message <i4ash8$27h$1@fred.mathworks.com>...
> >
> > I am sending a structure as a 'vector' . for above problem it is 'image.name'.
> > (is it right?)
> > what is compfunc supposed to be. It is just to be sorted in ascending/descending order.
> > once again thanks a lot
> >
> Pass in in structure as a vector, compfunc is the handle to a routine that returns the comparison
>
> answer = function comparestructures(a, b)
> % compare a.name and b.name by length
> if(length(a.name) < length(b.name))
> answer = -1;
> elseif(length(a.name) > length(b.name))
> answer = 1;
> else
> answer = 0;
> end
>
> sorted = qsort( structurevector, @comparestructures)
>
>
> When you have a structure, there's no right way to compare. You might want to sort on one field, on a combination of fields, or on a field given by a foreign key, or even by something odder, as in my example, where ypu sort on the length of the name field.
> Use of a comparision function allows you to use any rule for specifying order.

Two very simple solutions:
- write a script to rename each file in the directory with zero-padding, so basic string sorting works properly on filenames.
- sort by the datenum value of the Matlab dir struct, rather than the date string. The date field is another string, that's probably why you're seeing the same problem when you use that field. The datenum value is a scalar double, which will sort as you expect it to, if the files were created in the order you believe they were.

Subject: sort multiple images

From: Rajesh

Date: 18 Aug, 2010 05:36:26

Message: 8 of 8

Thanks folks heres the final code.

[sorted index]=qsort(images,@compfunc);

function compfunc(a,b)

filename1=a.name
filename2=b.name
[number1 st en]=regexp(filename1,'[0-9]','match');
[[number2 st en]=regexp(filename2,'[0-9]','match');

a=str2double[number1{:}]
b=str2double[number2{:}]

if a>b
ans=-1;
elseif a<b
ans=1
else
ans=0;
end

It worked. Thanks.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us