Hi all, I'm super new to Matlab coding! I have the following code which was written by a labmate to do a particle thresholding algorithm for one jpg file, in this case "1.jpg." At the end of her script, I added a part that writes the percentage of certain pixels ("Cov") to a csv file. This works well for one file even though it is redundant in parts (multiple steps were combined). How do I repeat this particle thresholding routine automatically for n .jpg files in a folder, extract each Cov output (a floating point variable), then write each each Cov to a table with the 1st column being the number in the jpg filename (i.e., 1 in 1.jpg) and the 2nd column being the Cov? Then, how do I save all the Covs for each image into one master csv file?
filename = '1.jpg'; filename1 = strcat(filename,'_flagged.mat'); %It is your input filename appended with a _flagged.mat at the end. So, if your image is named image1.jpg, the results will be saved as image1.jpg_flagged.mat
red_min = 0; red_max = 1; green_min = 0; green_max = 1;blue_min = 0;blue_max = 1;%You can also iteratively choose color ranges. Save each intermediate result by, say, changing filename1
%to end with, "_flagged1.mat', '_flagged2.mat', etc, then load each of
%these and just add them together (since the particle flag is 1). Save their sum as
%'sample1_2xppl_compimage_flagged.mat' to be used in step2
a = imread(filename);a_red = a(:,:,1);a_green = a(:,:,2);a_blue = a(:,:,3);ind_flag = find( a_red >= red_min & a_red <= red_max & a_green >= green_min & a_green <= green_max & a_blue >= blue_min & a_blue <= blue_max );flagged = zeros(size(a_red));flagged(ind_flag) = 1; figureimage(uint8(cat(3,255*ones(size(flagged)),255*(ones(size(flagged))-flagged),255*(ones(size(flagged))-flagged))))figureimage(uint8(a))save(filename1,'flagged')load '1.jpg_flagged.mat' %Make sure that this matches the sample you want; Generated from step1.m
filename = '1_particles.mat'; %This is the mat-file the results will be saved under. Make sure this matches your sample
flagged = padarray(flagged,[1,1],0); %Pad the marked pixel array to avoid a different neighbor index algorithm at the edges
ind = find(flagged > 0);flagged(ind) = Inf;particles = zeros(size(flagged));neighbors = zeros(size(flagged));[m,n] = size(flagged); %Get the numbers of rows and columns
neighbors_offset = [m,-m,1,-1,m+1,m-1,-m+1,-m-1];j = 1;while( min(size(find(isinf(flagged) == 1))) > 0 ) particles(ind(1,1)) = j; %Save index location
flagged(ind(1,1)) = 0; %Unmark pixel
neighbor_indices = bsxfun(@plus,ind(1,1),neighbors_offset); %Find the 8 neighbor pixels
neighbors(neighbor_indices) = 1; ind_neighbors = find(isinf(neighbors.*flagged) > 0); neighbors(neighbor_indices) = 0; while( min(size(ind_neighbors)) > 0 ) %While you have neighbors to check
particles(ind_neighbors) = j; flagged(ind_neighbors) = 0; neighbor_indices = bsxfun(@plus,ind_neighbors,neighbors_offset); %Find the 8 neighbors of each of the orignal neighbors
neighbors(neighbor_indices) = 1; ind_neighbors = find(isinf(neighbors.*flagged) > 0); neighbors(neighbor_indices) = 0; end neighbors = 0*neighbors; j = j+1; %Update the particle number
ind = find(flagged > 0); %Update the to-be-checked list
endparticles = particles(2:end-1,2:end-1); %Remove the padding
save(filename,'particles');figurepcolor(particles);shading interpload '1_particles.mat' %Generated from step2.m Make sure it matches the sample you're working withicl
filename = '1_particles_filtered.mat';filename1 = '1.jpg';particle_threshold = 2; %The minimum area, in pixels, to be considered a grain. Everything below this size is discarded.
num_particles_before = max(particles(:)); %Number of particles before filtering
num_particles_after = 0; %Number of particles after filtering
particles_after = zeros(size(particles)); %Storage for filtered particles
for j = 1:num_particles_before ind = find(particles == j); if( max(size(ind)) > particle_threshold ) num_particles_after = num_particles_after+1; particles_after(ind) = num_particles_after; end endsave(filename,'particles_after');ind = find(particles > 0);particles(ind) = 1;ind = find(particles_after > 0);particles_after(ind) = 1;figureimage(uint8(cat(3,255*ones(size(particles)),255*ones(size(particles))-255*particles,255*ones(size(particles))-255*particles)));figureimage(uint8(cat(3,255*ones(size(particles)),255*ones(size(particles))-255*particles_after,255*ones(size(particles))-255*particles_after)));figurea = imread(filename1);image(a)load '1_particles_filtered.mat' %Generated from step3.m
%Compute any relevant statistics
num_particles = max(particles_after(:)); %Total number of particles
size_of_particles = zeros(1,num_particles); %Size of each, in pixels
for j = 1:num_particles ind = find(particles_after == j); size_of_particles(1,j) = max(size(ind));endaverage_size = mean(size_of_particles);var_of_size = var(size_of_particles);percentage_of_slide = sum(size_of_particles)/prod(size(particles_after));Cov = 100 - 100*percentage_of_slide;fprintf('\n%s%i%s\n','There are ',num_particles,' total particles')fprintf('\n%s%.2f%s\n','The average size is ',average_size,' pixels')fprintf('\n%s%.2f%s%.2f\n','The size has variance ',var_of_size,' and standard deviation ',sqrt(var_of_size))fprintf('\n%s%.2f%s\n','Kerogen coverage: ',Cov,' % of the image.')str_f = sprintf('%0.4f',Cov);fprintf('%0.4f\n',Cov); x = Cov; fid = fopen('Output final kerogen percentages.csv','wt'); fprintf(fid,'%0.4f',Cov); fclose(fid);
I know I use something like this:
FileList = dir('Users/galaxz/Desktop/thresholding/*.jpg');N = size(FileList,1);disp(FileList);
for k = 1:N
% modified version of loop above for thresholding n .jpg files
end
But I'm not sure of the details. Thanks!
Best Answer