From: Lukáš Jiřiště Date: Mon, 22 Jul 2024 07:11:11 +0000 (+0200) Subject: Refactor meter from the main script as a function X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=4b4ff1b045f3ab72613ea2ea0bcae05a5f770b7b;p=Bubble_column.git Refactor meter from the main script as a function --- diff --git a/Bubliny.m b/Bubliny.m index c3bec7f..d863af3 100755 --- a/Bubliny.m +++ b/Bubliny.m @@ -41,7 +41,7 @@ for iii=1:1:nSelectedDirectory % Cycles for directories with files % read names of all files FileType = '*.png'; % extension of image file - FileName=([DirectoryName '/' cell2mat(SelectedDirectory(iii)) '/' FileType]); + 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 @@ -93,129 +93,8 @@ for iii=1:1:nSelectedDirectory % Cycles for directories with files %% % Load the dimensions from the 1st image (p&q in pxl) if jjj == 1 - -%% -% Meter + [MeterScale, polypar_Metr] = meter(SelectedDirectory{iii}, ContrastFigure, ResFile); D_num=str2double(SelectedDirectory{iii}(21)); % position of camera - - reply = input('Is this a NEW EXPERIMENT that does not yet have calculated parameters for the cm scale? (y/n): ','s'); % y, n Selects where to find parameters for the cm scale. Either in the array or guess them. - if strcmp(reply,'n') - AxisIntensArray=[22,22,22,22,25,27,7,7,7,7,7,23,3,20,11,2,4,19,6,25,3]; % this needs to be guessed - AxisIntensUpArray=[60,60,65,65,34,5,29,29,29,29,29,22,26,2,27,2,28,2,29,2,29]; % this needs to be guessed - AxisEmptyArray=[190,190,190,190,200,190,190,190,190,190,190,190,190,190,190,190,190,190,190,190,190]; % this needs to be guessed 190 - LevelMeterArray=[0.95,0.95,0.95,1,0.11,0.8,0.2,0.2,0.2,0.1,0.29,0.9,0.39,0.9,0.39,0.6,0.29,0.9,0.25,0.7,0.27]; - AxisIntens=AxisIntensArray(Selected-13); % The number of all files minus 9 in which there is no data - AxisIntensUp=AxisIntensUpArray(Selected-13); - AxisEmpty=AxisEmptyArray(Selected-13); - LevelMeter=LevelMeterArray(Selected-13); - elseif strcmp(reply,'y') - AxisIntens=25; % 1.- 4. =22; 5. - 29 6.-27 7.8.9.11.11.-7 12.=23 13.=3 % Right this needs to be guessed - AxisIntensUp=2; % 3. = 65; 1. a 2. = 60; 5. - 34 6. a 7.8.9.10.11. = 29 12.=22 13.=26 % Left this needs to be guessed - AxisEmpty=190; % 5. = 200; 6. 7.8.9.10.11.12.13 = 190 % this needs to be guessed 190 - LevelMeter=0.7; % 5. = 0.11, 4. = 1; 1., 2. a 3. = 0.95, 6. =0.9; 7.8.9.=0.2 10.=0.1 11.=0.29 12.=0.9 13.=0.39 % number on the meter so that the left dash is above the surface and the right is below the surface - end - - Meter=ContrastFigure(floor(p*3/4-p*1/16+p*1/64):floor(p*3/4+p*1/64), 1:q); - ContrastMeter = imadjust(Meter); - [r,s]=size(ContrastMeter); - ContrastMeterMid=ContrastMeter(floor(end/2),:); - - hf1=figure ('Name','Contrasted meter','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); - imshow(ContrastMeter) - hold on - plot(ContrastMeterMid,'-m','linewidth',2) - hold on - - for kkk=2:1:q - if kkk<(1-LevelMeter)*q && ContrastMeterMid(kkk)>AxisIntensUp % looks for intensity minima for dashes on the axis above the level, Left - ContrastMeterMid(kkk)=0; - elseif kkk>=(1-LevelMeter)*q && ContrastMeterMid(kkk)>AxisIntens % looks for intensity minima for dashes on the axis under the level, Right - ContrastMeterMid(kkk)=0; - else - ContrastMeterMid(kkk)=1; - end - if ContrastMeterMid(kkk) == ContrastMeterMid(kkk-1) % looking for the right side of the peak - ContrastMeterMid(kkk-1)=0; - end - end - for mmm=q:-1:AxisEmpty - if ContrastMeterMid(mmm)==1 % erases the peaks (written numbers) to the left of the dashes on the axis - ContrastMeterMid((mmm-AxisEmpty):mmm-1)=0; - mmm=mmm-AxisEmpty; - end - end - ooo=1; - for nnn=1:1:q - if ContrastMeterMid(nnn)==1 % Writes x places of dashes on the axis - MeterScale(ooo)=nnn; - ooo=ooo+1; - end - end - - if D_num==1 - plot(MeterScale, floor(r/2) * ones(10, 1),'oc','linewidth',2) - elseif D_num==2 - plot(MeterScale(1:end-1), floor(r/2) * ones(8, 1),'oc','linewidth',2) - end -%-------------------------------------------------------------------------- - print ('-dpng', '-r1200', ['Contrasted_meter.png']) -%-------------------------------------------------------------------------- - - - hf2=figure ('Name','Intensity minima for dashes on the axis','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); - plot(ContrastMeterMid,'-m','linewidth',2) - - xlim([1,q]); - ylim([0,1]); -%-------------------------------------------------------------------------- - print ('-dpng', '-r1200', ['Intensity_places_dashes.png']) -%-------------------------------------------------------------------------- -clear Meter -clear ContrastMeter -clear ContrastMeterMid - -% Calculation of the actual position in mm using the scale of the meter - - hf3=figure ('Name','Dashes scale','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); - if D_num==1 - plot(MeterScale,[1000;900;800;700;600;500;400;300;200;100],'ob','linewidth',2) - hold on - polypar_Metr = polyfit(MeterScale,[1000;900;800;700;600;500;400;300;200;100],2); - elseif D_num==2 - plot(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],'ob','linewidth',2) - hold on - polypar_Metr = polyfit(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],2); - end - no_layer_fit=1: 1 : q; - Metr_fit=polyval(polypar_Metr,no_layer_fit); - plot(no_layer_fit,Metr_fit,'-r','linewidth',2); - hold on - if D_num==1 - xlim([0,q]); - ylim([0,1200]); - elseif D_num==2 - xlim([0,q]); - ylim([0,2000]); - end -%-------------------------------------------------------------------------- - print ('-dpng', '-r1200', 'Dashes_scale.png') -%-------------------------------------------------------------------------- - - writematrix("Contrasted meter", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range","A2"); %[cell2mat(SelectedDirectory(iii)) ': Meter scale'],'A1'); ['Meter scale ' num2str(iii)] - %%xlswritefig(hf1, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))] , 'A4') - writematrix("Intensity: places of dashes on the axis", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A6"); - %%xlswritefig(hf2, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'B8') - writematrix("Dashes scale", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "K6"); - %%xlswritefig(hf3, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'L8') - writematrix(["Meter Scale" "Dashes scale"], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A28"); - writematrix(["AxisIntens" "AxisIntensUp" "AxisEmpty" "LevelMeter"], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "D28"); - if D_num==1 - writematrix([MeterScale' [1000;900;800;700;600;500;400;300;200;100]], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A29"); - elseif D_num==2 - writematrix([MeterScale(1:end-1)' [1800;1700;1600;1500;1400;1300;1200;1100]], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A29"); - end - writematrix([AxisIntens AxisIntensUp AxisEmpty LevelMeter], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "D29"); - end %% % Figure with average intensity, without the bands with hole and meter @@ -446,6 +325,6 @@ clear ContrastMeterMid %-------------------------------------------------------------------------- - %%xlswritefig(hf4, ResFile, cell2mat(SelectedDirectory(iii)), 'J2') + %%xlswritefig(hf4, ResFile, SelectedDirectory{iii}, 'J2') end % iii, Cycles for directories with files diff --git a/meter.m b/meter.m new file mode 100644 index 0000000..08440d6 --- /dev/null +++ b/meter.m @@ -0,0 +1,123 @@ +function [MeterScale, polypar_Metr] = meter(SelectedDirectory, ContrastFigure, ResFile) + + [p, q] = size(ContrastFigure); +% Meter + D_num=str2double(SelectedDirectory(21)); % position of camera + + reply = input('Is this a NEW EXPERIMENT that does not yet have calculated parameters for the cm scale? (y/n): ','s'); % y, n Selects where to find parameters for the cm scale. Either in the array or guess them. + if strcmp(reply,'n') + AxisIntensArray=[22,22,22,22,25,27,7,7,7,7,7,23,3,20,11,2,4,19,6,25,3]; % this needs to be guessed + AxisIntensUpArray=[60,60,65,65,34,5,29,29,29,29,29,22,26,2,27,2,28,2,29,2,29]; % this needs to be guessed + AxisEmptyArray=[190,190,190,190,200,190,190,190,190,190,190,190,190,190,190,190,190,190,190,190,190]; % this needs to be guessed 190 + LevelMeterArray=[0.95,0.95,0.95,1,0.11,0.8,0.2,0.2,0.2,0.1,0.29,0.9,0.39,0.9,0.39,0.6,0.29,0.9,0.25,0.7,0.27]; + AxisIntens=AxisIntensArray(Selected-13); % The number of all files minus 9 in which there is no data + AxisIntensUp=AxisIntensUpArray(Selected-13); + AxisEmpty=AxisEmptyArray(Selected-13); + LevelMeter=LevelMeterArray(Selected-13); + elseif strcmp(reply,'y') + AxisIntens=25; % 1.- 4. =22; 5. - 29 6.-27 7.8.9.11.11.-7 12.=23 13.=3 % Right this needs to be guessed + AxisIntensUp=2; % 3. = 65; 1. a 2. = 60; 5. - 34 6. a 7.8.9.10.11. = 29 12.=22 13.=26 % Left this needs to be guessed + AxisEmpty=190; % 5. = 200; 6. 7.8.9.10.11.12.13 = 190 % this needs to be guessed 190 + LevelMeter=0.7; % 5. = 0.11, 4. = 1; 1., 2. a 3. = 0.95, 6. =0.9; 7.8.9.=0.2 10.=0.1 11.=0.29 12.=0.9 13.=0.39 % number on the meter so that the left dash is above the surface and the right is below the surface + end + + Meter=ContrastFigure(floor(p*3/4-p*1/16+p*1/64):floor(p*3/4+p*1/64), 1:q); + ContrastMeter = imadjust(Meter); + [r,s]=size(ContrastMeter); + ContrastMeterMid=ContrastMeter(floor(end/2),:); + + hf1=figure ('Name','Contrasted meter','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); + imshow(ContrastMeter) + hold on + plot(ContrastMeterMid,'-m','linewidth',2) + hold on + + for kkk=2:1:q + if kkk<(1-LevelMeter)*q && ContrastMeterMid(kkk)>AxisIntensUp % looks for intensity minima for dashes on the axis above the level, Left + ContrastMeterMid(kkk)=0; + elseif kkk>=(1-LevelMeter)*q && ContrastMeterMid(kkk)>AxisIntens % looks for intensity minima for dashes on the axis under the level, Right + ContrastMeterMid(kkk)=0; + else + ContrastMeterMid(kkk)=1; + end + if ContrastMeterMid(kkk) == ContrastMeterMid(kkk-1) % looking for the right side of the peak + ContrastMeterMid(kkk-1)=0; + end + end + for mmm=q:-1:AxisEmpty + if ContrastMeterMid(mmm)==1 % erases the peaks (written numbers) to the left of the dashes on the axis + ContrastMeterMid((mmm-AxisEmpty):mmm-1)=0; + mmm=mmm-AxisEmpty; + end + end + ooo=1; + for nnn=1:1:q + if ContrastMeterMid(nnn)==1 % Writes x places of dashes on the axis + MeterScale(ooo)=nnn; + ooo=ooo+1; + end + end + + if D_num==1 + plot(MeterScale, floor(r/2) * ones(10, 1),'oc','linewidth',2) + elseif D_num==2 + plot(MeterScale(1:end-1), floor(r/2) * ones(8, 1),'oc','linewidth',2) + end +%-------------------------------------------------------------------------- + print ('-dpng', '-r1200', ['Contrasted_meter.png']) +%-------------------------------------------------------------------------- + + + hf2=figure ('Name','Intensity minima for dashes on the axis','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); + plot(ContrastMeterMid,'-m','linewidth',2) + + xlim([1,q]); + ylim([0,1]); +%-------------------------------------------------------------------------- + print ('-dpng', '-r1200', ['Intensity_places_dashes.png']) +%-------------------------------------------------------------------------- +clear Meter +clear ContrastMeter +clear ContrastMeterMid + +% Calculation of the actual position in mm using the scale of the meter + + hf3=figure ('Name','Dashes scale','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters'); + if D_num==1 + plot(MeterScale,[1000;900;800;700;600;500;400;300;200;100],'ob','linewidth',2) + hold on + polypar_Metr = polyfit(MeterScale,[1000;900;800;700;600;500;400;300;200;100],2); + elseif D_num==2 + plot(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],'ob','linewidth',2) + hold on + polypar_Metr = polyfit(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],2); + end + no_layer_fit=1: 1 : q; + Metr_fit=polyval(polypar_Metr,no_layer_fit); + plot(no_layer_fit,Metr_fit,'-r','linewidth',2); + hold on + if D_num==1 + xlim([0,q]); + ylim([0,1200]); + elseif D_num==2 + xlim([0,q]); + ylim([0,2000]); + end +%-------------------------------------------------------------------------- + print ('-dpng', '-r1200', 'Dashes_scale.png') +%-------------------------------------------------------------------------- + + writematrix("Contrasted meter", ResFile, "Sheet", append("Met ", SelectedDirectory), "Range","A2"); %[cell2mat(SelectedDirectory(iii)) ': Meter scale'],'A1'); ['Meter scale ' num2str(iii)] + %%xlswritefig(hf1, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))] , 'A4') + writematrix("Intensity: places of dashes on the axis", ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "A6"); + %%xlswritefig(hf2, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'B8') + writematrix("Dashes scale", ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "K6"); + %%xlswritefig(hf3, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'L8') + writematrix(["Meter Scale" "Dashes scale"], ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "A28"); + writematrix(["AxisIntens" "AxisIntensUp" "AxisEmpty" "LevelMeter"], ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "D28"); + if D_num==1 + writematrix([MeterScale' [1000;900;800;700;600;500;400;300;200;100]], ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "A29"); + elseif D_num==2 + writematrix([MeterScale(1:end-1)' [1800;1700;1600;1500;1400;1300;1200;1100]], ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "A29"); + end + writematrix([AxisIntens AxisIntensUp AxisEmpty LevelMeter], ResFile, "Sheet", append("Met ", SelectedDirectory), "Range", "D29");