FormAutoDo.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 System.Windows.Forms.DataVisualization.Charting;
  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. float[] values = new float[files.Length];
  37. this.dataGridView1.Rows.Clear();
  38. Series series = chart1.Series[0];
  39. series.Points.Clear();
  40. int x = 0;
  41. double smax = LoGMean(files[0]);
  42. int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  43. foreach (var file in files)
  44. {
  45. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  46. double svalue = LoGMean(file);
  47. if (smax < svalue)
  48. {
  49. smax = svalue;
  50. wdmax = wd;
  51. }
  52. ShowData(wd, svalue);
  53. x++;
  54. }
  55. this.label1.Text = "焦距:" + wdmax.ToString();
  56. }
  57. //选择图像文件夹
  58. private void button2_Click(object sender, EventArgs e)
  59. {
  60. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  61. dialog.Description = "选择拍图保存的文件夹";
  62. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  63. {
  64. if (string.IsNullOrEmpty(dialog.SelectedPath))
  65. {
  66. MessageBox.Show("图像文件夹不能为空");
  67. return;
  68. }
  69. m_strImgPath = dialog.SelectedPath;
  70. tBImgPath.Text = dialog.SelectedPath;
  71. }
  72. }
  73. //LoG算子计算
  74. private double LoGMean(string a_strImgPath)
  75. {
  76. //读入Img文件
  77. if (!File.Exists(a_strImgPath))
  78. {
  79. return 0;
  80. }
  81. Mat src, gray_src;
  82. int kernel_size = 3;
  83. src = Cv2.ImRead(a_strImgPath);
  84. gray_src = new Mat();
  85. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  86. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  87. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  88. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  89. Cv2.Laplacian(src, src, MatType.CV_8UC3, kernel_size);
  90. Cv2.ConvertScaleAbs(src, src);
  91. //图像的平均灰度
  92. double meanValue = 0.0;
  93. meanValue = Cv2.Mean(src)[0];
  94. return meanValue;
  95. }
  96. //梯度计算
  97. private double Tenengrad(string a_strImgPath)
  98. {
  99. //读入Img文件
  100. if (!File.Exists(a_strImgPath))
  101. {
  102. return 0;
  103. }
  104. Mat src;
  105. src = Cv2.ImRead(a_strImgPath);
  106. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  107. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  108. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  109. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  110. Cv2.Sobel(src, src, MatType.CV_8UC1, 1, 1);
  111. //图像的平均灰度
  112. double meanValue = 0.0;
  113. meanValue = Cv2.Mean(src)[0];
  114. return meanValue;
  115. }
  116. //梯度计算
  117. private double TTgrad(string a_strImgPath)
  118. {
  119. //读入Img文件
  120. if (!File.Exists(a_strImgPath))
  121. {
  122. return 0;
  123. }
  124. Mat src;
  125. src = Cv2.ImRead(a_strImgPath);
  126. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  127. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  128. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  129. Mat scalex = new Mat();
  130. Mat scaley = new Mat();
  131. Cv2.Scharr(src, src, src.Depth(),1, 0);
  132. Cv2.ConvertScaleAbs(src, scalex);
  133. Cv2.Scharr(src, src, src.Depth(), 0, 1);
  134. Cv2.ConvertScaleAbs(src, scaley);
  135. Cv2.AddWeighted(scalex, 0.5, scaley, 0.5, 0, src);
  136. //图像的平均灰度
  137. double meanValue = 0.0;
  138. meanValue = Cv2.Mean(src)[0];
  139. return meanValue;
  140. }
  141. private void button3_Click(object sender, EventArgs e)
  142. {
  143. //第二种方法
  144. var files = Directory.GetFiles(m_strImgPath, "*.tif");
  145. float[] values = new float[files.Length];
  146. this.dataGridView1.Rows.Clear();
  147. Series series = chart1.Series[0];
  148. series.Points.Clear();
  149. double smax = Tenengrad(files[0]);
  150. int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  151. int x = 0;
  152. foreach (var file in files)
  153. {
  154. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  155. double svalue = Tenengrad(file);
  156. if (smax < svalue)
  157. {
  158. smax = svalue;
  159. wdmax = wd;
  160. }
  161. ShowData(wd, svalue);
  162. x++;
  163. }
  164. this.label1.Text = "焦距:" + wdmax.ToString();
  165. }
  166. //显示数据
  167. private void ShowData(int wd, double svalue)
  168. {
  169. //绘制评价函数曲线s
  170. Series series = chart1.Series[0];
  171. series.Points.AddXY(wd, svalue);
  172. //显示评价函数数值
  173. int index = this.dataGridView1.Rows.Add();
  174. this.dataGridView1.Rows[index].Cells[0].Value = wd;
  175. this.dataGridView1.Rows[index].Cells[1].Value = svalue;
  176. }
  177. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  178. {
  179. }
  180. private void button4_Click(object sender, EventArgs e)
  181. {
  182. var files = Directory.GetFiles(m_strImgPath, "*.tif");
  183. float[] values = new float[files.Length];
  184. double smax1 = LoGMean(files[0]);
  185. int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  186. double smax2 = Tenengrad(files[0]);
  187. int wdmax2 = wdmax1;
  188. double smax3 = TTgrad(files[0]);
  189. int wdmax3 = wdmax1;
  190. int x = 0;
  191. foreach (var file in files)
  192. {
  193. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  194. double svalue1 = LoGMean(file);
  195. double svalue2 = Tenengrad(file);
  196. double svalue3 = TTgrad(file);
  197. if (smax1 < svalue1)
  198. {
  199. smax1 = svalue1;
  200. wdmax1= wd;
  201. }
  202. if (smax2 < svalue2)
  203. {
  204. smax2 = svalue2;
  205. wdmax2 = wd;
  206. }
  207. if (smax3 < svalue3)
  208. {
  209. smax3 = svalue3;
  210. wdmax3 = wd;
  211. }
  212. x++;
  213. }
  214. this.label2.Text = "";
  215. this.label2.Text = "LoG = " + wdmax1.ToString() + ",梯度 = " + wdmax2.ToString() + ",滤波 = " + wdmax3.ToString();
  216. if (wdmax1 == wdmax2 || wdmax1 == wdmax3)
  217. {
  218. this.label2.Text += ",焦距 = " + wdmax1.ToString();
  219. }
  220. else if (wdmax1 == wdmax2 || wdmax2 == wdmax3)
  221. {
  222. this.label2.Text += ",焦距 = " + wdmax2.ToString();
  223. }
  224. else if (wdmax3 == wdmax2 || wdmax1 == wdmax3)
  225. {
  226. this.label2.Text += ",焦距 = " + wdmax3.ToString();
  227. }
  228. else
  229. {
  230. this.label2.Text += ",对焦失败。";
  231. }
  232. }
  233. private void button5_Click(object sender, EventArgs e)
  234. {
  235. //第一种方法
  236. var files = Directory.GetFiles(m_strImgPath, "*.tif");
  237. float[] values = new float[files.Length];
  238. this.dataGridView1.Rows.Clear();
  239. Series series = chart1.Series[0];
  240. series.Points.Clear();
  241. int x = 0;
  242. double smax = TTgrad(files[0]);
  243. int wdmax = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  244. foreach (var file in files)
  245. {
  246. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  247. double svalue = TTgrad(file);
  248. if (smax < svalue)
  249. {
  250. smax = svalue;
  251. wdmax = wd;
  252. }
  253. ShowData(wd, svalue);
  254. x++;
  255. }
  256. this.label1.Text = "焦距:" + wdmax.ToString();
  257. }
  258. }
  259. }