Caliper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using CommonMethods;
  3. using HalconDotNet;
  4. using ToolBase;
  5. using static DataStruct.DataStructClass;
  6. using ChoiceTech.Halcon.Control;
  7. using ViewROI;
  8. namespace CaliperTool
  9. {
  10. [Serializable]
  11. public class Caliper: IToolBase
  12. {
  13. public bool toolEnable = true;
  14. /// <summary>
  15. /// 输入姿态..
  16. /// </summary>
  17. public PosXYU inputPose = new PosXYU();
  18. /// <summary>
  19. /// 制作模板时的输入位姿
  20. /// </summary>
  21. public PosXYU templatePose = new PosXYU();
  22. /// <summary>
  23. /// 卡尺
  24. /// </summary>
  25. public HObject contoursDisp = null;
  26. /// <summary>
  27. /// 箭头
  28. /// </summary>
  29. public HObject arrowDisp = null;
  30. /// <summary>
  31. /// 交点
  32. /// </summary>
  33. public HObject crossDisp = null;
  34. /// <summary>
  35. /// 期望矩形中心行坐标
  36. /// </summary>
  37. public HTuple expectRecStartRow = 200;
  38. /// <summary>
  39. /// 期望矩形中心列坐标
  40. /// </summary>
  41. public HTuple expectRecStartColumn = 200;
  42. /// <summary>
  43. /// 期望矩形起点方向
  44. /// </summary>
  45. public HTuple expectAngle = 0;
  46. /// <summary>
  47. /// 卡尺高
  48. /// </summary>
  49. public HTuple length1 = 40;
  50. /// <summary>
  51. /// 卡尺宽
  52. /// </summary>
  53. public HTuple length2 = 40;
  54. /// <summary>
  55. /// 找边极性,从明到暗或从暗到明
  56. /// </summary>
  57. public string polarity = "negative";
  58. /// <summary>
  59. /// 边阈值
  60. /// </summary>
  61. public int threshold = 30;
  62. /// <summary>
  63. /// 边Sigma
  64. /// </summary>
  65. public double sigma = 1.0;
  66. /// <summary>
  67. /// 选择所查找到的点
  68. /// </summary>
  69. public string edgeSelect = "all";
  70. /// <summary>
  71. /// 矩形框显示
  72. /// </summary>
  73. public bool dispRec = true;
  74. /// <summary>
  75. /// 交点显示
  76. /// </summary>
  77. public bool dispCross = true;
  78. /// <summary
  79. /// 是否显示的线
  80. /// </summary>
  81. public bool LineDisp = true;
  82. /// <summary>
  83. /// 新的跟随姿态变化后的预期线信息
  84. /// </summary>
  85. HTuple newExpectRecStartRow = new HTuple(200), newExpectRecStartColumn = new HTuple(200), newExpectPhi = new HTuple(0);
  86. [NonSerialized]
  87. public HDrawingObject calibDrawObject = new HDrawingObject();
  88. /// <summary>
  89. /// 查找到的线的起点行坐标
  90. /// </summary>
  91. private HTuple _resultRow = 0;
  92. public HTuple ResulttRow
  93. {
  94. get
  95. {
  96. _resultRow = Math.Round((double)_resultRow, 3);
  97. return _resultRow;
  98. }
  99. set { _resultRow = value; }
  100. }
  101. /// <summary>
  102. /// 查找到的线的起点列坐标
  103. /// </summary>
  104. private HTuple _resultCol = 0;
  105. public HTuple ResultCol
  106. {
  107. get
  108. {
  109. _resultCol = Math.Round((double)_resultCol, 3);
  110. return _resultCol;
  111. }
  112. set { _resultCol = value; }
  113. }
  114. public override void DispImage()
  115. {
  116. if (inputImage != null)
  117. {
  118. FormCaliper.Instance.myHwindow.DispHWindow.DispObj(inputImage);
  119. }
  120. }
  121. internal void DrawExpectLine(HWindow myHwindow)
  122. {
  123. if (inputImage != null)
  124. {
  125. try
  126. {
  127. HOperatorSet.SetColor(myHwindow, new HTuple("green"));
  128. HOperatorSet.DrawRectangle2Mod(myHwindow, expectRecStartRow, expectRecStartColumn, expectAngle, length1, length2,
  129. out expectRecStartRow, out expectRecStartColumn, out expectAngle, out length1, out length2);
  130. if (inputPose != null)
  131. {
  132. templatePose.X = inputPose.X;
  133. templatePose.Y = inputPose.Y;
  134. templatePose.U = inputPose.U;
  135. }
  136. // 参数
  137. FormCaliper.Instance.tbx_caliperLength1.Text = length1.TupleString("10.3f");
  138. FormCaliper.Instance.tbx_caliperLength2.Text = length2.TupleString("10.3f");
  139. // Run();
  140. }
  141. catch (Exception ex)
  142. {
  143. FormCaliper.Instance.SetToolStatus(ex.Message, ToolRunStatu.Tool_Run_Error);
  144. }
  145. }
  146. else
  147. {
  148. FormCaliper.Instance.SetToolStatus("图像为空", ToolRunStatu.Not_Asign_Input_Image);
  149. }
  150. }
  151. public void UpdateImage()
  152. {
  153. FormCaliper.Instance.myHwindow.DispHWindow.ClearWindow();
  154. DispImage();
  155. }
  156. public override void Run(SoftwareRunState softwareRunState)
  157. {
  158. HTuple HMeasureHandle = new HTuple();
  159. HTuple resultRow, resultCol;
  160. if (inputImage == null)
  161. {
  162. if(softwareRunState == SoftwareRunState.Debug)
  163. {
  164. FormCaliper.Instance.SetToolStatus("图像为空", ToolRunStatu.Not_Asign_Input_Image);
  165. }
  166. toolRunStatu = ToolRunStatu.Not_Input_Image;
  167. return;
  168. }
  169. try
  170. {
  171. UpdateImage();
  172. if (inputPose != null)
  173. {
  174. HTuple Row = inputPose.X - templatePose.X;
  175. HTuple Col = inputPose.Y - templatePose.Y;
  176. HTuple angle = inputPose.U - templatePose.U;
  177. HTuple _homMat2D;
  178. HOperatorSet.HomMat2dIdentity(out _homMat2D);
  179. HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D);
  180. HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D);
  181. //对预期线的起始点做放射变换
  182. HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectRecStartRow, (HTuple)expectRecStartColumn, out newExpectRecStartRow, out newExpectRecStartColumn);
  183. }
  184. else
  185. {
  186. newExpectRecStartRow = expectRecStartRow;
  187. newExpectRecStartColumn = expectRecStartColumn;
  188. }
  189. HTuple width, height, AmplitudeThreshold, distance;
  190. HOperatorSet.GetImageSize(inputImage, out width, out height);
  191. HOperatorSet.GenMeasureRectangle2(expectRecStartRow, expectRecStartColumn, expectAngle, length1, length2, width, height, "nearest_neighbor", out HMeasureHandle);
  192. HOperatorSet.MeasurePos(inputImage, HMeasureHandle, sigma, threshold, polarity, edgeSelect, out resultRow, out resultCol, out AmplitudeThreshold, out distance);
  193. if(resultRow.Length != 0)
  194. {
  195. ResulttRow = resultRow;
  196. ResultCol = resultCol;
  197. }
  198. //把点显示出来
  199. HOperatorSet.GenCrossContourXld(out crossDisp, ResulttRow, ResultCol, new HTuple(80), new HTuple(0));
  200. if(softwareRunState == SoftwareRunState.Debug)
  201. {
  202. DispMainWindow(FormCaliper.Instance.myHwindow);
  203. FormCaliper.Instance.tbx_resultStartRow.Text = ResulttRow.ToString();
  204. FormCaliper.Instance.tbx_resultStartCol.Text = ResultCol.ToString();
  205. FormCaliper.Instance.SetToolStatus("运行成功", ToolRunStatu.Succeed);
  206. }
  207. // 参数传递
  208. ParamsTrans();
  209. toolRunStatu = ToolRunStatu.Succeed;
  210. }
  211. catch (Exception ex)
  212. {
  213. toolRunStatu = ToolRunStatu.Not_Succeed;
  214. if (softwareRunState == SoftwareRunState.Debug)
  215. {
  216. FormCaliper.Instance.SetToolStatus("工具运行异常" + ex.Message, ToolRunStatu.Tool_Run_Error);
  217. }
  218. }
  219. finally
  220. {
  221. //homMat2DArrow.Dispose();
  222. //arrow.Dispose();
  223. //arrowTrans.Dispose();
  224. }
  225. }
  226. /// <summary>
  227. /// 将数据传递给FindlineToolInterface
  228. /// </summary>
  229. private void ParamsTrans()
  230. {
  231. if(FormCaliper.Instance.myToolInfo != null)
  232. {
  233. FormCaliper.Instance.myToolInfo.toolOutput.Clear();
  234. FormCaliper.Instance.myToolInfo.toolOutput.Add(new ToolIO("outputCenterRow", ResulttRow, DataType.IntValue));
  235. FormCaliper.Instance.myToolInfo.toolOutput.Add(new ToolIO("outputCenterColumn", ResultCol, DataType.IntValue));
  236. }
  237. }
  238. public override void DispMainWindow(HWindowTool_Smart window)
  239. {
  240. // 显示矩形
  241. if (dispRec && contoursDisp != null)
  242. {
  243. window.DispHWindow.SetColor("blue");
  244. window.DispHWindow.DispObj(contoursDisp);
  245. }
  246. // 显示交点
  247. if (dispCross && crossDisp != null)
  248. {
  249. window.DispHWindow.SetColor("green");
  250. window.DispHWindow.DispObj(crossDisp);
  251. }
  252. //显示找到的线
  253. // window.DispObj(LineDisp, "green");
  254. }
  255. }
  256. }