LineSegmentDetector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.ImgprocModule
  7. {
  8. // C++: class LineSegmentDetector
  9. /**
  10. * Line segment detector class
  11. *
  12. * following the algorithm described at CITE: Rafael12 .
  13. *
  14. * <b>Note:</b> Implementation has been removed from OpenCV version 3.4.6 to 3.4.15 and version 4.1.0 to 4.5.3 due original code license conflict.
  15. * restored again after [Computation of a NFA](https://github.com/rafael-grompone-von-gioi/binomial_nfa) code published under the MIT license.
  16. */
  17. public class LineSegmentDetector : Algorithm
  18. {
  19. protected override void Dispose(bool disposing)
  20. {
  21. try
  22. {
  23. if (disposing)
  24. {
  25. }
  26. if (IsEnabledDispose)
  27. {
  28. if (nativeObj != IntPtr.Zero)
  29. imgproc_LineSegmentDetector_delete(nativeObj);
  30. nativeObj = IntPtr.Zero;
  31. }
  32. }
  33. finally
  34. {
  35. base.Dispose(disposing);
  36. }
  37. }
  38. protected internal LineSegmentDetector(IntPtr addr) : base(addr) { }
  39. // internal usage only
  40. public static new LineSegmentDetector __fromPtr__(IntPtr addr) { return new LineSegmentDetector(addr); }
  41. //
  42. // C++: void cv::LineSegmentDetector::detect(Mat image, Mat& lines, Mat& width = Mat(), Mat& prec = Mat(), Mat& nfa = Mat())
  43. //
  44. /**
  45. * Finds lines in the input image.
  46. *
  47. * This is the output of the default parameters of the algorithm on the above shown image.
  48. *
  49. * ![image](pics/building_lsd.png)
  50. *
  51. * param image A grayscale (CV_8UC1) input image. If only a roi needs to be selected, use:
  52. * {code lsd_ptr-&gt;detect(image(roi), lines, ...); lines += Scalar(roi.x, roi.y, roi.x, roi.y);}
  53. * param lines A vector of Vec4f elements specifying the beginning and ending point of a line. Where
  54. * Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly
  55. * oriented depending on the gradient.
  56. * param width Vector of widths of the regions, where the lines are found. E.g. Width of line.
  57. * param prec Vector of precisions with which the lines are found.
  58. * param nfa Vector containing number of false alarms in the line region, with precision of 10%. The
  59. * bigger the value, logarithmically better the detection.
  60. * <ul>
  61. * <li>
  62. * -1 corresponds to 10 mean false alarms
  63. * </li>
  64. * <li>
  65. * 0 corresponds to 1 mean false alarm
  66. * </li>
  67. * <li>
  68. * 1 corresponds to 0.1 mean false alarms
  69. * This vector will be calculated only when the objects type is #LSD_REFINE_ADV.
  70. * </li>
  71. * </ul>
  72. */
  73. public void detect(Mat image, Mat lines, Mat width, Mat prec, Mat nfa)
  74. {
  75. ThrowIfDisposed();
  76. if (image != null) image.ThrowIfDisposed();
  77. if (lines != null) lines.ThrowIfDisposed();
  78. if (width != null) width.ThrowIfDisposed();
  79. if (prec != null) prec.ThrowIfDisposed();
  80. if (nfa != null) nfa.ThrowIfDisposed();
  81. imgproc_LineSegmentDetector_detect_10(nativeObj, image.nativeObj, lines.nativeObj, width.nativeObj, prec.nativeObj, nfa.nativeObj);
  82. }
  83. /**
  84. * Finds lines in the input image.
  85. *
  86. * This is the output of the default parameters of the algorithm on the above shown image.
  87. *
  88. * ![image](pics/building_lsd.png)
  89. *
  90. * param image A grayscale (CV_8UC1) input image. If only a roi needs to be selected, use:
  91. * {code lsd_ptr-&gt;detect(image(roi), lines, ...); lines += Scalar(roi.x, roi.y, roi.x, roi.y);}
  92. * param lines A vector of Vec4f elements specifying the beginning and ending point of a line. Where
  93. * Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly
  94. * oriented depending on the gradient.
  95. * param width Vector of widths of the regions, where the lines are found. E.g. Width of line.
  96. * param prec Vector of precisions with which the lines are found.
  97. * bigger the value, logarithmically better the detection.
  98. * <ul>
  99. * <li>
  100. * -1 corresponds to 10 mean false alarms
  101. * </li>
  102. * <li>
  103. * 0 corresponds to 1 mean false alarm
  104. * </li>
  105. * <li>
  106. * 1 corresponds to 0.1 mean false alarms
  107. * This vector will be calculated only when the objects type is #LSD_REFINE_ADV.
  108. * </li>
  109. * </ul>
  110. */
  111. public void detect(Mat image, Mat lines, Mat width, Mat prec)
  112. {
  113. ThrowIfDisposed();
  114. if (image != null) image.ThrowIfDisposed();
  115. if (lines != null) lines.ThrowIfDisposed();
  116. if (width != null) width.ThrowIfDisposed();
  117. if (prec != null) prec.ThrowIfDisposed();
  118. imgproc_LineSegmentDetector_detect_11(nativeObj, image.nativeObj, lines.nativeObj, width.nativeObj, prec.nativeObj);
  119. }
  120. /**
  121. * Finds lines in the input image.
  122. *
  123. * This is the output of the default parameters of the algorithm on the above shown image.
  124. *
  125. * ![image](pics/building_lsd.png)
  126. *
  127. * param image A grayscale (CV_8UC1) input image. If only a roi needs to be selected, use:
  128. * {code lsd_ptr-&gt;detect(image(roi), lines, ...); lines += Scalar(roi.x, roi.y, roi.x, roi.y);}
  129. * param lines A vector of Vec4f elements specifying the beginning and ending point of a line. Where
  130. * Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly
  131. * oriented depending on the gradient.
  132. * param width Vector of widths of the regions, where the lines are found. E.g. Width of line.
  133. * bigger the value, logarithmically better the detection.
  134. * <ul>
  135. * <li>
  136. * -1 corresponds to 10 mean false alarms
  137. * </li>
  138. * <li>
  139. * 0 corresponds to 1 mean false alarm
  140. * </li>
  141. * <li>
  142. * 1 corresponds to 0.1 mean false alarms
  143. * This vector will be calculated only when the objects type is #LSD_REFINE_ADV.
  144. * </li>
  145. * </ul>
  146. */
  147. public void detect(Mat image, Mat lines, Mat width)
  148. {
  149. ThrowIfDisposed();
  150. if (image != null) image.ThrowIfDisposed();
  151. if (lines != null) lines.ThrowIfDisposed();
  152. if (width != null) width.ThrowIfDisposed();
  153. imgproc_LineSegmentDetector_detect_12(nativeObj, image.nativeObj, lines.nativeObj, width.nativeObj);
  154. }
  155. /**
  156. * Finds lines in the input image.
  157. *
  158. * This is the output of the default parameters of the algorithm on the above shown image.
  159. *
  160. * ![image](pics/building_lsd.png)
  161. *
  162. * param image A grayscale (CV_8UC1) input image. If only a roi needs to be selected, use:
  163. * {code lsd_ptr-&gt;detect(image(roi), lines, ...); lines += Scalar(roi.x, roi.y, roi.x, roi.y);}
  164. * param lines A vector of Vec4f elements specifying the beginning and ending point of a line. Where
  165. * Vec4f is (x1, y1, x2, y2), point 1 is the start, point 2 - end. Returned lines are strictly
  166. * oriented depending on the gradient.
  167. * bigger the value, logarithmically better the detection.
  168. * <ul>
  169. * <li>
  170. * -1 corresponds to 10 mean false alarms
  171. * </li>
  172. * <li>
  173. * 0 corresponds to 1 mean false alarm
  174. * </li>
  175. * <li>
  176. * 1 corresponds to 0.1 mean false alarms
  177. * This vector will be calculated only when the objects type is #LSD_REFINE_ADV.
  178. * </li>
  179. * </ul>
  180. */
  181. public void detect(Mat image, Mat lines)
  182. {
  183. ThrowIfDisposed();
  184. if (image != null) image.ThrowIfDisposed();
  185. if (lines != null) lines.ThrowIfDisposed();
  186. imgproc_LineSegmentDetector_detect_13(nativeObj, image.nativeObj, lines.nativeObj);
  187. }
  188. //
  189. // C++: void cv::LineSegmentDetector::drawSegments(Mat& image, Mat lines)
  190. //
  191. /**
  192. * Draws the line segments on a given image.
  193. * param image The image, where the lines will be drawn. Should be bigger or equal to the image,
  194. * where the lines were found.
  195. * param lines A vector of the lines that needed to be drawn.
  196. */
  197. public void drawSegments(Mat image, Mat lines)
  198. {
  199. ThrowIfDisposed();
  200. if (image != null) image.ThrowIfDisposed();
  201. if (lines != null) lines.ThrowIfDisposed();
  202. imgproc_LineSegmentDetector_drawSegments_10(nativeObj, image.nativeObj, lines.nativeObj);
  203. }
  204. //
  205. // C++: int cv::LineSegmentDetector::compareSegments(Size size, Mat lines1, Mat lines2, Mat& image = Mat())
  206. //
  207. /**
  208. * Draws two groups of lines in blue and red, counting the non overlapping (mismatching) pixels.
  209. *
  210. * param size The size of the image, where lines1 and lines2 were found.
  211. * param lines1 The first group of lines that needs to be drawn. It is visualized in blue color.
  212. * param lines2 The second group of lines. They visualized in red color.
  213. * param image Optional image, where the lines will be drawn. The image should be color(3-channel)
  214. * in order for lines1 and lines2 to be drawn in the above mentioned colors.
  215. * return automatically generated
  216. */
  217. public int compareSegments(Size size, Mat lines1, Mat lines2, Mat image)
  218. {
  219. ThrowIfDisposed();
  220. if (lines1 != null) lines1.ThrowIfDisposed();
  221. if (lines2 != null) lines2.ThrowIfDisposed();
  222. if (image != null) image.ThrowIfDisposed();
  223. return imgproc_LineSegmentDetector_compareSegments_10(nativeObj, size.width, size.height, lines1.nativeObj, lines2.nativeObj, image.nativeObj);
  224. }
  225. /**
  226. * Draws two groups of lines in blue and red, counting the non overlapping (mismatching) pixels.
  227. *
  228. * param size The size of the image, where lines1 and lines2 were found.
  229. * param lines1 The first group of lines that needs to be drawn. It is visualized in blue color.
  230. * param lines2 The second group of lines. They visualized in red color.
  231. * in order for lines1 and lines2 to be drawn in the above mentioned colors.
  232. * return automatically generated
  233. */
  234. public int compareSegments(Size size, Mat lines1, Mat lines2)
  235. {
  236. ThrowIfDisposed();
  237. if (lines1 != null) lines1.ThrowIfDisposed();
  238. if (lines2 != null) lines2.ThrowIfDisposed();
  239. return imgproc_LineSegmentDetector_compareSegments_11(nativeObj, size.width, size.height, lines1.nativeObj, lines2.nativeObj);
  240. }
  241. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  242. const string LIBNAME = "__Internal";
  243. #else
  244. const string LIBNAME = "opencvforunity";
  245. #endif
  246. // C++: void cv::LineSegmentDetector::detect(Mat image, Mat& lines, Mat& width = Mat(), Mat& prec = Mat(), Mat& nfa = Mat())
  247. [DllImport(LIBNAME)]
  248. private static extern void imgproc_LineSegmentDetector_detect_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr lines_nativeObj, IntPtr width_nativeObj, IntPtr prec_nativeObj, IntPtr nfa_nativeObj);
  249. [DllImport(LIBNAME)]
  250. private static extern void imgproc_LineSegmentDetector_detect_11(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr lines_nativeObj, IntPtr width_nativeObj, IntPtr prec_nativeObj);
  251. [DllImport(LIBNAME)]
  252. private static extern void imgproc_LineSegmentDetector_detect_12(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr lines_nativeObj, IntPtr width_nativeObj);
  253. [DllImport(LIBNAME)]
  254. private static extern void imgproc_LineSegmentDetector_detect_13(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr lines_nativeObj);
  255. // C++: void cv::LineSegmentDetector::drawSegments(Mat& image, Mat lines)
  256. [DllImport(LIBNAME)]
  257. private static extern void imgproc_LineSegmentDetector_drawSegments_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr lines_nativeObj);
  258. // C++: int cv::LineSegmentDetector::compareSegments(Size size, Mat lines1, Mat lines2, Mat& image = Mat())
  259. [DllImport(LIBNAME)]
  260. private static extern int imgproc_LineSegmentDetector_compareSegments_10(IntPtr nativeObj, double size_width, double size_height, IntPtr lines1_nativeObj, IntPtr lines2_nativeObj, IntPtr image_nativeObj);
  261. [DllImport(LIBNAME)]
  262. private static extern int imgproc_LineSegmentDetector_compareSegments_11(IntPtr nativeObj, double size_width, double size_height, IntPtr lines1_nativeObj, IntPtr lines2_nativeObj);
  263. // native support for java finalize()
  264. [DllImport(LIBNAME)]
  265. private static extern void imgproc_LineSegmentDetector_delete(IntPtr nativeObj);
  266. }
  267. }