AutoFocusWorkflow.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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((AxisController.ControllerType.Equals("HDS") ? 200 : 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. if (AxisController.ControllerType.Equals("HDS") && pulse <= 2 && pulse >= -2) //结束
  79. return "聚焦到最小距离,聚焦结束";
  80. stage.Up(pulse * dir);
  81. }
  82. return "聚焦中断";
  83. }
  84. catch (Exception ex)
  85. {
  86. return "聚焦中断";
  87. }
  88. finally
  89. {
  90. stage.WaitMoveDone();
  91. IsWorking = false;
  92. stage.FreeZ();
  93. }
  94. }
  95. public static void AutoFocusFast(AxisController stage, Func<Bitmap> CurrentImage)
  96. {
  97. if (IsWorking)
  98. return;
  99. IsWorking = true;
  100. int round = 0;//翻转次数
  101. double pulse = FocusingParameter.getRuleFocus().StepLength / 4;//最小步长1.25um
  102. double lastValue = 0.0;
  103. int dir = -1; //方向
  104. double trip = 0.0;
  105. //stage.SetSpeedZ(40);
  106. try
  107. {
  108. while (IsWorking)
  109. {
  110. //Console.Write("Before focus:");
  111. stage.WaitMoveDone();
  112. //Console.Write("自动聚焦:");
  113. Thread.Sleep(40);
  114. try
  115. {
  116. Bitmap m_bitmap = CurrentImage();
  117. CurrentValue = getMeanValueOfBitmap(m_bitmap);
  118. //Console.WriteLine(string.Format("{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  119. }
  120. catch (Exception ex)
  121. {
  122. return;
  123. }
  124. if (round > 1) return;
  125. else if (lastValue == 0) ;
  126. else if (CurrentValue > lastValue)
  127. {
  128. round = 1;
  129. }
  130. else
  131. {
  132. //Console.WriteLine("Reverse:" + (dir > 0 ? "+" : "-"));
  133. round++;
  134. dir = -dir;
  135. }
  136. lastValue = CurrentValue;
  137. trip += pulse * dir;
  138. if (Math.Abs(trip) > pulse * 5)
  139. {
  140. Console.WriteLine("Trip of Z out of autofocus range.");
  141. return;
  142. }
  143. stage.Up(pulse * dir);
  144. }
  145. }
  146. catch
  147. {
  148. }
  149. finally
  150. {
  151. // Console.Write("End focus:");
  152. stage.WaitMoveDone();
  153. //m_CurrentValue = getMeanValueOfBitmap(CurrentImage());
  154. //Console.WriteLine(string.Format("聚焦完成:{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  155. IsWorking = false;
  156. // stage.LockZ();
  157. }
  158. }
  159. /// <summary>
  160. /// 获取输入图片的清晰度
  161. /// </summary>
  162. /// <param name="bitmap"></param>
  163. /// <returns></returns>
  164. private static double getMeanValueOfBitmap(Bitmap bitmap)
  165. {
  166. Mat converted = PaintDotNet.Camera.Tools.ToMat(bitmap);
  167. Mat imageGrey = new Mat();
  168. Mat imageSobel = new Mat();
  169. try
  170. {
  171. if (converted.Channels() == 3)
  172. OpenCvSharp.Cv2.CvtColor(converted, imageGrey, OpenCvSharp.ColorConversionCodes.RGB2GRAY);
  173. else if (converted.Channels() == 1)
  174. imageGrey = converted;
  175. }
  176. catch (Exception)
  177. {
  178. imageGrey = converted;
  179. }
  180. OpenCvSharp.Mat meanValueImage = new OpenCvSharp.Mat();
  181. OpenCvSharp.Mat meanStdValueImage = new OpenCvSharp.Mat();
  182. //求灰度图像的标准差 值越大越好
  183. OpenCvSharp.Cv2.MeanStdDev(imageGrey, meanValueImage, meanStdValueImage);
  184. return meanStdValueImage.At<double>(0, 0);
  185. }
  186. }
  187. }