浏览代码

自动对焦显示评价函数

HaoShuang 5 年之前
父节点
当前提交
86bc2e3788
共有 3 个文件被更改,包括 179 次插入2 次删除
  1. 31 0
      AutoDo/AutoDo.csproj
  2. 62 2
      AutoDo/FormAutoDo.Designer.cs
  3. 86 0
      AutoDo/FormAutoDo.cs

+ 31 - 0
AutoDo/AutoDo.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" />
@@ -55,6 +70,9 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <EmbeddedResource Include="FormAutoDo.resx">
+      <DependentUpon>FormAutoDo.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>
@@ -65,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>
@@ -78,5 +97,17 @@
   <ItemGroup>
     <None Include="App.config" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\SmartSEMControl\SmartSEMControl.csproj">
+      <Project>{bf7f80b0-a6da-4470-a331-4c96057fc7fa}</Project>
+      <Name>SmartSEMControl</Name>
+    </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>

+ 62 - 2
AutoDo/FormAutoDo.Designer.cs

@@ -28,13 +28,73 @@
         /// </summary>
         private void InitializeComponent()
         {
-            this.components = new System.ComponentModel.Container();
+            this.button1 = new System.Windows.Forms.Button();
+            this.button2 = new System.Windows.Forms.Button();
+            this.tBImgPath = new System.Windows.Forms.TextBox();
+            this.pictureBox1 = new System.Windows.Forms.PictureBox();
+            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
+            this.SuspendLayout();
+            // 
+            // button1
+            // 
+            this.button1.Location = new System.Drawing.Point(43, 24);
+            this.button1.Name = "button1";
+            this.button1.Size = new System.Drawing.Size(108, 34);
+            this.button1.TabIndex = 0;
+            this.button1.Text = "自动对焦";
+            this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click);
+            // 
+            // button2
+            // 
+            this.button2.Location = new System.Drawing.Point(238, 24);
+            this.button2.Name = "button2";
+            this.button2.Size = new System.Drawing.Size(141, 34);
+            this.button2.TabIndex = 1;
+            this.button2.Text = "选择图像文件夹";
+            this.button2.UseVisualStyleBackColor = true;
+            this.button2.Click += new System.EventHandler(this.button2_Click);
+            // 
+            // tBImgPath
+            // 
+            this.tBImgPath.Location = new System.Drawing.Point(385, 30);
+            this.tBImgPath.Name = "tBImgPath";
+            this.tBImgPath.ReadOnly = true;
+            this.tBImgPath.Size = new System.Drawing.Size(479, 28);
+            this.tBImgPath.TabIndex = 2;
+            // 
+            // pictureBox1
+            // 
+            this.pictureBox1.Location = new System.Drawing.Point(43, 82);
+            this.pictureBox1.Name = "pictureBox1";
+            this.pictureBox1.Size = new System.Drawing.Size(535, 493);
+            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
+            this.pictureBox1.TabIndex = 3;
+            this.pictureBox1.TabStop = false;
+            // 
+            // FormAutoDo
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(800, 450);
+            this.ClientSize = new System.Drawing.Size(1185, 605);
+            this.Controls.Add(this.pictureBox1);
+            this.Controls.Add(this.tBImgPath);
+            this.Controls.Add(this.button2);
+            this.Controls.Add(this.button1);
+            this.Name = "FormAutoDo";
             this.Text = "Form1";
+            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
         }
 
         #endregion
+
+        private System.Windows.Forms.Button button1;
+        private System.Windows.Forms.Button button2;
+        private System.Windows.Forms.TextBox tBImgPath;
+        private System.Windows.Forms.PictureBox pictureBox1;
     }
 }
 

+ 86 - 0
AutoDo/FormAutoDo.cs

@@ -10,14 +10,100 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using System.IO;
+using OpenCvSharp;
+
+using SmartSEMControl;
+
 
 namespace AutoDo
 {
     public partial class FormAutoDo : Form
     {
+        //全局只有一个fatorySEM
+        static FactoryHardware factorySEM = FactoryHardware.Instance;
+        ISEMControl iSEM = factorySEM.ISEM;
+
+        //图像文件夹
+        public string  m_strImgPath;
+
         public FormAutoDo()
         {
             InitializeComponent();
         }
+
+        //自动对焦过程
+        private void button1_Click(object sender, EventArgs e)
+        {
+            //第一种方法
+            var files = Directory.GetFiles(m_strImgPath, "*.tif");
+           
+            foreach (var file in files)
+            {
+                LoGEdge(file);
+
+                //Bitmap bmp = MatToBitmap(src);
+                //this.pictureBox1.Image = Image.FromHbitmap(bmp.GetHbitmap());
+            }
+        }
+
+        //选择图像文件夹
+        private void button2_Click(object sender, EventArgs e)
+        {
+            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
+            dialog.Description = "选择拍图保存的文件夹";
+            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                if (string.IsNullOrEmpty(dialog.SelectedPath))
+                {
+                    MessageBox.Show("图像文件夹不能为空");
+                    return;
+                }
+
+                m_strImgPath = dialog.SelectedPath;
+                tBImgPath.Text = dialog.SelectedPath;
+
+            }
+        }
+
+        //LoG算子计算
+        private bool LoGEdge(string a_strImgPath)
+        {
+            //读入Img文件
+            if (!File.Exists(a_strImgPath))
+            {
+                return false;
+            }
+
+            Mat src, src_gray;
+            int kernel_size = 3;
+
+            src = Cv2.ImRead(a_strImgPath);
+            src_gray = new Mat();
+
+            //高斯滤波,噪声消除
+            Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
+            //转化为灰度图像
+            Cv2.CvtColor(src, src_gray, ColorConversionCodes.BayerBG2GRAY);
+
+            Mat dst, abs_dst;
+            dst = new Mat();
+            abs_dst = new Mat();
+
+            //使用Laplace函数
+            Cv2.Laplacian(src_gray, dst, MatType.CV_16S, kernel_size);
+            //计算绝对值,并将结果转为8位
+            Cv2.ConvertScaleAbs(dst, abs_dst);
+           
+            //进行LoG运算
+            return true;
+        }
+
+        //将Mat转化为Bitmap
+        private Bitmap MatToBitmap(Mat image)
+        {
+            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
+        }
+
     }
 }