|
-
November 23rd, 2007, 12:03 AM
#1
Read & write multiple images by single execution(Matlab)?
Hi all,
I am new in Matlab.
Code:
for img=1:5
a=imread(['input\m',int2str(img),'.pgm']);
:
:
imwrite(I2,['result\m',int2str(img),'.pgm']);
end
My code can only run if i change the filenames to m1,m2......
Before this, my filename is m-001-1.pgm,.....,m-002-1.pgm,...,m-003-1.pgm....
Is there any way to make a loop to read this original filename?
I have done this in C by searching for m*.pgm, then all the files with "m" in front will be read. How about Matlab?
-
November 29th, 2007, 06:47 AM
#2
Re: Read & write multiple images by single execution(Matlab)?
 Originally Posted by tommy_chai
Hi all,
I am new in Matlab.
Code:
for img=1:5
a=imread(['input\m',int2str(img),'.pgm']);
:
:
imwrite(I2,['result\m',int2str(img),'.pgm']);
end
My code can only run if i change the filenames to m1,m2......
Before this, my filename is m-001-1.pgm,.....,m-002-1.pgm,...,m-003-1.pgm....
Is there any way to make a loop to read this original filename?
I have done this in C by searching for m*.pgm, then all the files with "m" in front will be read. How about Matlab?
Yes you can do this in matlab. Here is the code (in general):
Code:
dirName = 'MyDir';
D = dir(dirName); % check dir command, in matlab documentation
for (i=3:length(D)) % for each file in the directory (1 and 2 are '.' and '..')
if ( strcmp(D(i).name(end-3:end), '.pgm') == 1) % check if the extension of the current filename is '.pgm'
a=imread(D(i).name);
.......
end
end
Hope this helps u.
-
December 9th, 2007, 01:57 AM
#3
Re: Read & write multiple images by single execution(Matlab)?
Sorry I have some problem with my matlab and cannot the software cannot be used in this period. According to what i have searched, this is what i understand (as shown in below.
Do you mind to clear my doubts here even if i haven't try to run it in Matlab (waiting for the software and computer to be fixed)?
for() is used to loop all the files inside the directory, right?
Is it ust an example that u start with "3" or any other meanings? Normally we start with "1" like for(i=1:length(D). I am afraid that i miss out something and didn't get your real meaning.
I have checked the "end" fuction but i can't understand in "(end-3:end)", do you mind to educate me on this?
Code:
dirName = 'MyDir'; %name of the directory
D = dir(dirName); %command to check files in directory
for (i=3:length(D)) % a loop of how many file in such directory
if ( strcmp(D(i).name(end-3:end), '.pgm') == 1) % string compare
a=imread(D(i).name);
.......
end
end
These are my doubts. Thank you.
Best regards,
Tommy
-
December 10th, 2007, 06:08 AM
#4
Re: Read & write multiple images by single execution(Matlab)?
Hi!
 Originally Posted by tommy_chai
for() is used to loop all the files inside the directory, right?
Is it ust an example that u start with "3" or any other meanings? Normally we start with "1" like for(i=1:length(D). I am afraid that i miss out something and didn't get your real meaning.
Yes for() is used for looping through all files of the directory. We start at 3 because the 1st file of ANY directory is the '.' and the 2nd is the '..' directory, just like when you type "dir" or "ls" in Dos/Unix.
 Originally Posted by tommy_chai
I have checked the "end" fuction but i can't understand in "(end-3:end)", do you mind to educate me on this?
End is used in matlab at the end of commends, like if, while, for etc. Though, in the (end-3:end) it is used as matrix indexing: this means the 4 last elements of the array "D(i).name" (i.e. the current file name) are used and then compared to the .pgm extension.
Code:
...if ( strcmp(D(i).name(end-3:end), '.pgm') == 1)....
Hope this gave u a more detailed explanation.
Feel free to ask any more questions.
Regards,
Theodore
-
December 11th, 2007, 01:35 AM
#5
Re: Read & write multiple images by single execution(Matlab)?
 Originally Posted by yiannakop
End is used in matlab at the end of commends, like if, while, for etc. Though, in the (end-3:end) it is used as matrix indexing: this means the 4 last elements of the array "D(i).name" (i.e. the current file name) are used and then compared to the .pgm extension.
...if ( strcmp(D(i).name(end-3:end), '.pgm') == 1)....
Thanks for the explanation. Now i have a clearer view except for End.
(end-3:end)
Do you mean "D(i).name" is the 4 elements?
What does "end-3" means?
Let's say the filename is "a.pgm". Do you mean the ".pgm" is the 4 elements? So the function compares the extension ".pgm" only? Does it compare the filename, "a" in this example?
Thank you.
Best regards,
Tommy
-
December 11th, 2007, 05:07 AM
#6
Re: Read & write multiple images by single execution(Matlab)?
 Originally Posted by tommy_chai
T
(end-3:end)
Do you mean "D(i).name" is the 4 elements?
What does "end-3" means?
Let's say the filename is "a.pgm". Do you mean the ".pgm" is the 4 elements? So the function compares the extension ".pgm" only? Does it compare the filename, "a" in this example?
First of all, D(i).name is the whole file name. We want to compare the extension of each file name (i.e. of each D(i).name), because in the given folder you may have files of other types which you cannot read using imread. So in order to do this, we take the last 4 elements (i.e. the extension of the file). In general, Matlab has a very simple way of taking a subvector (or substring). Suppose you have a vector called str. By typing:
You take the elements 4 to 7 of the str array. In the same way, by typing:
Code:
...D(i).name(end-3:end)...
you take the elements N-3,N-2,N-1 and N of the string D(i).name, where N is the length of the array, which (only when used as above is given by the "end" keyword).
In any case, you should also read a simple introductory Matlab tutorial (google for it), in order to get the basic of Matlab. For example:
http://www.mathworks.com/academia/st...ter/tutorials/
Regards,
Theodore
-
December 11th, 2007, 10:40 PM
#7
Re: Read & write multiple images by single execution(Matlab)?
Sorry, if i caused any misunderstanding from my previous questions. Yup, i know D(i).name is the name of the file and a:b is from "a" part of the string to "b" part. Actually, what i want to ask before is why start with end-3?
Now i understand, N-3, N-2, N-1, N is the 4 elements from the filename to compare with ".pgm", right?
Yes, you are right there might be other file extensions as well in the same directory.
If my explantion above is correct, i guess i have no more doubts but to say thank you for your helps and explanations.
Thank you very much.
Best regards,
Tommy
-
December 12th, 2007, 08:28 AM
#8
Re: Read & write multiple images by single execution(Matlab)?
 Originally Posted by tommy_chai
Now i understand, N-3, N-2, N-1, N is the 4 elements from the filename to compare with ".pgm", right?
Yes, you are right there might be other file extensions as well in the same directory.
If my explantion above is correct, i guess i have no more doubts but to say thank you for your helps and explanations.
Yes you are right.
 Originally Posted by tommy_chai
If my explantion above is correct, i guess i have no more doubts but to say thank you for your helps and explanations.
Any time!
-
December 16th, 2007, 07:01 AM
#9
Re: Read & write multiple images by single execution(Matlab)?
Hi. I forgot to say that it is better to use strcmpi() instead of strcmp(), in order to ignore case differences (e.g. .PNG files could be found...)
Last edited by yiannakop; December 16th, 2007 at 07:07 AM.
-
December 16th, 2007, 11:59 PM
#10
Re: Read & write multiple images by single execution(Matlab)?
Ops, thanks for the suggestions. If you have experience in template matching using matlab, please contribute a bit under my post in General Developer Topics section. Thank you very much for what you have shared.
Best regards,
Tommy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|