Thread Subject: read array of images from a folder

Subject: read array of images from a folder

From: ennesai s

Date: 22 Nov, 2009 12:54:02

Message: 1 of 8

hi all,
     plz help me to find, how to read 20 images from a folder and compare an image from the folder with all other in that same folder.

Subject: read array of images from a folder

From: Dave Robinson

Date: 22 Nov, 2009 17:58:02

Message: 2 of 8

"ennesai s" <innisai19@gmail.com> wrote in message <hebc9a$n5g$1@fred.mathworks.com>...
> hi all,
> plz help me to find, how to read 20 images from a folder and compare an image from the folder with all other in that same folder.

Lots of different ways of getting the 20 file names into Matlab,

[Filenames,Pathname] = uigetfile('*.jpg', 'multiselect','on')

Allows the user to select the relevent folder and control or shift click on the images. This will provide a cell array containing the selected filenames, and the path or folder that the images are stored in.

You can then do something like

for imagenumber = 1:20
     CurrentFile = fullfile(Pathname,cellstr(Filenames(imagenumber));
     CurrentImage = imread(CurrentFile);
     YourComparisonFunction(CurrentImage);
end

Note I haven't tried the above code, it might need tweaking, Alternative you could use

help dir

With respect to image comparison, you need to define exactly what you are trying to compare.

1) Image similarity
2) Images being identical.

Image similarity again need to be defined, are we talking about similarity of colour distribution, similarity of subject, similarity of intensity distribution.

For example some people in the social science field would say that a picture of an inuit mother watching her children playing in the snow was identical to a bedouin mother watching her children playing in the sand. However no image processing system would agree that these images were in any way similar. Another example would be a close up image of an orange fruit would look similar in colour distribution to an image of the setting sun, based on colour and shape alone.

Come back with either samples of the images you are trying to compare, else a better definition of what you are trying to do here.

Regards

Dave Robinson

Subject: read array of images from a folder

From: Mark Hayworth

Date: 23 Nov, 2009 02:07:00

Message: 3 of 8

"ennesai s"
You can try MAGIC (MAtlab Generic Imaging Component). It's a framework (a template GUI) that handles the drudgery of selecting a folder, listing the images in a listbox, allowing you to select certain ones, displaying them, optionally masking them, batch processing your selected images, writing your results to Excel, etc. The only thing you have to do it to write your comparison function (I don't mean to imply that that is a trivial thing though). Like Dave said, that function is something only you can write. Just write it inside the MAGIC function called AnalyzeSingleImage(), and the UI will take care of itself.

Here's the link to MAGIC:
http://www.mathworks.com/matlabcentral/fileexchange/24224

Subject: read array of images from a folder

From: ennesai s

Date: 23 Nov, 2009 12:34:03

Message: 4 of 8

i have done this code
here i named my images as 1.jpg,2.jpg,....upto 40 images

for i=1:40
    s1=int2str(i);
    s2=strcat(s1,'.jpg');
    img=imread(s2);
end
now this is showing error like

1.jpg doesnot exist


y this error is coming..

Subject: read array of images from a folder

From: Dave Robinson

Date: 23 Nov, 2009 13:25:23

Message: 5 of 8

"ennesai s" <innisai19@gmail.com> wrote in message <hedvfr$6ob$1@fred.mathworks.com>...
> i have done this code
> here i named my images as 1.jpg,2.jpg,....upto 40 images
>
> for i=1:40
> s1=int2str(i);
> s2=strcat(s1,'.jpg');
> img=imread(s2);
> end
> now this is showing error like
>
> 1.jpg doesnot exist
>
>
> y this error is coming..

Are your images in the same directory as your source code for your application, or is the directory on your path - if not your program would not be able to find your images, and you would get the result you are seeing. You will need to append the path to your image file directory, so you would have something like
img = imread('c:\MyImageDirectory\1.jpg');

where MyImageDirectory represents the path to your image folder.

Check out

help fullfile

Hope that helps

Dave Robinson

Subject: read array of images from a folder

From: ImageAnalyst

Date: 23 Nov, 2009 14:24:47

Message: 6 of 8

On Nov 23, 7:34 am, "ennesai s" <innisa...@gmail.com> wrote:
> i have done this code
> here i named my images as 1.jpg,2.jpg,....upto 40 images
>
> for i=1:40
>     s1=int2str(i);
>     s2=strcat(s1,'.jpg');
>     img=imread(s2);
> end
> now this is showing error  like
>
> 1.jpg doesnot exist
>
> y this error is coming..

----------------
I always use sprintf() because, if you don't, sometimes you get
unexpected behavior with spaces like this:
s1 = 'abc ' % Note trailing space. Length should be 4.
length(s1) % You get 4 as expected.
s2 = strcat(s1, '567') % Produces 'abc567' not 'abc 567' as expected
% Prove that the space is gone!
length(s2) % You expect 7 but get 6. It ignored the space for some
reason.

so try this:
% Assign folder where the images will reside.
myFolder = mfilename('fullpath')
% Loop through creating filenames.
for k = 1:40
  baseFileName = sprintf('%d.jpg', k)
  fullFileName = fullfile(myFolder, baseFileName)
  % Now do something with fullFileName
end

Also note I changed your loop variable from i to k. Using i is not a
good idea since it also represents the imaginary constant.

Subject: read array of images from a folder

From: ennesai s

Date: 24 Nov, 2009 13:05:22

Message: 7 of 8

thanks for all sugpgestions..i found wat's gone wrong in mine..i didnt put program and images in a same folder. so it was not able to find wat is 1.jpg which i have given in my program...thank u so much..

Subject: read array of images from a folder

From: ImageAnalyst

Date: 24 Nov, 2009 14:08:04

Message: 8 of 8

On Nov 24, 8:05 am, "ennesai s" <innisa...@gmail.com> wrote:
> thanks for all sugpgestions..i found wat's gone wrong in mine..i didnt put program and images in a same folder. so it was not able to find wat is 1.jpg which i have given in my program...thank u so much..

----------------------------------------------------------------------
You're welcome. It's a common problem. That's why I always make sure
I have the FULL filename (folder + base file name). I always know
what folder my files are in and I make very frequent use of the
fullfile() and fileparts() functions. Not putting the full pathname
is dangerous and unpredictable as you found out.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
magic Mark Hayworth 22 Nov, 2009 21:09:04
matlab ennesai s 22 Nov, 2009 07:54:05
rssFeed for this Thread

Contact us at files@mathworks.com