MSER.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.Features2dModule
  7. {
  8. // C++: class MSER
  9. /**
  10. * Maximally stable extremal region extractor
  11. *
  12. * The class encapsulates all the parameters of the %MSER extraction algorithm (see [wiki
  13. * article](http://en.wikipedia.org/wiki/Maximally_stable_extremal_regions)).
  14. *
  15. * <ul>
  16. * <li>
  17. * there are two different implementation of %MSER: one for grey image, one for color image
  18. * </li>
  19. * </ul>
  20. *
  21. * <ul>
  22. * <li>
  23. * the grey image algorithm is taken from: CITE: nister2008linear ; the paper claims to be faster
  24. * than union-find method; it actually get 1.5~2m/s on my centrino L7200 1.2GHz laptop.
  25. * </li>
  26. * </ul>
  27. *
  28. * <ul>
  29. * <li>
  30. * the color image algorithm is taken from: CITE: forssen2007maximally ; it should be much slower
  31. * than grey image method ( 3~4 times )
  32. * </li>
  33. * </ul>
  34. *
  35. * <ul>
  36. * <li>
  37. * (Python) A complete example showing the use of the %MSER detector can be found at samples/python/mser.py
  38. * </li>
  39. * </ul>
  40. */
  41. public class MSER : Feature2D
  42. {
  43. protected override void Dispose(bool disposing)
  44. {
  45. try
  46. {
  47. if (disposing)
  48. {
  49. }
  50. if (IsEnabledDispose)
  51. {
  52. if (nativeObj != IntPtr.Zero)
  53. features2d_MSER_delete(nativeObj);
  54. nativeObj = IntPtr.Zero;
  55. }
  56. }
  57. finally
  58. {
  59. base.Dispose(disposing);
  60. }
  61. }
  62. protected internal MSER(IntPtr addr) : base(addr) { }
  63. // internal usage only
  64. public static new MSER __fromPtr__(IntPtr addr) { return new MSER(addr); }
  65. //
  66. // C++: static Ptr_MSER cv::MSER::create(int delta = 5, int min_area = 60, int max_area = 14400, double max_variation = 0.25, double min_diversity = .2, int max_evolution = 200, double area_threshold = 1.01, double min_margin = 0.003, int edge_blur_size = 5)
  67. //
  68. /**
  69. * Full constructor for %MSER detector
  70. *
  71. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  72. * param min_area prune the area which smaller than minArea
  73. * param max_area prune the area which bigger than maxArea
  74. * param max_variation prune the area have similar size to its children
  75. * param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  76. * param max_evolution for color image, the evolution steps
  77. * param area_threshold for color image, the area threshold to cause re-initialize
  78. * param min_margin for color image, ignore too small margin
  79. * param edge_blur_size for color image, the aperture size for edge blur
  80. * return automatically generated
  81. */
  82. public static MSER create(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold, double min_margin, int edge_blur_size)
  83. {
  84. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_10(delta, min_area, max_area, max_variation, min_diversity, max_evolution, area_threshold, min_margin, edge_blur_size)));
  85. }
  86. /**
  87. * Full constructor for %MSER detector
  88. *
  89. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  90. * param min_area prune the area which smaller than minArea
  91. * param max_area prune the area which bigger than maxArea
  92. * param max_variation prune the area have similar size to its children
  93. * param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  94. * param max_evolution for color image, the evolution steps
  95. * param area_threshold for color image, the area threshold to cause re-initialize
  96. * param min_margin for color image, ignore too small margin
  97. * return automatically generated
  98. */
  99. public static MSER create(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold, double min_margin)
  100. {
  101. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_11(delta, min_area, max_area, max_variation, min_diversity, max_evolution, area_threshold, min_margin)));
  102. }
  103. /**
  104. * Full constructor for %MSER detector
  105. *
  106. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  107. * param min_area prune the area which smaller than minArea
  108. * param max_area prune the area which bigger than maxArea
  109. * param max_variation prune the area have similar size to its children
  110. * param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  111. * param max_evolution for color image, the evolution steps
  112. * param area_threshold for color image, the area threshold to cause re-initialize
  113. * return automatically generated
  114. */
  115. public static MSER create(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold)
  116. {
  117. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_12(delta, min_area, max_area, max_variation, min_diversity, max_evolution, area_threshold)));
  118. }
  119. /**
  120. * Full constructor for %MSER detector
  121. *
  122. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  123. * param min_area prune the area which smaller than minArea
  124. * param max_area prune the area which bigger than maxArea
  125. * param max_variation prune the area have similar size to its children
  126. * param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  127. * param max_evolution for color image, the evolution steps
  128. * return automatically generated
  129. */
  130. public static MSER create(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution)
  131. {
  132. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_13(delta, min_area, max_area, max_variation, min_diversity, max_evolution)));
  133. }
  134. /**
  135. * Full constructor for %MSER detector
  136. *
  137. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  138. * param min_area prune the area which smaller than minArea
  139. * param max_area prune the area which bigger than maxArea
  140. * param max_variation prune the area have similar size to its children
  141. * param min_diversity for color image, trace back to cut off mser with diversity less than min_diversity
  142. * return automatically generated
  143. */
  144. public static MSER create(int delta, int min_area, int max_area, double max_variation, double min_diversity)
  145. {
  146. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_14(delta, min_area, max_area, max_variation, min_diversity)));
  147. }
  148. /**
  149. * Full constructor for %MSER detector
  150. *
  151. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  152. * param min_area prune the area which smaller than minArea
  153. * param max_area prune the area which bigger than maxArea
  154. * param max_variation prune the area have similar size to its children
  155. * return automatically generated
  156. */
  157. public static MSER create(int delta, int min_area, int max_area, double max_variation)
  158. {
  159. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_15(delta, min_area, max_area, max_variation)));
  160. }
  161. /**
  162. * Full constructor for %MSER detector
  163. *
  164. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  165. * param min_area prune the area which smaller than minArea
  166. * param max_area prune the area which bigger than maxArea
  167. * return automatically generated
  168. */
  169. public static MSER create(int delta, int min_area, int max_area)
  170. {
  171. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_16(delta, min_area, max_area)));
  172. }
  173. /**
  174. * Full constructor for %MSER detector
  175. *
  176. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  177. * param min_area prune the area which smaller than minArea
  178. * return automatically generated
  179. */
  180. public static MSER create(int delta, int min_area)
  181. {
  182. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_17(delta, min_area)));
  183. }
  184. /**
  185. * Full constructor for %MSER detector
  186. *
  187. * param delta it compares \((size_{i}-size_{i-delta})/size_{i-delta}\)
  188. * return automatically generated
  189. */
  190. public static MSER create(int delta)
  191. {
  192. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_18(delta)));
  193. }
  194. /**
  195. * Full constructor for %MSER detector
  196. *
  197. * return automatically generated
  198. */
  199. public static MSER create()
  200. {
  201. return MSER.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_create_19()));
  202. }
  203. //
  204. // C++: void cv::MSER::detectRegions(Mat image, vector_vector_Point& msers, vector_Rect& bboxes)
  205. //
  206. /**
  207. * Detect %MSER regions
  208. *
  209. * param image input image (8UC1, 8UC3 or 8UC4, must be greater or equal than 3x3)
  210. * param msers resulting list of point sets
  211. * param bboxes resulting bounding boxes
  212. */
  213. public void detectRegions(Mat image, List<MatOfPoint> msers, MatOfRect bboxes)
  214. {
  215. ThrowIfDisposed();
  216. if (image != null) image.ThrowIfDisposed();
  217. if (bboxes != null) bboxes.ThrowIfDisposed();
  218. Mat msers_mat = new Mat();
  219. Mat bboxes_mat = bboxes;
  220. features2d_MSER_detectRegions_10(nativeObj, image.nativeObj, msers_mat.nativeObj, bboxes_mat.nativeObj);
  221. Converters.Mat_to_vector_vector_Point(msers_mat, msers);
  222. msers_mat.release();
  223. }
  224. //
  225. // C++: void cv::MSER::setDelta(int delta)
  226. //
  227. public void setDelta(int delta)
  228. {
  229. ThrowIfDisposed();
  230. features2d_MSER_setDelta_10(nativeObj, delta);
  231. }
  232. //
  233. // C++: int cv::MSER::getDelta()
  234. //
  235. public int getDelta()
  236. {
  237. ThrowIfDisposed();
  238. return features2d_MSER_getDelta_10(nativeObj);
  239. }
  240. //
  241. // C++: void cv::MSER::setMinArea(int minArea)
  242. //
  243. public void setMinArea(int minArea)
  244. {
  245. ThrowIfDisposed();
  246. features2d_MSER_setMinArea_10(nativeObj, minArea);
  247. }
  248. //
  249. // C++: int cv::MSER::getMinArea()
  250. //
  251. public int getMinArea()
  252. {
  253. ThrowIfDisposed();
  254. return features2d_MSER_getMinArea_10(nativeObj);
  255. }
  256. //
  257. // C++: void cv::MSER::setMaxArea(int maxArea)
  258. //
  259. public void setMaxArea(int maxArea)
  260. {
  261. ThrowIfDisposed();
  262. features2d_MSER_setMaxArea_10(nativeObj, maxArea);
  263. }
  264. //
  265. // C++: int cv::MSER::getMaxArea()
  266. //
  267. public int getMaxArea()
  268. {
  269. ThrowIfDisposed();
  270. return features2d_MSER_getMaxArea_10(nativeObj);
  271. }
  272. //
  273. // C++: void cv::MSER::setMaxVariation(double maxVariation)
  274. //
  275. public void setMaxVariation(double maxVariation)
  276. {
  277. ThrowIfDisposed();
  278. features2d_MSER_setMaxVariation_10(nativeObj, maxVariation);
  279. }
  280. //
  281. // C++: double cv::MSER::getMaxVariation()
  282. //
  283. public double getMaxVariation()
  284. {
  285. ThrowIfDisposed();
  286. return features2d_MSER_getMaxVariation_10(nativeObj);
  287. }
  288. //
  289. // C++: void cv::MSER::setMinDiversity(double minDiversity)
  290. //
  291. public void setMinDiversity(double minDiversity)
  292. {
  293. ThrowIfDisposed();
  294. features2d_MSER_setMinDiversity_10(nativeObj, minDiversity);
  295. }
  296. //
  297. // C++: double cv::MSER::getMinDiversity()
  298. //
  299. public double getMinDiversity()
  300. {
  301. ThrowIfDisposed();
  302. return features2d_MSER_getMinDiversity_10(nativeObj);
  303. }
  304. //
  305. // C++: void cv::MSER::setMaxEvolution(int maxEvolution)
  306. //
  307. public void setMaxEvolution(int maxEvolution)
  308. {
  309. ThrowIfDisposed();
  310. features2d_MSER_setMaxEvolution_10(nativeObj, maxEvolution);
  311. }
  312. //
  313. // C++: int cv::MSER::getMaxEvolution()
  314. //
  315. public int getMaxEvolution()
  316. {
  317. ThrowIfDisposed();
  318. return features2d_MSER_getMaxEvolution_10(nativeObj);
  319. }
  320. //
  321. // C++: void cv::MSER::setAreaThreshold(double areaThreshold)
  322. //
  323. public void setAreaThreshold(double areaThreshold)
  324. {
  325. ThrowIfDisposed();
  326. features2d_MSER_setAreaThreshold_10(nativeObj, areaThreshold);
  327. }
  328. //
  329. // C++: double cv::MSER::getAreaThreshold()
  330. //
  331. public double getAreaThreshold()
  332. {
  333. ThrowIfDisposed();
  334. return features2d_MSER_getAreaThreshold_10(nativeObj);
  335. }
  336. //
  337. // C++: void cv::MSER::setMinMargin(double min_margin)
  338. //
  339. public void setMinMargin(double min_margin)
  340. {
  341. ThrowIfDisposed();
  342. features2d_MSER_setMinMargin_10(nativeObj, min_margin);
  343. }
  344. //
  345. // C++: double cv::MSER::getMinMargin()
  346. //
  347. public double getMinMargin()
  348. {
  349. ThrowIfDisposed();
  350. return features2d_MSER_getMinMargin_10(nativeObj);
  351. }
  352. //
  353. // C++: void cv::MSER::setEdgeBlurSize(int edge_blur_size)
  354. //
  355. public void setEdgeBlurSize(int edge_blur_size)
  356. {
  357. ThrowIfDisposed();
  358. features2d_MSER_setEdgeBlurSize_10(nativeObj, edge_blur_size);
  359. }
  360. //
  361. // C++: int cv::MSER::getEdgeBlurSize()
  362. //
  363. public int getEdgeBlurSize()
  364. {
  365. ThrowIfDisposed();
  366. return features2d_MSER_getEdgeBlurSize_10(nativeObj);
  367. }
  368. //
  369. // C++: void cv::MSER::setPass2Only(bool f)
  370. //
  371. public void setPass2Only(bool f)
  372. {
  373. ThrowIfDisposed();
  374. features2d_MSER_setPass2Only_10(nativeObj, f);
  375. }
  376. //
  377. // C++: bool cv::MSER::getPass2Only()
  378. //
  379. public bool getPass2Only()
  380. {
  381. ThrowIfDisposed();
  382. return features2d_MSER_getPass2Only_10(nativeObj);
  383. }
  384. //
  385. // C++: String cv::MSER::getDefaultName()
  386. //
  387. public override string getDefaultName()
  388. {
  389. ThrowIfDisposed();
  390. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(features2d_MSER_getDefaultName_10(nativeObj)));
  391. return retVal;
  392. }
  393. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  394. const string LIBNAME = "__Internal";
  395. #else
  396. const string LIBNAME = "opencvforunity";
  397. #endif
  398. // C++: static Ptr_MSER cv::MSER::create(int delta = 5, int min_area = 60, int max_area = 14400, double max_variation = 0.25, double min_diversity = .2, int max_evolution = 200, double area_threshold = 1.01, double min_margin = 0.003, int edge_blur_size = 5)
  399. [DllImport(LIBNAME)]
  400. private static extern IntPtr features2d_MSER_create_10(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold, double min_margin, int edge_blur_size);
  401. [DllImport(LIBNAME)]
  402. private static extern IntPtr features2d_MSER_create_11(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold, double min_margin);
  403. [DllImport(LIBNAME)]
  404. private static extern IntPtr features2d_MSER_create_12(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution, double area_threshold);
  405. [DllImport(LIBNAME)]
  406. private static extern IntPtr features2d_MSER_create_13(int delta, int min_area, int max_area, double max_variation, double min_diversity, int max_evolution);
  407. [DllImport(LIBNAME)]
  408. private static extern IntPtr features2d_MSER_create_14(int delta, int min_area, int max_area, double max_variation, double min_diversity);
  409. [DllImport(LIBNAME)]
  410. private static extern IntPtr features2d_MSER_create_15(int delta, int min_area, int max_area, double max_variation);
  411. [DllImport(LIBNAME)]
  412. private static extern IntPtr features2d_MSER_create_16(int delta, int min_area, int max_area);
  413. [DllImport(LIBNAME)]
  414. private static extern IntPtr features2d_MSER_create_17(int delta, int min_area);
  415. [DllImport(LIBNAME)]
  416. private static extern IntPtr features2d_MSER_create_18(int delta);
  417. [DllImport(LIBNAME)]
  418. private static extern IntPtr features2d_MSER_create_19();
  419. // C++: void cv::MSER::detectRegions(Mat image, vector_vector_Point& msers, vector_Rect& bboxes)
  420. [DllImport(LIBNAME)]
  421. private static extern void features2d_MSER_detectRegions_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr msers_mat_nativeObj, IntPtr bboxes_mat_nativeObj);
  422. // C++: void cv::MSER::setDelta(int delta)
  423. [DllImport(LIBNAME)]
  424. private static extern void features2d_MSER_setDelta_10(IntPtr nativeObj, int delta);
  425. // C++: int cv::MSER::getDelta()
  426. [DllImport(LIBNAME)]
  427. private static extern int features2d_MSER_getDelta_10(IntPtr nativeObj);
  428. // C++: void cv::MSER::setMinArea(int minArea)
  429. [DllImport(LIBNAME)]
  430. private static extern void features2d_MSER_setMinArea_10(IntPtr nativeObj, int minArea);
  431. // C++: int cv::MSER::getMinArea()
  432. [DllImport(LIBNAME)]
  433. private static extern int features2d_MSER_getMinArea_10(IntPtr nativeObj);
  434. // C++: void cv::MSER::setMaxArea(int maxArea)
  435. [DllImport(LIBNAME)]
  436. private static extern void features2d_MSER_setMaxArea_10(IntPtr nativeObj, int maxArea);
  437. // C++: int cv::MSER::getMaxArea()
  438. [DllImport(LIBNAME)]
  439. private static extern int features2d_MSER_getMaxArea_10(IntPtr nativeObj);
  440. // C++: void cv::MSER::setMaxVariation(double maxVariation)
  441. [DllImport(LIBNAME)]
  442. private static extern void features2d_MSER_setMaxVariation_10(IntPtr nativeObj, double maxVariation);
  443. // C++: double cv::MSER::getMaxVariation()
  444. [DllImport(LIBNAME)]
  445. private static extern double features2d_MSER_getMaxVariation_10(IntPtr nativeObj);
  446. // C++: void cv::MSER::setMinDiversity(double minDiversity)
  447. [DllImport(LIBNAME)]
  448. private static extern void features2d_MSER_setMinDiversity_10(IntPtr nativeObj, double minDiversity);
  449. // C++: double cv::MSER::getMinDiversity()
  450. [DllImport(LIBNAME)]
  451. private static extern double features2d_MSER_getMinDiversity_10(IntPtr nativeObj);
  452. // C++: void cv::MSER::setMaxEvolution(int maxEvolution)
  453. [DllImport(LIBNAME)]
  454. private static extern void features2d_MSER_setMaxEvolution_10(IntPtr nativeObj, int maxEvolution);
  455. // C++: int cv::MSER::getMaxEvolution()
  456. [DllImport(LIBNAME)]
  457. private static extern int features2d_MSER_getMaxEvolution_10(IntPtr nativeObj);
  458. // C++: void cv::MSER::setAreaThreshold(double areaThreshold)
  459. [DllImport(LIBNAME)]
  460. private static extern void features2d_MSER_setAreaThreshold_10(IntPtr nativeObj, double areaThreshold);
  461. // C++: double cv::MSER::getAreaThreshold()
  462. [DllImport(LIBNAME)]
  463. private static extern double features2d_MSER_getAreaThreshold_10(IntPtr nativeObj);
  464. // C++: void cv::MSER::setMinMargin(double min_margin)
  465. [DllImport(LIBNAME)]
  466. private static extern void features2d_MSER_setMinMargin_10(IntPtr nativeObj, double min_margin);
  467. // C++: double cv::MSER::getMinMargin()
  468. [DllImport(LIBNAME)]
  469. private static extern double features2d_MSER_getMinMargin_10(IntPtr nativeObj);
  470. // C++: void cv::MSER::setEdgeBlurSize(int edge_blur_size)
  471. [DllImport(LIBNAME)]
  472. private static extern void features2d_MSER_setEdgeBlurSize_10(IntPtr nativeObj, int edge_blur_size);
  473. // C++: int cv::MSER::getEdgeBlurSize()
  474. [DllImport(LIBNAME)]
  475. private static extern int features2d_MSER_getEdgeBlurSize_10(IntPtr nativeObj);
  476. // C++: void cv::MSER::setPass2Only(bool f)
  477. [DllImport(LIBNAME)]
  478. private static extern void features2d_MSER_setPass2Only_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool f);
  479. // C++: bool cv::MSER::getPass2Only()
  480. [DllImport(LIBNAME)]
  481. [return: MarshalAs(UnmanagedType.U1)]
  482. private static extern bool features2d_MSER_getPass2Only_10(IntPtr nativeObj);
  483. // C++: String cv::MSER::getDefaultName()
  484. [DllImport(LIBNAME)]
  485. private static extern IntPtr features2d_MSER_getDefaultName_10(IntPtr nativeObj);
  486. // native support for java finalize()
  487. [DllImport(LIBNAME)]
  488. private static extern void features2d_MSER_delete(IntPtr nativeObj);
  489. }
  490. }