FormAutoDo.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //时间:
  2. //作者:
  3. //功能:自动化测试功能
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.IO;
  14. using OpenCvSharp;
  15. using SmartSEMControl;
  16. namespace AutoDo
  17. {
  18. public partial class FormAutoDo : Form
  19. {
  20. //全局只有一个fatorySEM
  21. //static FactoryHardware factorySEM = FactoryHardware.Instance;
  22. //ISEMControl iSEM = factorySEM.ISEM;
  23. //图像文件夹
  24. public string m_strImgPath;
  25. //显示处理结果
  26. public Bitmap m_bitmap;
  27. public FormAutoDo()
  28. {
  29. InitializeComponent();
  30. }
  31. //自动对焦过程
  32. private void button1_Click(object sender, EventArgs e)
  33. {
  34. //第一种方法
  35. var files = Directory.GetFiles(m_strImgPath, "*.tif");
  36. int i = 0;
  37. foreach (var file in files)
  38. {
  39. LoGEdge(file);
  40. }
  41. }
  42. //选择图像文件夹
  43. private void button2_Click(object sender, EventArgs e)
  44. {
  45. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  46. dialog.Description = "选择拍图保存的文件夹";
  47. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  48. {
  49. if (string.IsNullOrEmpty(dialog.SelectedPath))
  50. {
  51. MessageBox.Show("图像文件夹不能为空");
  52. return;
  53. }
  54. m_strImgPath = dialog.SelectedPath;
  55. tBImgPath.Text = dialog.SelectedPath;
  56. }
  57. }
  58. //LoG算子计算
  59. private bool LoGEdge(string a_strImgPath)
  60. {
  61. //读入Img文件
  62. if (!File.Exists(a_strImgPath))
  63. {
  64. return false;
  65. }
  66. Mat src, src_gray;
  67. int kernel_size = 3;
  68. src = Cv2.ImRead(a_strImgPath);
  69. src_gray = new Mat();
  70. //高斯滤波,噪声消除
  71. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  72. //转化为灰度图像
  73. Cv2.CvtColor(src, src_gray, ColorConversionCodes.BayerBG2GRAY);
  74. Mat dst, abs_dst;
  75. dst = new Mat();
  76. abs_dst = new Mat();
  77. //使用Laplace函数
  78. Cv2.Laplacian(src_gray, dst, MatType.CV_16S, kernel_size);
  79. //计算绝对值,并将结果转为8位
  80. Cv2.ConvertScaleAbs(dst, abs_dst);
  81. //进行LoG运算
  82. return true;
  83. }
  84. }
  85. }