Refactor meter from the main script as a function
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 22 Jul 2024 07:11:11 +0000 (09:11 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 22 Jul 2024 08:22:27 +0000 (10:22 +0200)
Bubliny.m
meter.m [new file with mode: 0644]

index c3bec7fdbc8277d07022a812e270eb414b3aad26..d863af3eb1b5a2aee731d70eb9adfb3f95f414bd 100755 (executable)
--- 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\r
 \r
        FileType = '*.png';                                                     % extension of image file\r
-       FileName=([DirectoryName '/' cell2mat(SelectedDirectory(iii)) '/' FileType]);\r
+       FileName=([DirectoryName '/' SelectedDirectory{iii} '/' FileType]);\r
        FFiles = dir(FileName);                                                 % Loads files to struct FFiles(1).name, FFiles(1).folder, FFiles(1).date - i s casem\r
        nFFiles = numel(FFiles);                                                % Counts files\r
 \r
@@ -93,129 +93,8 @@ for iii=1:1:nSelectedDirectory  % Cycles for directories with files
 %%\r
 % Load the dimensions from the 1st image (p&q in pxl)\r
                if jjj == 1\r
-\r
-%%\r
-% Meter\r
+                       [MeterScale, polypar_Metr] = meter(SelectedDirectory{iii}, ContrastFigure, ResFile);\r
                        D_num=str2double(SelectedDirectory{iii}(21));                   % position of camera\r
-\r
-                       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.\r
-                       if strcmp(reply,'n')\r
-                               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\r
-                               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\r
-                               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\r
-                               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];\r
-                               AxisIntens=AxisIntensArray(Selected-13);                         % The number of all files minus 9 in which there is no data\r
-                               AxisIntensUp=AxisIntensUpArray(Selected-13);\r
-                               AxisEmpty=AxisEmptyArray(Selected-13);\r
-                               LevelMeter=LevelMeterArray(Selected-13);\r
-                       elseif strcmp(reply,'y')\r
-                               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\r
-                               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\r
-                               AxisEmpty=190;         % 5. = 200; 6. 7.8.9.10.11.12.13 = 190                                       % this needs to be guessed 190\r
-                               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\r
-                       end\r
-\r
-                       Meter=ContrastFigure(floor(p*3/4-p*1/16+p*1/64):floor(p*3/4+p*1/64), 1:q);\r
-                       ContrastMeter = imadjust(Meter);\r
-                       [r,s]=size(ContrastMeter);\r
-                       ContrastMeterMid=ContrastMeter(floor(end/2),:);\r
-\r
-                       hf1=figure ('Name','Contrasted meter','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters');\r
-                       imshow(ContrastMeter)\r
-                       hold on\r
-                       plot(ContrastMeterMid,'-m','linewidth',2)\r
-                       hold on\r
-\r
-                       for kkk=2:1:q\r
-                               if kkk<(1-LevelMeter)*q && ContrastMeterMid(kkk)>AxisIntensUp                         % looks for intensity minima for dashes on the axis above the level, Left\r
-                                       ContrastMeterMid(kkk)=0;\r
-                               elseif kkk>=(1-LevelMeter)*q  && ContrastMeterMid(kkk)>AxisIntens                     % looks for intensity minima for dashes on the axis under the level, Right\r
-                                       ContrastMeterMid(kkk)=0;\r
-                               else\r
-                                       ContrastMeterMid(kkk)=1;\r
-                               end\r
-                               if ContrastMeterMid(kkk) == ContrastMeterMid(kkk-1)         % looking for the right side of the peak\r
-                                       ContrastMeterMid(kkk-1)=0;\r
-                               end\r
-                       end\r
-                       for mmm=q:-1:AxisEmpty\r
-                               if ContrastMeterMid(mmm)==1                                 % erases the peaks (written numbers) to the left of the dashes on the axis\r
-                                       ContrastMeterMid((mmm-AxisEmpty):mmm-1)=0;\r
-                                       mmm=mmm-AxisEmpty;\r
-                               end\r
-                       end\r
-                       ooo=1;\r
-                       for nnn=1:1:q\r
-                               if ContrastMeterMid(nnn)==1                                 % Writes x places of dashes on the axis\r
-                                       MeterScale(ooo)=nnn;\r
-                                       ooo=ooo+1;\r
-                               end\r
-                       end\r
-\r
-                       if D_num==1\r
-                               plot(MeterScale, floor(r/2) * ones(10, 1),'oc','linewidth',2)\r
-                       elseif D_num==2\r
-                               plot(MeterScale(1:end-1), floor(r/2) * ones(8, 1),'oc','linewidth',2)\r
-                       end\r
-%--------------------------------------------------------------------------\r
-                       print ('-dpng', '-r1200', ['Contrasted_meter.png'])\r
-%--------------------------------------------------------------------------\r
-\r
-\r
-                       hf2=figure ('Name','Intensity minima for dashes on the axis','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters');\r
-                       plot(ContrastMeterMid,'-m','linewidth',2)\r
-\r
-                       xlim([1,q]);\r
-                       ylim([0,1]);\r
-%--------------------------------------------------------------------------\r
-                       print ('-dpng', '-r1200', ['Intensity_places_dashes.png'])\r
-%--------------------------------------------------------------------------\r
-clear Meter\r
-clear ContrastMeter\r
-clear ContrastMeterMid\r
-\r
-% Calculation of the actual position in mm using the scale of the meter\r
-\r
-                       hf3=figure ('Name','Dashes scale','NumberTitle','off','PaperSize',[8.5 6],'PaperUnits','centimeters');\r
-                       if D_num==1\r
-                               plot(MeterScale,[1000;900;800;700;600;500;400;300;200;100],'ob','linewidth',2)\r
-                               hold on\r
-                               polypar_Metr = polyfit(MeterScale,[1000;900;800;700;600;500;400;300;200;100],2);\r
-                       elseif D_num==2\r
-                               plot(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],'ob','linewidth',2)\r
-                               hold on\r
-                               polypar_Metr = polyfit(MeterScale(1:end-1),[1800;1700;1600;1500;1400;1300;1200;1100],2);\r
-                       end\r
-                       no_layer_fit=1: 1 : q;\r
-                       Metr_fit=polyval(polypar_Metr,no_layer_fit);\r
-                       plot(no_layer_fit,Metr_fit,'-r','linewidth',2);\r
-                       hold on\r
-                       if D_num==1\r
-                               xlim([0,q]);\r
-                               ylim([0,1200]);\r
-                       elseif D_num==2\r
-                               xlim([0,q]);\r
-                               ylim([0,2000]);\r
-                       end\r
-%--------------------------------------------------------------------------\r
-                       print ('-dpng', '-r1200', 'Dashes_scale.png')\r
-%--------------------------------------------------------------------------\r
-\r
-                       writematrix("Contrasted meter", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range","A2"); %[cell2mat(SelectedDirectory(iii)) ': Meter scale'],'A1'); ['Meter scale ' num2str(iii)]\r
-                       %%xlswritefig(hf1, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))] , 'A4')\r
-                       writematrix("Intensity: places of dashes on the axis", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A6");\r
-                       %%xlswritefig(hf2, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'B8')\r
-                       writematrix("Dashes scale", ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "K6");\r
-                       %%xlswritefig(hf3, ResFile, ['Met ' cell2mat(SelectedDirectory(iii))], 'L8')\r
-                       writematrix(["Meter Scale" "Dashes scale"], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A28");\r
-                       writematrix(["AxisIntens" "AxisIntensUp" "AxisEmpty" "LevelMeter"], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "D28");\r
-                       if D_num==1\r
-                               writematrix([MeterScale' [1000;900;800;700;600;500;400;300;200;100]], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A29");\r
-                       elseif D_num==2\r
-                               writematrix([MeterScale(1:end-1)' [1800;1700;1600;1500;1400;1300;1200;1100]], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "A29");\r
-                       end\r
-                       writematrix([AxisIntens AxisIntensUp AxisEmpty LevelMeter], ResFile, "Sheet", append("Met ", SelectedDirectory(iii)), "Range", "D29");\r
-\r
                end\r
 %%\r
 % Figure with average intensity, without the bands with hole and meter\r
@@ -446,6 +325,6 @@ clear ContrastMeterMid
 %--------------------------------------------------------------------------\r
 \r
 \r
-       %%xlswritefig(hf4, ResFile, cell2mat(SelectedDirectory(iii)), 'J2')\r
+       %%xlswritefig(hf4, ResFile, SelectedDirectory{iii}, 'J2')\r
 \r
 end  % iii, Cycles for directories with files\r
diff --git a/meter.m b/meter.m
new file mode 100644 (file)
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");