conversionFactor = 0.021636;
fileList = dir(fullfile(folder, '*.tif'));
spreadingWidthArray = [];
for i = 1:numel(fileList)
baseFileName = fileList(i).name;
fullFileName = fullfile(folder, baseFileName);
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
rgbImage = imread(fullFileName);
grayImage = rgb2gray(rgbImage);
grayImage = imflatfield(grayImage, 6);
reducedContrastImage = imadjust(grayImage, [], [], gammaValue);
enhancedImage = adapthisteq(reducedContrastImage, 'clipLimit', 0.001, 'Distribution', 'rayleigh');
tophatFiltered = imtophat(enhancedImage, se);
bottomhatFiltered = imbothat(enhancedImage, se);
contrastEnhanced = imsubtract(imadd(enhancedImage, tophatFiltered), bottomhatFiltered);
binaryImage = ~imbinarize(contrastEnhanced, 'adaptive', 'ForegroundPolarity', 'dark', 'Sensitivity', 0.4);
binaryImage = bwareaopen(binaryImage, 50);
binaryImage = imclose(binaryImage, se);
edges = edge(contrastEnhanced, 'Canny', [0.9, 0.98]);
combinedImage = binaryImage * (1 - edgeWeight) + edges * edgeWeight;
finalBinaryImage = combinedImage > thresholdValue;
finalBinaryImage = imfill(finalBinaryImage, 'holes');
finalBinaryImage(775:end, :) = false;
finalBinaryImage = bwareafilt(finalBinaryImage, 1);
props = regionprops(finalBinaryImage, 'BoundingBox');
spreadingWidth = props.BoundingBox(3);
dropletHeight = props.BoundingBox(4);
spreadingWidthReal = spreadingWidth * conversionFactor;
dropletHeightReal = dropletHeight * conversionFactor;
spreadingWidthReal = round(spreadingWidthReal, 4);
dropletHeightReal = round(dropletHeightReal, 4);
spreadingWidthArray = [spreadingWidthArray, spreadingWidthReal];
dropletHeightArray = [dropletHeightArray, dropletHeightReal];
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 0.5);
title('Original with Bounding Box');
fprintf('Processed %s\n', baseFileName);
disp('Spreading Widths in Real Dimensions:');
fprintf('%.4f\n', spreadingWidthArray);
disp('Droplet Heights in Real Dimensions:');
fprintf('%.4f\n', dropletHeightArray);