how to read images form folders directory

hi, how to reasd images , say '*.png' images from folders in directory and produce output same as folder name.

 채택된 답변

Matt J
Matt J 2012년 10월 20일

0 개 추천

So each sub-folder contains only 1 file (a .png file)?
The code below puts the results in the structure Images.foldername
D = dir(directory_name);
D(1:2)=[];
D(~D.isdir)=[];
for i=1:length(D)
foldername=D(i).name;
pth=[directory_name pathsep foldername]);
files=dir(pth);
files(files.isdir)=[];
for j=1:length(files)
[p,n,e]=fileparts(files.name(j));
if strcmp(e,'.png')
Images.(foldername)=imread(files.name(j));
break;
end
end
end

댓글 수: 11

Tulips
Tulips 2012년 10월 20일
ermmm, more than 0ne image. what is the meaning of pathsep? [p,n,e] represents which output?thanks for spending your time making the code. cheers T
Matt J
Matt J 2012년 10월 20일
PATHSEP is a command which generates the symbol '/' or '\' used by your OS to specify directory paths.
As for [p,n,e] see the help documentation for the FILEPARTS command for the best explanation.
Matt J
Matt J 2012년 10월 20일
If each folder contains more than 1 image, you cannot name both images with the folder name. Each image has to have a separate name.
Tulips
Tulips 2012년 10월 22일
Matt J, yesterday, I'd used this D(~D.isdir)=[]; syntax, but it doesn't work. why is that? pls advice. thanks.
You don't need to worry about that if you just ask for *.png files in the dir() command.
D = dir(fullfile(directory_name, '*.png'))
Tulips
Tulips 2012년 10월 22일
what abt this pth=[directory_name pathsep foldername]); and files(files.isdir)=[]; what is the meaning of isdir.files. does it correct syntax?
Image Analyst
Image Analyst 2012년 10월 22일
Like I said, you don't even need to worry about that. But in general, isdir is a flag stating whether the file is a folder (directory). And setting some element = to [] means that you are removing it from the array.
Tulips
Tulips 2012년 10월 22일
편집: Tulips 2012년 10월 22일
D = dir(directory_name);
D(1:2)=[];
D(~D.isdir)=[];
for i=1:length(D)
foldername=D(i).name;
pth=[directory_name pathsep foldername]);
files=dir(pth);
files(files.isdir)=[];
for j=1:length(files)
[p,n,e]=fileparts(files.name(j));
if strcmp(e,'.png')
Images.(foldername)=imread(files.name(j));
break;
end
end
end
sir can you give the correct syntax instead of the above code.
what is the correct syntax for isdir actually?many thanks!
Image Analyst
Image Analyst 2012년 10월 22일
편집: Image Analyst 2012년 10월 22일
I wouldn't even do it that way. I'd follow the FAQ. But perhaps I don't know what you mean by " produce output same as folder name". Can you explain exactly what does that mean? What is the output? Is it a variable, an Excel workbook, a new image file, a structure like Matt suggested, what??????? I have no idea.
Tulips
Tulips 2012년 10월 22일
In a folder there have 24 images which in *.png extension with each of them consists of 20x20 dimensions. for the recognition part, it recognize, like this Input is apple.png The output is 2.png '2.png' is referred to as one of 24 images that have been recognized. The problem is how can I create folders and subfolders that represents different type of fruit that each of them consist of 5 images inside it. So that, when the sytem recoznize (2.png) it shows that the output is referred to apple from the folder of different fruits I created. Anyone?
Image Analyst
Image Analyst 2012년 10월 22일
See comment in my answer.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 10월 22일
편집: Image Analyst 2012년 10월 22일

0 개 추천

You may be interested in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F which has several code examples.
OK, that's totally different. You need a function called "ClassifyFruit" that takes an image and then returns the class number 1-5 depending on whether it's an apple, banana, peach, etc. Then use sprintf() to create the folder name, and mkdir to create it if you don't have it. Then create the full filename with sprintf and use imwrite to copy the image to that folder.

댓글 수: 1

Tulips
Tulips 2012년 10월 29일
how to create a ClassifyFruit function.can u give hint or idea on this?

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

sawsan mahmood
sawsan mahmood 2016년 4월 21일

0 개 추천

hi , please I need help I have database of iris segmentation a folder contain 241 subfolders (named from 1 to 241) each of them contain 5 images (named from Img_1_1_1 to Img_1_1_5)and second subfolder's image named (Img_2_1_1 to Img_2_1_5) and so on ,please I need code to read the first image only from each subfolder and make same processing to them thanks

댓글 수: 1

Image Analyst
Image Analyst 2016년 4월 21일
See my attached code. Change the file pattern to Img_1_1_1 and it will work.

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

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

질문:

2012년 10월 20일

댓글:

2016년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by