I was trying the GLCM on MATLAB but I have to feed the image one by one each and that takes me forever. Is there anyway we can feed a large number of images, say 1,000 to the GLCM? How to write such loop?
Asked
Active
Viewed 248 times
1 Answers
1
If you have n images which follow a systematic naming scheme (image1.jpg,image2.jpg,...) then it is simple:
for k = 1 : n
image = imread(strcat('image',num2str(k),'.jpg'));
%do your GLCM analysis
end
If they have less well formatted names, but are all stored in the same folder, then you'd have to read them using something like as follows:
cd DIRECTORY_IMAGES_ARE_IN;
file_list = dir;
for k = 1 : n
image = imread(file_list(k).name);
%GLCM code
end
For the worst case scenario, where your files are in a directory mixed with other things, and have no sensible naming convention, you can iterate through them using some wildcards. dir
can take a single argument, which is the filename to look for. If you wanted to iterate over all the jpeg images in a directory, use file_list = dir('*.jpg');
, or if the files you wanted to analyse all had 'GCLM' somewhere in them, use file_list = dir('*GCLM*');

srthompers
- 169
- 8
-
Thank you very much. The image file names are actually formatted like this: EH446_20000_21000, EH446_21000_22000. – Anh Nguyen Jul 09 '15 at 20:47
-
@Anh In that case I'd recommend you use the second example I posted. It looks like all your files start with a stub 'EH446', so the second line would become `file_list = dir('EH446*');`. You could then iterate `for k =1:length(file_list)`, everything else can stay the same. If I've answered your question, don't forget to accept it as an answer! – srthompers Jul 10 '15 at 13:47
-
Thank you. But so with the image = imread(file_list(k).name) what should it be in for "name"? Also, after extracting the stats = graycoprops(GLCM,'all') for 8,779 images, how can I convert structure to a 8,779 x 8 matrix, i.e. 8 features extracted for 8,779 images? – Anh Nguyen Jul 10 '15 at 20:56
-
@ Anh This line `image = imread(file_list(k).name);` should stay exactly as it is - it's telling matlab to iterate through the 'name' field of the file data. To write the data into one big array, add the line `data = zeros(8779,8);` outside the loop to preallocate the array, and then copy your data to `data(k)` inside the loop. I can't give you exact code without knowing more about the output of `graycoprops`, but you should be able to work it out... – srthompers Jul 12 '15 at 20:39
-
Thanks for your help. I still have trouble making it to report the data as 8779 arrays. My codes are like this: for k=1:8779 image = imread(strcat('EH574Sub',num2str(k),'.jpeg')); I = rgb2gray(image); GLCM = graycomatrix(I,'Offset',[2 0;0 2]); stats = graycoprops(GLCM,'all') t = struct2array(stats) end – Anh Nguyen Jul 18 '15 at 02:13