Skip to content

Instantly share code, notes, and snippets.

@Ortham
Created September 10, 2015 17:19
Show Gist options
  • Save Ortham/e35419164a204601a017 to your computer and use it in GitHub Desktop.
Save Ortham/e35419164a204601a017 to your computer and use it in GitHub Desktop.
Beam spot analysis Matlab script
function BSA_process_directory(path)
path = strcat(path, '\');
% Filter only camera PNGs.
files = dir(strcat(path, 'Frame_*.png'));
% Create a Gaussian filter. This will be used
% to blur the image slightly so that spots
% form contiguous regions which can be
% analysed.
G = fspecial('gaussian',[5 5],3);
output = table();
for file = files'
image = imread(strcat(path, file.name));
% Check for saturation.
if BSA_is_saturated(image);
disp(strcat('WARNING! ', file.name,...
' is saturated! ',...
'Relative intensities may be ',...
'inaccuate.'));
end;
image = rgb2gray(image);
image = imfilter(image, G, 'same');
% Create a mask for the image regions.
% Cutoff is 10% the difference between max
% and mean values.
lower_bound = single(max(max(image))...
- mean(mean(image))) / ...
(10 * 255);
bw = im2bw(image, lower_bound);
% Calculate the properties for each region.
stats = regionprops(bw, image, ...
'Centroid',...
'Area',...
'MeanIntensity',...
'PixelValues');
length = size(stats);
for i=1:length(1);
name_arr = strsplit(file.name, {'_','.'});
stats(i).FrameNo = name_arr(2);
stats(i).SumIntensity = sum(stats(i).PixelValues);
stats(i).SIError = sqrt(stats(i).Area * 0.5 ^ 2);
end;
stats = rmfield(stats,'PixelValues');
stats = orderfields(stats, [4,2,5,6,1,3]);
cells = struct2table(stats);
output = [ output; cells ];
end;
output.Properties.VariableNames = {...
'FrameNo',...
'Centroid',...
'SumIntensity',...
'SIError',...
'Area',...
'MeanIntensity'};
% Remove regions which comprise of less than
% 10000 pixels.
output( output.Area < 10000,:) = [];
writetable(output, strcat(path, 'maxima.csv'));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment