hanhuark's picture
Add files using upload-large-folder tool
b90e704 verified
Raw
History Blame Contribute Delete
6.4 kB
%% CALCULATE BUBBLE STATISTICS FROM JSON DATA
%{
Description:
This script reads a JSON file containing manually annotated bubble contours.
It then calculates key bubble statistics for each image, aggregates the
results by heat load, and computes the mean and standard deviation for
each heat load condition.
Instructions:
0. Define the pixel resolution parameter (l_px) accordingly.
1. Run this script.
2. A dialog will prompt you to select the JSON data file.
3. A second dialog will prompt you to select the folder containing the
corresponding original images.
4. The script will process the data and display a results table in the
Command Window.
5. (Optional) Set `save_contour_Mode` to true to save annotated images.
Last modified: 2025/10/08
Author: Lige Zhang
Reference: International Journal of Heat and Mass Transfer 255 (2026): 127894.
%}
clear; close all; clc
format long
%% --------------------- USER SETTINGS ---------------------
% --- Set to true to save images with contours drawn on them ---
save_contour_Mode = true;
% --- Physical Constants ---
% l_px = 32.26e-3; % Pixel resolution in [mm/pixel] <- IMPORTANT: need to be updated accroding to the dataset!
l_px = 31.25e-3;
A_px = l_px^2; % Area of a single pixel in [mm^2/pixel]
%% --------------------- DATA LOADING ---------------------
[jsonFileName, jsonPath] = uigetfile('*.json', 'Select the annotated JSON file');
if isequal(jsonFileName, 0), disp('File selection cancelled.'); return; end
jsonFile = fullfile(jsonPath, jsonFileName);
imageFolderDIR = uigetdir(jsonPath, 'Select the folder containing the original images');
if isequal(imageFolderDIR, 0), disp('Folder selection cancelled.'); return; end
jsonText = fileread(jsonFile);
structData = jsondecode(jsonText);
imageDataNames = fieldnames(structData);
%% ----------------- BUBBLE STATISTICS CALCULATION -----------------
bubbleStat = struct;
disp('Processing images and calculating statistics...');
% --- Create the output directory for contour images ---
if save_contour_Mode == true
save_contour_DIR = fullfile(imageFolderDIR, "BubbleContours");
if ~exist(save_contour_DIR, 'dir'), mkdir(save_contour_DIR); end
end
for i = 1:length(imageDataNames)
currentImageKey = imageDataNames{i};
currentImageData = structData.(currentImageKey);
imageFileName = currentImageData.FileName;
heatLoadToken = regexp(imageFileName, '_(\d+(\.\d+)?W)_', 'tokens');
if isempty(heatLoadToken)
warning('Could not parse heat load from filename: %s. Skipping.', imageFileName);
continue;
end
heatLoadStr = heatLoadToken{1}{1};
heatLoadFieldName = matlab.lang.makeValidName(['load_' heatLoadStr]);
bubbles = currentImageData.Bubbles;
bubbleNames = fieldnames(bubbles);
numOfBubbles_i = length(bubbleNames);
if numOfBubbles_i == 0, continue; end
allBubbleAreas_i = zeros(numOfBubbles_i, 1);
for j = 1:numOfBubbles_i
bubbleName = bubbleNames{j};
x_coord = bubbles.(bubbleName).x_coordinate;
y_coord = bubbles.(bubbleName).y_coordinate;
allBubbleAreas_i(j) = polyarea(x_coord, y_coord) * A_px;
end
sumAreaBubbles_i = sum(allBubbleAreas_i);
avgBubbleArea_i = mean(allBubbleAreas_i);
avgBubbleRadius_i = sqrt(avgBubbleArea_i / pi);
Img_proc = imread(fullfile(imageFolderDIR, imageFileName));
imageTotalArea = size(Img_proc, 1) * size(Img_proc, 2) * A_px;
vaporAreaFraction_i = sumAreaBubbles_i / imageTotalArea;
if ~isfield(bubbleStat, heatLoadFieldName)
bubbleStat.(heatLoadFieldName).NumBubbles = [];
bubbleStat.(heatLoadFieldName).AvgArea = [];
bubbleStat.(heatLoadFieldName).AvgRadius = [];
bubbleStat.(heatLoadFieldName).VaporFraction = [];
end
bubbleStat.(heatLoadFieldName).NumBubbles(end+1) = numOfBubbles_i;
bubbleStat.(heatLoadFieldName).AvgArea(end+1) = avgBubbleArea_i;
bubbleStat.(heatLoadFieldName).AvgRadius(end+1) = avgBubbleRadius_i;
bubbleStat.(heatLoadFieldName).VaporFraction(end+1) = vaporAreaFraction_i;
%% --- CORRECTED: Save tightly-cropped image with sanitized filename ---
if save_contour_Mode == true
% Create a figure but keep it invisible for faster processing
fig = figure('Visible', 'off');
ax = axes(fig); % Create axes in the figure
imshow(Img_proc, 'Parent', ax);
hold(ax, 'on');
for j = 1:numOfBubbles_i
bubbleName = bubbleNames{j};
plot(ax, bubbles.(bubbleName).x_coordinate, bubbles.(bubbleName).y_coordinate, 'r--', 'LineWidth', 2);
end
hold(ax, 'off');
% --- FIX 1: Sanitize filename to handle '.' correctly ---
[~, base_name, ~] = fileparts(imageFileName);
sanitized_name = strrep(base_name, '.', 'd');
output_image_path = fullfile(save_contour_DIR, [sanitized_name, '.png']);
% --- FIX 2: Use exportgraphics to save without whitespace ---
exportgraphics(ax, output_image_path, 'Resolution', 150);
close(fig);
end
end
disp('Calculation finished. Summarizing results...');
%% ------------------- FINAL RESULTS SUMMARY -------------------
loadNames = fieldnames(bubbleStat);
numLoads = length(loadNames);
resultsData = zeros(numLoads, 9);
for k = 1:numLoads
loadName = loadNames{k};
loadValueStr = regexp(loadName, '\d+(\.?\d+)?', 'match');
loadValue = str2double(loadValueStr{1});
resultsData(k, :) = [
loadValue, ...
mean(bubbleStat.(loadName).NumBubbles), std(bubbleStat.(loadName).NumBubbles), ...
mean(bubbleStat.(loadName).AvgArea), std(bubbleStat.(loadName).AvgArea), ...
mean(bubbleStat.(loadName).AvgRadius), std(bubbleStat.(loadName).AvgRadius), ...
mean(bubbleStat.(loadName).VaporFraction), std(bubbleStat.(loadName).VaporFraction)
];
end
resultsData = sortrows(resultsData, 1);
resultsTable = array2table(resultsData, 'VariableNames', {
'HeatLoad_W', ...
'Mean_NumBubbles', 'StdDev_NumBubbles', ...
'Mean_AvgArea_mm2', 'StdDev_AvgArea_mm2', ...
'Mean_AvgRadius_mm', 'StdDev_AvgRadius_mm', ...
'Mean_VaporFraction', 'StdDev_VaporFraction'
});
disp(resultsTable);