AutoFocusWorkflow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Metis.AutoAnalysis
  13. {
  14. public class AutoFocusWorkflow
  15. {
  16. private static bool _isWorking;
  17. public static double m_CurrentValue;
  18. public static void Stop()
  19. {
  20. _isWorking = false;
  21. }
  22. public static void AutoFocusFast(AxisController stage, Func<Bitmap> CurrentImage)
  23. {
  24. if (_isWorking)
  25. return;
  26. _isWorking = true;
  27. int round = 0;//翻转次数
  28. double pulse = FocusingParameter.getRuleFocus().StepLength / 4;//最小步长1.25um
  29. double lastValue = 0.0;
  30. int dir = -1; //方向
  31. double trip = 0.0;
  32. // _mask = CreateMask(CurrentImage());
  33. try
  34. {
  35. while (_isWorking)
  36. {
  37. //Console.Write("Before focus:");
  38. stage.WaitMoveDone();
  39. //Console.Write("自动聚焦:");
  40. Thread.Sleep(40);
  41. try
  42. {
  43. Bitmap m_bitmap = CurrentImage();
  44. m_CurrentValue = getMeanValueOfBitmap(m_bitmap);
  45. //Console.WriteLine(string.Format("{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  46. }
  47. catch (Exception ex)
  48. {
  49. return;
  50. }
  51. if (round > 1) return;
  52. else if (lastValue == 0) ;
  53. else if (m_CurrentValue > lastValue)
  54. {
  55. round = 1;
  56. }
  57. else
  58. {
  59. //Console.WriteLine("Reverse:" + (dir > 0 ? "+" : "-"));
  60. round++;
  61. dir = -dir;
  62. }
  63. lastValue = m_CurrentValue;
  64. trip += pulse * dir;
  65. if (Math.Abs(trip) > pulse * 10)
  66. {
  67. Console.WriteLine("Trip of Z out of autofocus range.");
  68. return;
  69. }
  70. stage.Up(pulse * dir);
  71. }
  72. }
  73. catch
  74. {
  75. }
  76. finally
  77. {
  78. // Console.Write("End focus:");
  79. stage.WaitMoveDone();
  80. //m_CurrentValue = getMeanValueOfBitmap(CurrentImage());
  81. //Console.WriteLine(string.Format("聚焦完成:{0:f3} Z:{1:f3}", m_CurrentValue, stage.Z));
  82. _isWorking = false;
  83. // stage.LockZ();
  84. }
  85. }
  86. static Mat _mask;
  87. private static Mat CreateMask(Bitmap img)
  88. {
  89. Mat converted = BitmapConverter.ToMat(img);
  90. Mat imageGrey = new Mat();
  91. try
  92. {
  93. if (converted.Channels() == 3)
  94. OpenCvSharp.Cv2.CvtColor(converted, imageGrey, OpenCvSharp.ColorConversionCodes.RGB2GRAY);
  95. else if (converted.Channels() == 1)
  96. imageGrey = converted;
  97. }
  98. catch (Exception)
  99. {
  100. imageGrey = converted;
  101. }
  102. //stage.SetSpeedZ(40);
  103. Mat mask = new Mat();
  104. Cv2.Threshold(imageGrey, mask, 0, 255, ThresholdTypes.Triangle);
  105. var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(20, 20));
  106. Cv2.Dilate(mask, mask, kernel);
  107. //Cv2.Erode(mask, mask, kernel);
  108. return mask;
  109. }
  110. private static Mat CreateMask(Mat imageGrey)
  111. {
  112. Mat mask = new Mat();
  113. Cv2.Threshold(imageGrey, mask, 0, 255, ThresholdTypes.Triangle);
  114. var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(20, 20));
  115. Cv2.Dilate(mask, mask, kernel);
  116. return mask;
  117. }
  118. /// <summary>
  119. /// 获取输入图片的清晰度
  120. /// </summary>
  121. /// <param name="bitmap"></param>
  122. /// <returns></returns>
  123. private static double getMeanValueOfBitmap(Bitmap bitmap)
  124. {
  125. Mat converted = BitmapConverter.ToMat(bitmap);
  126. Mat imageGrey = new Mat();
  127. Mat imageSobel = new Mat();
  128. try
  129. {
  130. if (converted.Channels() == 3)
  131. OpenCvSharp.Cv2.CvtColor(converted, imageGrey, OpenCvSharp.ColorConversionCodes.RGB2GRAY);
  132. else if (converted.Channels() == 1)
  133. imageGrey = converted;
  134. }
  135. catch (Exception)
  136. {
  137. imageGrey = converted;
  138. }
  139. OpenCvSharp.Mat meanValueImage = new OpenCvSharp.Mat();
  140. OpenCvSharp.Mat meanStdValueImage = new OpenCvSharp.Mat();
  141. _mask = CreateMask(imageGrey);
  142. //求灰度图像的标准差 值越大越好
  143. OpenCvSharp.Cv2.MeanStdDev(imageGrey, meanValueImage, meanStdValueImage, _mask);
  144. return meanStdValueImage.At<double>(0, 0);
  145. }
  146. }
  147. }