From 366618f32633b78ce4df5d63fadf9b4ee83b8427 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 20 Aug 2024 14:03:37 +0200 Subject: [PATCH] Implement the prototype of the data analysis Also get rid of some pointless comments and commands. --- Bubliny.m | 78 ++++++++++++++++++++++++++------------------------ get_levels.m | 33 +++++++++++++++++++++ name_to_time.m | 3 ++ smooth_peaks.m | 26 +++++++++++++++++ 4 files changed, 102 insertions(+), 38 deletions(-) create mode 100644 get_levels.m create mode 100644 name_to_time.m create mode 100644 smooth_peaks.m diff --git a/Bubliny.m b/Bubliny.m index 89cb88a..29a0460 100755 --- a/Bubliny.m +++ b/Bubliny.m @@ -1,16 +1,15 @@ -close all; % deletes all figures whose handles are not hidden -clear all; % removes all variables from memory +close all; +clear all; %% vstupni data T:\00-Orvalho\Travnickova\Bubble column -% - -%% % User dialog for the data directory used for files DirectoryName = get_data_dir(); -%DirectoryPath='T:\00-Orvalho\Travnickova\Bubble column\'; +if DirectoryName == 0 + return +end DDirectory = dir(DirectoryName); % Loads directories or subdirectories to struct FFiles(1).name, FFiles(1).folder, FFiles(1).date - i s casem @@ -23,10 +22,10 @@ end list={DDirectory.name}; [Selected,OK] = listdlg('ListString',list,'ListSize',[200 500]); SelectedDirectory = list(Selected)'; -if isempty(SelectedDirectory) % handles the error, when no spreadsheet is selected - error('No experiment selected.') +if isempty(SelectedDirectory) + return end -nSelectedDirectory = numel(SelectedDirectory); % Counts directories +nSelectedDirectory = numel(SelectedDirectory); %{ [ResFile, res_path] = uiputfile('*.xlsm', 'Save results to'); @@ -37,36 +36,20 @@ ResFile = [res_path, ResFile]; %} % helper variables -infmt = 'hh:mm:ss.SS'; % Time format in file names -FileType = '*.png'; % extension of image file +FileType = '*.png'; -%% % load of all directories in cycles for iii=1:nSelectedDirectory % Cycles for directories with files - - -%% % read names of all files - FileName=([DirectoryName '/' SelectedDirectory{iii} '/' FileType]); FFiles = dir(FileName); % Loads files to struct FFiles(1).name, FFiles(1).folder, FFiles(1).date - i s casem - nFFiles = numel(FFiles); % Counts files - LevelNum=str2double(SelectedDirectory{iii}(7:9))*1000; -%% variable allocation - - Height = zeros(1,nFFiles); - Metr_fit_H = zeros(1,nFFiles); - Bottom = zeros(1,nFFiles); - Metr_fit_B = zeros(1,nFFiles); - -%% % Initiation from the first file ImageName = FFiles(1).name; - start_time = duration([(ImageName(8:9)) ':' (ImageName(10:11)) ':' (ImageName(12:16))],'Format',infmt); + start_time = name_to_time(FFiles(1).name); Figure = imread([FFiles(1).folder '/' ImageName]); - ContrastFigure = imadjust(Figure); % Adjust the contrast of the image using imadjust. - D_num=str2double(SelectedDirectory{iii}(21)); % position of camera + + %%D_num=str2double(SelectedDirectory{iii}(21)); % position of camera [image_width, image_height] = size(Figure); sumfig = zeros(image_width, image_height); @@ -74,17 +57,16 @@ for iii=1:nSelectedDirectory % Cycles for directories with files maxfig = Figure; % load of all files in cycles - step=1; - startFile= 1; %945; %200 (20. ) - endFile= numel(FFiles); %nFFiles; %1000; %325 (20.) - nFFiles=endFile; % Attention!!! + step = 10; + startFile = 1; %945; %200 (20. ) + endFile = numel(FFiles); %nFFiles; %1000; %325 (20.) wait_bar = waitbar(0, ''); set(gca(wait_bar).Title, 'Interpreter', 'none'); - for jjj=startFile:step:nFFiles % Cycles for figures files + for jjj=startFile:step:endFile % Cycles for figures files ImageName = FFiles(jjj).name; Figure = imread([FFiles(jjj).folder '/' ImageName]); - waitbar(jjj/nFFiles, wait_bar, ['Preproccessing images from meassurement ' num2str(iii) '/' num2str(nSelectedDirectory)]); + waitbar((jjj - startFile)/(endFile - startFile), wait_bar, ['Preproccessing images from meassurement ' num2str(iii) '/' num2str(nSelectedDirectory)]); sumfig = sumfig + double(Figure); minfig = min(minfig, Figure); @@ -93,9 +75,10 @@ for iii=1:nSelectedDirectory % Cycles for directories with files close(wait_bar); - meanfig = uint8(sumfig ./ nFFiles); + meanfig = uint8(sumfig / ((endFile - startFile) / step + 1)); changefig = maxfig - minfig; + %{ figure subplot(2,2,1); title('Pixel maxima'); @@ -109,13 +92,32 @@ for iii=1:nSelectedDirectory % Cycles for directories with files subplot(2,2,4); title('Difference between max and min'); imshow(changefig); + %} mask = get_mask(changefig, meanfig); figure - imshow(meanfig .* uint8(mask)); - figure plot(mean(double(meanfig), Weights = mask)); meter = get_meter(maxfig, mask); + [~, calm_level] = min(mean(double(meanfig), Weights = mask)); + + wait_bar = waitbar(0, ''); + set(gca(wait_bar).Title, 'Interpreter', 'none'); + intensity = []; + step = 10; + N = floor((endFile - startFile)/step) + 1; + for j = 1:N + file_ind = startFile + (j - 1) * step; + ImageName = FFiles(file_ind).name; + Figure = imread([FFiles(file_ind).folder '/' ImageName]); + waitbar(j/N, wait_bar, ['Proccessing images from meassurement ' num2str(iii) '/' num2str(nSelectedDirectory)]); + intensity = mean(double(Figure), Weights = mask); + [water(j), bubble_top(j), bubble_bot(j)] = get_levels(intensity); + end + close(wait_bar); + + figure + plot(meter([water; bubble_top; bubble_bot]')); + end % iii, Cycles for directories with files diff --git a/get_levels.m b/get_levels.m new file mode 100644 index 0000000..5327925 --- /dev/null +++ b/get_levels.m @@ -0,0 +1,33 @@ +function [water, bubble_top, bubble_bot] = get_levels(intensity, threshold, level_width) + if nargin < 2 + threshold = 125; + end + if nargin < 3 + level_width = 50; + end + + water = NaN; + bubble_top = NaN; + bubble_bot = NaN; + signal = intensity > threshold; + diff_sig = abs(signal(3:end) - signal(1:end-2)); + diff_sig = smooth_peaks(diff_sig, level_width); + indeces = (1:numel(diff_sig)) + 1; + features = indeces(diff_sig == 1); + features = [1 features numel(signal)]; + for i = 2:numel(features) + signal(features(i - 1):features(i)) = mean(signal(features(i - 1):features(i))) > 0.5; + end + features = features(2:end-1); + for i = 1:numel(features) + top_state = signal(features(i) - 1); + bot_state = signal(features(i) + 1); + if (top_state == 0 && bot_state == 1) + bubble_bot = features(i); + elseif (top_state == 1 && bot_state == 0) + bubble_top = features(i); + elseif (top_state == 1 && bot_state == 1) + water = features(i); + end + end +end diff --git a/name_to_time.m b/name_to_time.m new file mode 100644 index 0000000..19ac704 --- /dev/null +++ b/name_to_time.m @@ -0,0 +1,3 @@ +function time = name_to_time(name) + time = duration([(name(8:9)) ':' (name(10:11)) ':' (name(12:16))], 'Format', 'hh:mm:ss.SS'); +end diff --git a/smooth_peaks.m b/smooth_peaks.m new file mode 100644 index 0000000..8041832 --- /dev/null +++ b/smooth_peaks.m @@ -0,0 +1,26 @@ +function sig = smooth_peaks(sig, window) + i = 1; + while true + start_point = find(sig(i:end) == 1, 1) + i - 1; + if isempty(start_point) + break + end + end_point = find(sig(start_point:min(end, start_point + window)) == 1, 1, 'last') + start_point - 1; + if isempty(end_point) + break + end + while true + new_end = find(sig(end_point:min(end, end_point + window)) == 1, 1, 'last') + end_point - 1; + if new_end == end_point + break + end + end_point = new_end; + end + sig(start_point:end_point) = 0; + i = ceil(mean(start_point, end_point)); + if i == 1 + i = i + 1; + end + sig(i - 1) = 1; + end + -- 2.30.2