HaoShuang 5 gadi atpakaļ
vecāks
revīzija
3d45705174

+ 10 - 10
AutoDo/FormAutoDo.cs

@@ -48,12 +48,12 @@ namespace AutoDo
             int x = 0;
 
             double smax = LoGMean(files[0]);
-            int wdmax = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
 
             foreach (var file in files)
             {
                
-                int wd = int.Parse(file.Substring(file.Length - 8, 4));
+                int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
                 double svalue = LoGMean(file);
 
                 if (smax < svalue)
@@ -191,14 +191,14 @@ namespace AutoDo
 
 
             double smax = Tenengrad(files[0]);
-            int wdmax = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
 
 
             int x = 0;
             foreach (var file in files)
             {
                 
-                int wd = int.Parse(file.Substring(file.Length - 8, 4));
+                int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
                 double svalue = Tenengrad(file);
 
 
@@ -241,13 +241,13 @@ namespace AutoDo
 
  
             double smax1 = LoGMean(files[0]);
-            int wdmax1 = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
 
             double smax2 = Tenengrad(files[0]);
-            int wdmax2 = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax2 = wdmax1;
 
             double smax3 = TTgrad(files[0]);
-            int wdmax3 = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax3 = wdmax1;
 
 
 
@@ -255,7 +255,7 @@ namespace AutoDo
             foreach (var file in files)
             {
 
-                int wd = int.Parse(file.Substring(file.Length - 8, 4));
+                int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
                 double svalue1 = LoGMean(file);
                 double svalue2 = Tenengrad(file);
                 double svalue3 = TTgrad(file);
@@ -324,12 +324,12 @@ namespace AutoDo
             int x = 0;
 
             double smax = TTgrad(files[0]);
-            int wdmax = int.Parse(files[0].Substring(files[0].Length - 8, 4));
+            int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
 
             foreach (var file in files)
             {
 
-                int wd = int.Parse(file.Substring(file.Length - 8, 4));
+                int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
                 double svalue = TTgrad(file);
 
                 if (smax < svalue)

+ 1 - 1
ManulDo/MainForm.Designer.cs

@@ -635,7 +635,7 @@
             this.Controls.Add(this.groupBox2);
             this.Controls.Add(this.groupBox1);
             this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
-            this.Name = "MainForm";
+            //this.Name = "MainForm";
             this.Text = "测量流程测试";
             this.groupBox1.ResumeLayout(false);
             this.groupBox2.ResumeLayout(false);

+ 33 - 4
ManulDo/MainForm.cs

@@ -66,6 +66,10 @@ namespace WindowsFormsApp1
             if (m_work == WorkType.AutoFocus)
             {
                 //自动对焦
+                int nWd;
+                m_Ms.AutoFocus(m_focusParam, out nWd);
+                double wd = (double)nWd / 100.0;
+                tB_WD.Text = wd.ToString();
             }
             else
             {
@@ -317,12 +321,22 @@ namespace WindowsFormsApp1
                 return;
             }
             //保存自动对焦函数
-            m_focusParam.Path = tBImgPath.Text;
-            m_focusParam.UP = float.Parse(tB_Up.Text);
-            m_focusParam.Down = float.Parse(tB_Down.Text);
+            m_focusParam.Path = tBImgPath.Text;            
             m_focusParam.Step = float.Parse(tB_Step.Text);
             m_focusParam.Range = float.Parse(tB_fRange.Text);
-            m_focusParam.fStep = float.Parse(tB_fStep.Text);           
+            m_focusParam.fStep = float.Parse(tB_fStep.Text);
+
+            float fUP = float.Parse(tB_Up.Text);
+            float fDown = float.Parse(tB_Down.Text);
+
+            if (fDown <= fUP)
+            {
+                MessageBox.Show("请输入下限大于上限的值");
+                return;
+            }
+           
+            m_focusParam.UP = float.Parse(tB_Up.Text);
+            m_focusParam.Down = float.Parse(tB_Down.Text);            
         }
 
         private void tB_TextChanged(object sender, EventArgs e)
@@ -407,7 +421,22 @@ namespace WindowsFormsApp1
 
         private void tbAutoFocus_Click(object sender, EventArgs e)
         {
+            m_work = WorkType.AutoFocus;
             // 调用对焦线程
+            if (m_MeasureFile == null)
+            {
+                MessageBox.Show("请新建一个测量文件");
+                listmsg.Items.Clear();
+            }
+            else
+            {
+                if (m_BackgroundWorker.IsBusy)
+                {
+                    MessageBox.Show("线程已经运行");
+                    return;
+                }
+                m_BackgroundWorker.RunWorkerAsync(this);
+            }
         }
     }
 }

+ 22 - 0
ManulDo/ManulDo.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props" Condition="Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" />
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -13,6 +14,8 @@
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <Deterministic>true</Deterministic>
     <TargetFrameworkProfile />
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -34,6 +37,18 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.Blob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.Blob.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.Extensions.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.UserInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.UserInterface.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -68,6 +83,7 @@
       <DependentUpon>Resources.resx</DependentUpon>
       <DesignTime>True</DesignTime>
     </Compile>
+    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -100,4 +116,10 @@
     </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props'))" />
+  </Target>
 </Project>

+ 260 - 8
MeasureThread/Measure.cs

@@ -15,6 +15,8 @@ using SmartSEMControl;
 using MeasureData;
 using FileManager;
 
+using OpenCvSharp;
+
 namespace MeasureThread
 {
     //图片模式
@@ -468,6 +470,12 @@ namespace MeasureThread
         }
 
         public void SendMsg(string step_code)
+        {
+            arg.State = step_code;
+            SendThreadStatus(this, arg);
+        }
+
+        public void SendMsg1(string step_code)
         {
             arg.Step_Code = step_code;
             arg.Time = DateTime.Now;
@@ -1641,26 +1649,270 @@ namespace MeasureThread
                 return false;
             }
 
+            string path = a_param.Path;
+            if (!Directory.Exists(path))
+            {
+                return false;
+            }
+            string path_coarse = a_param.Path + "\\Coarse";
+            if (!Directory.Exists(path_coarse))
+            {
+                Directory.CreateDirectory(path_coarse);
+            }
+            string path_fine = a_param.Path + "\\Fine";
+            if (!Directory.Exists(path_fine))
+            {
+                Directory.CreateDirectory(path_fine);
+            }
+
+
             //1. 拍照
-            //1.1, 初拍照
+            //1.1, 拍照
             //1.1.1, 根据参数计算拍照次数
-            //int n = 
+            double nUp = a_param.UP * 100.0 ;
+            double nDown = a_param.Down * 100.0;
+            double nStep = a_param.Step * 100.0;
+            double nTimes = Math.Ceiling((nDown - nUp) / nStep + 0.5);
+
+            SendMsg("粗对焦:最小值" + nUp.ToString() + ",最大值" + nDown.ToString() + ",步长" + nStep.ToString() + "拍照张数" + nTimes.ToString());
 
-            //3.控制SEM拍照
-            String fileName4 = WorkingFolder + ImageName4;
-            if (!GetImage(ImageMode.SEM, fileName4))
+            //粗拍照
+            for (int i = 0; i < nTimes; i++)
             {
-                return false;
+
+                int cWd =(int) Math.Ceiling(nUp + i * nStep + 0.5);
+                iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
+                Thread.Sleep(3000);
+
+                int swd = cWd / 100;
+                SendMsg("wd = " + swd.ToString() +"um");
+
+                String fileName = path_coarse + "\\" + cWd.ToString() + ".tif";
+                if (!GetImage(ImageMode.SEM, fileName))
+                {
+                    return false;
+                }
+
+                Thread.Sleep(5000);
             }
 
             //1.2, 选定细拍照的工作距离
+            int CorWd;
+            ChooseBest(path_coarse,out CorWd);
+
+            a_nWd = CorWd;
+
+            SendMsg("粗选工作距离wd = " + a_nWd.ToString() );
+            
+
             //1.3, 细拍照
+            if ( (CorWd - a_param.Range * 100.0) <=0)
+            {
+                return false;
+            }
+
+            double nfUp = CorWd - a_param.Range * 100.0;
+            double nfStep = a_param.fStep * 100.0;
+            double nfTimes = Math.Ceiling((a_param.Range * 100.0 * 2) / nfStep + 0.5);
+
+
+            SendMsg("粗对焦:最小值" + nfUp.ToString()  + ",步长" + nfStep.ToString() + "拍照张数" + nfTimes.ToString());
+
+
+            for (int i = 0; i < nfTimes; i++)
+            {
+                int cWd = (int)Math.Ceiling(nfUp + i * nfStep + 0.5);
+                iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
+                Thread.Sleep(3000);
+
+                int swd = cWd / 100;
+                SendMsg("wd = " + swd.ToString() + "um");
+
+                String fileName = path_fine + "\\" + cWd.ToString() + ".tif";
+                if (!GetImage(ImageMode.SEM, fileName))
+                {
+                    return false;
+                }
+
+                Thread.Sleep(5000);
+            }
+
+            int fineWd;
+            ChooseBest(path_coarse, out fineWd);
+
+            SendMsg("wd fine max = " + fineWd.ToString());
+
+            a_nWd = fineWd;
 
-            //2. 计算评价函数
+            SendMsg("细选工作距离wd = " + a_nWd.ToString());
 
-            //3. 3选2策略获取工作距离
             return true;
 
         }
+
+        //LoG算子计算
+        private double LoGMean(string a_strImgPath)
+        {
+            //读入Img文件
+            if (!File.Exists(a_strImgPath))
+            {
+                return 0;
+            }
+
+            Mat src, gray_src;
+            int kernel_size = 3;
+
+            src = Cv2.ImRead(a_strImgPath);
+            gray_src = new Mat();
+
+            src.CvtColor(ColorConversionCodes.BGR2GRAY);
+
+            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
+            Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
+
+            Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
+            Cv2.Laplacian(src, src, MatType.CV_8UC3, kernel_size);
+            Cv2.ConvertScaleAbs(src, src);
+
+            //图像的平均灰度
+
+            double meanValue = 0.0;
+            meanValue = Cv2.Mean(src)[0];
+            return meanValue;
+        }
+
+        //梯度计算
+        private double Tenengrad(string a_strImgPath)
+        {
+            //读入Img文件
+            if (!File.Exists(a_strImgPath))
+            {
+                return 0;
+            }
+
+            Mat src;
+            src = Cv2.ImRead(a_strImgPath);
+
+            src.CvtColor(ColorConversionCodes.BGR2GRAY);
+
+            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
+            Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
+
+            Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
+            Cv2.Sobel(src, src, MatType.CV_8UC1, 1, 1);
+            //图像的平均灰度
+            double meanValue = 0.0;
+            meanValue = Cv2.Mean(src)[0];
+            return meanValue;
+        }
+
+        //梯度计算
+        private double TTgrad(string a_strImgPath)
+        {
+            //读入Img文件
+            if (!File.Exists(a_strImgPath))
+            {
+                return 0;
+            }
+
+            Mat src;
+            src = Cv2.ImRead(a_strImgPath);
+
+            src.CvtColor(ColorConversionCodes.BGR2GRAY);
+
+            Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
+            Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
+
+            Mat scalex = new Mat();
+            Mat scaley = new Mat();
+
+            Cv2.Scharr(src, src, src.Depth(), 1, 0);
+            Cv2.ConvertScaleAbs(src, scalex);
+
+            Cv2.Scharr(src, src, src.Depth(), 0, 1);
+            Cv2.ConvertScaleAbs(src, scaley);
+
+            Cv2.AddWeighted(scalex, 0.5, scaley, 0.5, 0, src);
+
+            //图像的平均灰度
+            double meanValue = 0.0;
+            meanValue = Cv2.Mean(src)[0];
+            return meanValue;
+        }
+
+        //三选二算法
+        private void ChooseBest(string a_strImgPath, out int a_nWd)
+        {
+            a_nWd = -1;
+
+            var files = Directory.GetFiles(a_strImgPath, "*.tif");
+            float[] values = new float[files.Length];
+
+
+            double smax1 = LoGMean(files[0]);
+            int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
+
+            double smax2 = Tenengrad(files[0]);
+            int wdmax2 = wdmax1;
+
+            double smax3 = TTgrad(files[0]);
+            int wdmax3 = wdmax1;
+
+
+
+            int x = 0;
+            foreach (var file in files)
+            {
+
+                int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
+                double svalue1 = LoGMean(file);
+                double svalue2 = Tenengrad(file);
+                double svalue3 = TTgrad(file);
+
+
+                if (smax1 < svalue1)
+                {
+                    smax1 = svalue1;
+                    wdmax1 = wd;
+                }
+
+                if (smax2 < svalue2)
+                {
+                    smax2 = svalue2;
+                    wdmax2 = wd;
+                }
+
+                if (smax3 < svalue3)
+                {
+                    smax3 = svalue3;
+                    wdmax3 = wd;
+                }
+
+                x++;
+            }
+
+            SendMsg("LoG = " + wdmax1.ToString() + ",梯度 = " + wdmax2.ToString() + ",滤波 = " + wdmax3.ToString());
+
+
+            if (wdmax1 == wdmax2 || wdmax1 == wdmax3)
+            {
+                a_nWd = wdmax1;
+                SendMsg("焦距 = " + wdmax1.ToString());
+            }
+            else if (wdmax1 == wdmax2 || wdmax2 == wdmax3)
+            {
+                a_nWd = wdmax2;
+                SendMsg("焦距 = " + wdmax2.ToString());
+            }
+            else if (wdmax3 == wdmax2 || wdmax1 == wdmax3)
+            {
+                a_nWd = wdmax3;
+                SendMsg("焦距 = " + wdmax3.ToString());
+            }
+            else
+            {
+                SendMsg("对焦失败。");
+            }
+        }
     }
 }

+ 24 - 0
MeasureThread/MeasureThread.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props" Condition="Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" />
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -13,6 +14,8 @@
     <FileAlignment>512</FileAlignment>
     <Deterministic>true</Deterministic>
     <TargetFrameworkProfile />
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -36,6 +39,18 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\bin\Debug\MeasureData.dll</HintPath>
     </Reference>
+    <Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.Blob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.Blob.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.Extensions.dll</HintPath>
+    </Reference>
+    <Reference Include="OpenCvSharp.UserInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
+      <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net461\OpenCvSharp.UserInterface.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Windows.Forms" />
@@ -61,5 +76,14 @@
       <Name>SmartSEMControl</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\build\OpenCvSharp3-AnyCPU.props'))" />
+  </Target>
 </Project>