AutoFocusWorkflow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using PaintDotNet.Setting;
  4. using StageController;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace PaintDotNet
  13. {
  14. public class AutoFocusWorkflow
  15. {
  16. public static bool IsWorking;
  17. public static double CurrentValue;
  18. public static void Stop()
  19. {
  20. IsWorking = false;
  21. }
  22. /// <summary>
  23. /// 自动聚焦的线程方法
  24. /// </summary>
  25. public static string AutoFocus(AxisController stage, Func<Bitmap> CurrentImage)
  26. {
  27. if (!stage.IsOpen) return "载物台离线";
  28. if (IsWorking) return "正在聚焦中......";
  29. IsWorking = true;
  30. int round = 0;//翻转次数
  31. double pulse = FocusingParameter.getRuleFocus().StepLength;
  32. var focusRange = pulse * 5;
  33. double lastMaxMean = 0.0;
  34. double maxMeanValue = 0.0;
  35. int dir = -1; //方向
  36. double trip = 0.0;
  37. try
  38. {
  39. while (IsWorking)
  40. {
  41. stage.WaitMoveDone();
  42. stage.SetSpeedZ(Math.Max(80 / (round + 1), 17));
  43. Bitmap m_bitmap = CurrentImage();
  44. CurrentValue = getMeanValueOfBitmap(m_bitmap);
  45. Console.WriteLine(string.Format("自动聚焦:{0:f3} Z:{1:f3}", CurrentValue, stage.Z));
  46. bool foundNextMaxMean = false;
  47. if (CurrentValue > lastMaxMean)
  48. {
  49. foundNextMaxMean = true;
  50. lastMaxMean = CurrentValue;
  51. }
  52. if (CurrentValue > maxMeanValue)
  53. {
  54. maxMeanValue = CurrentValue;
  55. }
  56. if (!foundNextMaxMean)
  57. {
  58. dir = -dir;
  59. if (pulse == 1) //结束
  60. {
  61. if (dir > 0)
  62. stage.Up(1.7);
  63. else
  64. stage.Up(-0.6);
  65. // MessageBox.Show("Done.");
  66. return "";
  67. }
  68. Console.WriteLine("Reverse:" + (dir > 0 ? "+" : "-"));
  69. round++;
  70. lastMaxMean = 0;
  71. pulse = (int)(Math.Max(1, pulse * 0.3));
  72. }
  73. trip += pulse * dir;
  74. if (Math.Abs(trip) > focusRange)
  75. {
  76. return "超出聚焦行程,聚焦结束";
  77. }
  78. stage.Up(pulse * dir);
  79. }
  80. return "聚焦中断";
  81. }
  82. catch (Exception ex)
  83. {
  84. return "聚焦中断";
  85. }
  86. finally
  87. {
  88. stage.WaitMoveDone();
  89. IsWorking = false;
  90. stage.FreeZ();
  91. }
  92. }
  93. public static void AutoFocusFast(AxisController stage, Func<Bitmap> CurrentImage)
  94. {
  95. if (IsWorking)
  96. return;
  97. IsWorking = true;
  98. int round = 0;//翻转次数
  99. double pulse = FocusingParameter.getRuleFocus().StepLength / 4;//最小步长1.25um
  100. double lastValue = 0.0;
  101. int dir = -1; //方向
  102. double trip = 0.0;
  103. //stage.SetSpeedZ(40);
  104. try
  105. {
  106. while (IsWorking)
  107. {
  108. //Console.Write("Before focus:");
  109. stage.WaitMoveDone();
  110. //Console.Write("自动聚焦:");
  111. Thread.Sleep(40);
  112. try
  113. {
  114. Bitmap m_bitmap = CurrentImage();
  115. CurrentValue = getMeanValueOfBitmap(m_bitmap);
  116. //Console.WriteLine(string.Format("{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  117. }
  118. catch (Exception ex)
  119. {
  120. return;
  121. }
  122. if (round > 1) return;
  123. else if (lastValue == 0) ;
  124. else if (CurrentValue > lastValue)
  125. {
  126. round = 1;
  127. }
  128. else
  129. {
  130. //Console.WriteLine("Reverse:" + (dir > 0 ? "+" : "-"));
  131. round++;
  132. dir = -dir;
  133. }
  134. lastValue = CurrentValue;
  135. trip += pulse * dir;
  136. if (Math.Abs(trip) > pulse * 5)
  137. {
  138. Console.WriteLine("Trip of Z out of autofocus range.");
  139. return;
  140. }
  141. stage.Up(pulse * dir);
  142. }
  143. }
  144. catch
  145. {
  146. }
  147. finally
  148. {
  149. // Console.Write("End focus:");
  150. stage.WaitMoveDone();
  151. //m_CurrentValue = getMeanValueOfBitmap(CurrentImage());
  152. //Console.WriteLine(string.Format("聚焦完成:{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  153. IsWorking = false;
  154. // stage.LockZ();
  155. }
  156. }
  157. /// <summary>
  158. /// 获取输入图片的清晰度
  159. /// </summary>
  160. /// <param name="bitmap"></param>
  161. /// <returns></returns>
  162. private static double getMeanValueOfBitmap(Bitmap bitmap)
  163. {
  164. Mat converted = BitmapConverter.ToMat(bitmap);
  165. Mat imageGrey = new Mat();
  166. Mat imageSobel = new Mat();
  167. try
  168. {
  169. if (converted.Channels() == 3)
  170. OpenCvSharp.Cv2.CvtColor(converted, imageGrey, OpenCvSharp.ColorConversionCodes.RGB2GRAY);
  171. else if (converted.Channels() == 1)
  172. imageGrey = converted;
  173. }
  174. catch (Exception)
  175. {
  176. imageGrey = converted;
  177. }
  178. OpenCvSharp.Mat meanValueImage = new OpenCvSharp.Mat();
  179. OpenCvSharp.Mat meanStdValueImage = new OpenCvSharp.Mat();
  180. //求灰度图像的标准差 值越大越好
  181. OpenCvSharp.Cv2.MeanStdDev(imageGrey, meanValueImage, meanStdValueImage);
  182. return meanStdValueImage.At<double>(0, 0);
  183. }
  184. }
  185. }