ROI.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using HalconDotNet;
  3. using System.Collections.Generic;
  4. namespace ViewWindow.Model
  5. {
  6. /// <summary>
  7. /// This class is a base class containing virtual methods for handling
  8. /// ROIs. Therefore, an inheriting class needs to define/override these
  9. /// methods to provide the ROIController with the necessary information on
  10. /// its (= the ROIs) shape and position. The example project provides
  11. /// derived ROI shapes for rectangles, lines, circles, and circular arcs.
  12. /// To use other shapes you must derive a new class from the base class
  13. /// ROI and implement its methods.
  14. /// </summary>
  15. [Serializable]
  16. public class ROI
  17. {
  18. private string color = "blue";
  19. public string Color
  20. {
  21. get { return this.color; }
  22. set { this.color = value; }
  23. }
  24. private string _type;
  25. public string Type
  26. {
  27. get
  28. {
  29. return this._type ;
  30. }
  31. set
  32. {
  33. this._type = value;
  34. }
  35. }
  36. // class members of inheriting ROI classes
  37. protected int NumHandles;
  38. protected int activeHandleIdx;
  39. /// <summary>
  40. /// Flag to define the ROI to be 'positive' or 'negative'.
  41. /// </summary>
  42. protected int OperatorFlag;
  43. /// <summary>Parameter to define the line style of the ROI.</summary>
  44. public HTuple flagLineStyle;
  45. /// <summary>Constant for a positive ROI flag.</summary>
  46. public const int POSITIVE_FLAG = ROIController.MODE_ROI_POS;
  47. /// <summary>Constant for a negative ROI flag.</summary>
  48. public const int NEGATIVE_FLAG = ROIController.MODE_ROI_NEG;
  49. public const int ROI_TYPE_LINE = 10;
  50. public const int ROI_TYPE_CIRCLE = 11;
  51. public const int ROI_TYPE_CIRCLEARC = 12;
  52. public const int ROI_TYPE_RECTANCLE1 = 13;
  53. public const int ROI_TYPE_RECTANGLE2 = 14;
  54. protected HTuple posOperation = new HTuple();
  55. protected HTuple negOperation = new HTuple(new int[] { 2, 2 });
  56. /// <summary>Constructor of abstract ROI class.</summary>
  57. public ROI() { }
  58. public virtual void createROINurbs(HTuple rows, HTuple cols) { }
  59. public virtual void createROINurbs(double imageHeight) { }
  60. public virtual void createRectangle1(double row1, double col1, double row2, double col2) { }
  61. public virtual void createInitRectangle1(double imageHeight) { }
  62. public virtual void createRectangle2(double row, double col, double phi, double length1, double length2) { }
  63. public virtual void createInitRectangle2(double imageHeight) { }
  64. public virtual void createCircle(double row,double col,double radius) { }
  65. public virtual void createCircularArc(double row, double col, double radius, double startPhi, double extentPhi, string direct) { }
  66. public virtual void createLine(double beginRow, double beginCol, double endRow, double endCol) { }
  67. /// <summary>Creates a new ROI instance at the mouse position.</summary>
  68. /// <param name="midX">
  69. /// x (=column) coordinate for ROI
  70. /// </param>
  71. /// <param name="midY">
  72. /// y (=row) coordinate for ROI
  73. /// </param>
  74. public virtual void createROI(double midX, double midY) { }
  75. /// <summary>Paints the ROI into the supplied window.</summary>
  76. /// <param name="window">HALCON window</param>
  77. public virtual void draw(HalconDotNet.HWindow window,int imageWidth,int imageHeight) { }
  78. /// <summary>
  79. /// Returns the distance of the ROI handle being
  80. /// closest to the image point(x,y)
  81. /// </summary>
  82. /// <param name="x">x (=column) coordinate</param>
  83. /// <param name="y">y (=row) coordinate</param>
  84. /// <returns>
  85. /// Distance of the closest ROI handle.
  86. /// </returns>
  87. public virtual double distToClosestHandle(double x, double y)
  88. {
  89. return 0.0;
  90. }
  91. /// <summary>
  92. /// Paints the active handle of the ROI object into the supplied window.
  93. /// </summary>
  94. /// <param name="window">HALCON window</param>
  95. public virtual void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight) { }
  96. /// <summary>
  97. /// Recalculates the shape of the ROI. Translation is
  98. /// performed at the active handle of the ROI object
  99. /// for the image coordinate (x,y).
  100. /// </summary>
  101. /// <param name="x">x (=column) coordinate</param>
  102. /// <param name="y">y (=row) coordinate</param>
  103. public virtual void moveByHandle(double x, double y, HWindowControl window) { }
  104. /// <summary>Gets the HALCON region described by the ROI.</summary>
  105. public virtual HRegion getRegion()
  106. {
  107. return null;
  108. }
  109. public virtual double getDistanceFromStartPoint(double row, double col)
  110. {
  111. return 0.0;
  112. }
  113. /// <summary>
  114. /// Gets the model information described by
  115. /// the ROI.
  116. /// </summary>
  117. public virtual HTuple getModelData()
  118. {
  119. return null;
  120. }
  121. public virtual HTuple getRowsData()
  122. {
  123. return null;
  124. }
  125. public virtual HTuple getColsData()
  126. {
  127. return null;
  128. }
  129. public virtual void getModelData(out HTuple t1,out HTuple t2)
  130. {
  131. t1 = new HTuple();
  132. t2 = new HTuple();
  133. }
  134. /// <summary>Number of handles defined for the ROI.</summary>
  135. /// <returns>Number of handles</returns>
  136. public int getNumHandles()
  137. {
  138. return NumHandles;
  139. }
  140. /// <summary>Gets the active handle of the ROI.</summary>
  141. /// <returns>Index of the active handle (from the handle list)</returns>
  142. public int getActHandleIdx()
  143. {
  144. return activeHandleIdx;
  145. }
  146. /// <summary>
  147. /// Gets the sign of the ROI object, being either
  148. /// 'positive' or 'negative'. This sign is used when creating a model
  149. /// region for matching applications from a list of ROIs.
  150. /// </summary>
  151. public int getOperatorFlag()
  152. {
  153. return OperatorFlag;
  154. }
  155. /// <summary>
  156. /// Sets the sign of a ROI object to be positive or negative.
  157. /// The sign is used when creating a model region for matching
  158. /// applications by summing up all positive and negative ROI models
  159. /// created so far.
  160. /// </summary>
  161. /// <param name="flag">Sign of ROI object</param>
  162. public void setOperatorFlag(int flag)
  163. {
  164. OperatorFlag = flag;
  165. switch (OperatorFlag)
  166. {
  167. case ROI.POSITIVE_FLAG:
  168. flagLineStyle = posOperation;
  169. break;
  170. case ROI.NEGATIVE_FLAG:
  171. flagLineStyle = negOperation;
  172. break;
  173. default:
  174. flagLineStyle = posOperation;
  175. break;
  176. }
  177. }
  178. }//end of class
  179. }//end of namespace