baseFileName = 'doubleBeadSample.mat';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
rgbImage = s.doubleBeadSample;
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
fprintf('It is not really gray scale like we expected - it is color\n');
grayImage = rgbImage(:, :, 3);
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
g.WindowState = 'maximized';
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
highThreshold = intmax("int16")
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
xline(lowThreshold, 'Color', 'r', 'LineWidth', 2)
mask = imclearborder(mask);
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 2);
title('Mask, a Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
boundaries = bwboundaries(mask);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(x, y, 'r-', 'LineWidth', 2);
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
props = regionprops(mask, grayImage, 'Area', 'Centroid', 'WeightedCentroid');
centroids = vertcat(props.Centroid)
weightedCentroid = vertcat(props.WeightedCentroid)
plot(centroids(:, 1), centroids(:, 2), 'r.', 'MarkerSize', 50);
plot(centroids(:, 1), centroids(:, 2), 'y-', 'LineWidth', 4);
deltas = diff(centroids, 1)
distance = sqrt(deltas(1) .^ 2 + deltas(2) .^ 2)
message = sprintf('The distance between centroids is %.1f pixels', distance);
title(message, 'FontSize', fontSize);