IntelligentScissorsMB.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 IntelligentScissorsMB
  9. /**
  10. * Intelligent Scissors image segmentation
  11. *
  12. * This class is used to find the path (contour) between two points
  13. * which can be used for image segmentation.
  14. *
  15. * Usage example:
  16. * SNIPPET: snippets/imgproc_segmentation.cpp usage_example_intelligent_scissors
  17. *
  18. * Reference: <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.138.3811&rep=rep1&type=pdf">"Intelligent Scissors for Image Composition"</a>
  19. * algorithm designed by Eric N. Mortensen and William A. Barrett, Brigham Young University
  20. * CITE: Mortensen95intelligentscissors
  21. */
  22. public class IntelligentScissorsMB : DisposableOpenCVObject
  23. {
  24. protected override void Dispose(bool disposing)
  25. {
  26. try
  27. {
  28. if (disposing)
  29. {
  30. }
  31. if (IsEnabledDispose)
  32. {
  33. if (nativeObj != IntPtr.Zero)
  34. imgproc_IntelligentScissorsMB_delete(nativeObj);
  35. nativeObj = IntPtr.Zero;
  36. }
  37. }
  38. finally
  39. {
  40. base.Dispose(disposing);
  41. }
  42. }
  43. protected internal IntelligentScissorsMB(IntPtr addr) : base(addr) { }
  44. public IntPtr getNativeObjAddr() { return nativeObj; }
  45. // internal usage only
  46. public static IntelligentScissorsMB __fromPtr__(IntPtr addr) { return new IntelligentScissorsMB(addr); }
  47. //
  48. // C++: cv::segmentation::IntelligentScissorsMB::IntelligentScissorsMB()
  49. //
  50. public IntelligentScissorsMB()
  51. {
  52. nativeObj = DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_IntelligentScissorsMB_10());
  53. }
  54. //
  55. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setWeights(float weight_non_edge, float weight_gradient_direction, float weight_gradient_magnitude)
  56. //
  57. /**
  58. * Specify weights of feature functions
  59. *
  60. * Consider keeping weights normalized (sum of weights equals to 1.0)
  61. * Discrete dynamic programming (DP) goal is minimization of costs between pixels.
  62. *
  63. * param weight_non_edge Specify cost of non-edge pixels (default: 0.43f)
  64. * param weight_gradient_direction Specify cost of gradient direction function (default: 0.43f)
  65. * param weight_gradient_magnitude Specify cost of gradient magnitude function (default: 0.14f)
  66. * return automatically generated
  67. */
  68. public IntelligentScissorsMB setWeights(float weight_non_edge, float weight_gradient_direction, float weight_gradient_magnitude)
  69. {
  70. ThrowIfDisposed();
  71. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setWeights_10(nativeObj, weight_non_edge, weight_gradient_direction, weight_gradient_magnitude)));
  72. }
  73. //
  74. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setGradientMagnitudeMaxLimit(float gradient_magnitude_threshold_max = 0.0f)
  75. //
  76. /**
  77. * Specify gradient magnitude max value threshold
  78. *
  79. * Zero limit value is used to disable gradient magnitude thresholding (default behavior, as described in original article).
  80. * Otherwize pixels with {code gradient magnitude >= threshold} have zero cost.
  81. *
  82. * <b>Note:</b> Thresholding should be used for images with irregular regions (to avoid stuck on parameters from high-contract areas, like embedded logos).
  83. *
  84. * param gradient_magnitude_threshold_max Specify gradient magnitude max value threshold (default: 0, disabled)
  85. * return automatically generated
  86. */
  87. public IntelligentScissorsMB setGradientMagnitudeMaxLimit(float gradient_magnitude_threshold_max)
  88. {
  89. ThrowIfDisposed();
  90. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setGradientMagnitudeMaxLimit_10(nativeObj, gradient_magnitude_threshold_max)));
  91. }
  92. /**
  93. * Specify gradient magnitude max value threshold
  94. *
  95. * Zero limit value is used to disable gradient magnitude thresholding (default behavior, as described in original article).
  96. * Otherwize pixels with {code gradient magnitude &gt;= threshold} have zero cost.
  97. *
  98. * <b>Note:</b> Thresholding should be used for images with irregular regions (to avoid stuck on parameters from high-contract areas, like embedded logos).
  99. *
  100. * return automatically generated
  101. */
  102. public IntelligentScissorsMB setGradientMagnitudeMaxLimit()
  103. {
  104. ThrowIfDisposed();
  105. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setGradientMagnitudeMaxLimit_11(nativeObj)));
  106. }
  107. //
  108. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setEdgeFeatureZeroCrossingParameters(float gradient_magnitude_min_value = 0.0f)
  109. //
  110. /**
  111. * Switch to "Laplacian Zero-Crossing" edge feature extractor and specify its parameters
  112. *
  113. * This feature extractor is used by default according to article.
  114. *
  115. * Implementation has additional filtering for regions with low-amplitude noise.
  116. * This filtering is enabled through parameter of minimal gradient amplitude (use some small value 4, 8, 16).
  117. *
  118. * <b>Note:</b> Current implementation of this feature extractor is based on processing of grayscale images (color image is converted to grayscale image first).
  119. *
  120. * <b>Note:</b> Canny edge detector is a bit slower, but provides better results (especially on color images): use setEdgeFeatureCannyParameters().
  121. *
  122. * param gradient_magnitude_min_value Minimal gradient magnitude value for edge pixels (default: 0, check is disabled)
  123. * return automatically generated
  124. */
  125. public IntelligentScissorsMB setEdgeFeatureZeroCrossingParameters(float gradient_magnitude_min_value)
  126. {
  127. ThrowIfDisposed();
  128. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setEdgeFeatureZeroCrossingParameters_10(nativeObj, gradient_magnitude_min_value)));
  129. }
  130. /**
  131. * Switch to "Laplacian Zero-Crossing" edge feature extractor and specify its parameters
  132. *
  133. * This feature extractor is used by default according to article.
  134. *
  135. * Implementation has additional filtering for regions with low-amplitude noise.
  136. * This filtering is enabled through parameter of minimal gradient amplitude (use some small value 4, 8, 16).
  137. *
  138. * <b>Note:</b> Current implementation of this feature extractor is based on processing of grayscale images (color image is converted to grayscale image first).
  139. *
  140. * <b>Note:</b> Canny edge detector is a bit slower, but provides better results (especially on color images): use setEdgeFeatureCannyParameters().
  141. *
  142. * return automatically generated
  143. */
  144. public IntelligentScissorsMB setEdgeFeatureZeroCrossingParameters()
  145. {
  146. ThrowIfDisposed();
  147. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setEdgeFeatureZeroCrossingParameters_11(nativeObj)));
  148. }
  149. //
  150. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setEdgeFeatureCannyParameters(double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false)
  151. //
  152. /**
  153. * Switch edge feature extractor to use Canny edge detector
  154. *
  155. * <b>Note:</b> "Laplacian Zero-Crossing" feature extractor is used by default (following to original article)
  156. *
  157. * SEE: Canny
  158. * param threshold1 automatically generated
  159. * param threshold2 automatically generated
  160. * param apertureSize automatically generated
  161. * param L2gradient automatically generated
  162. * return automatically generated
  163. */
  164. public IntelligentScissorsMB setEdgeFeatureCannyParameters(double threshold1, double threshold2, int apertureSize, bool L2gradient)
  165. {
  166. ThrowIfDisposed();
  167. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_10(nativeObj, threshold1, threshold2, apertureSize, L2gradient)));
  168. }
  169. /**
  170. * Switch edge feature extractor to use Canny edge detector
  171. *
  172. * <b>Note:</b> "Laplacian Zero-Crossing" feature extractor is used by default (following to original article)
  173. *
  174. * SEE: Canny
  175. * param threshold1 automatically generated
  176. * param threshold2 automatically generated
  177. * param apertureSize automatically generated
  178. * return automatically generated
  179. */
  180. public IntelligentScissorsMB setEdgeFeatureCannyParameters(double threshold1, double threshold2, int apertureSize)
  181. {
  182. ThrowIfDisposed();
  183. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_11(nativeObj, threshold1, threshold2, apertureSize)));
  184. }
  185. /**
  186. * Switch edge feature extractor to use Canny edge detector
  187. *
  188. * <b>Note:</b> "Laplacian Zero-Crossing" feature extractor is used by default (following to original article)
  189. *
  190. * SEE: Canny
  191. * param threshold1 automatically generated
  192. * param threshold2 automatically generated
  193. * return automatically generated
  194. */
  195. public IntelligentScissorsMB setEdgeFeatureCannyParameters(double threshold1, double threshold2)
  196. {
  197. ThrowIfDisposed();
  198. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_12(nativeObj, threshold1, threshold2)));
  199. }
  200. //
  201. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::applyImage(Mat image)
  202. //
  203. /**
  204. * Specify input image and extract image features
  205. *
  206. * param image input image. Type is #CV_8UC1 / #CV_8UC3
  207. * return automatically generated
  208. */
  209. public IntelligentScissorsMB applyImage(Mat image)
  210. {
  211. ThrowIfDisposed();
  212. if (image != null) image.ThrowIfDisposed();
  213. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_applyImage_10(nativeObj, image.nativeObj)));
  214. }
  215. //
  216. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::applyImageFeatures(Mat non_edge, Mat gradient_direction, Mat gradient_magnitude, Mat image = Mat())
  217. //
  218. /**
  219. * Specify custom features of input image
  220. *
  221. * Customized advanced variant of applyImage() call.
  222. *
  223. * param non_edge Specify cost of non-edge pixels. Type is CV_8UC1. Expected values are {code {0, 1}}.
  224. * param gradient_direction Specify gradient direction feature. Type is CV_32FC2. Values are expected to be normalized: {code x^2 + y^2 == 1}
  225. * param gradient_magnitude Specify cost of gradient magnitude function: Type is CV_32FC1. Values should be in range {code [0, 1]}.
  226. * param image <b>Optional parameter</b>. Must be specified if subset of features is specified (non-specified features are calculated internally)
  227. * return automatically generated
  228. */
  229. public IntelligentScissorsMB applyImageFeatures(Mat non_edge, Mat gradient_direction, Mat gradient_magnitude, Mat image)
  230. {
  231. ThrowIfDisposed();
  232. if (non_edge != null) non_edge.ThrowIfDisposed();
  233. if (gradient_direction != null) gradient_direction.ThrowIfDisposed();
  234. if (gradient_magnitude != null) gradient_magnitude.ThrowIfDisposed();
  235. if (image != null) image.ThrowIfDisposed();
  236. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_applyImageFeatures_10(nativeObj, non_edge.nativeObj, gradient_direction.nativeObj, gradient_magnitude.nativeObj, image.nativeObj)));
  237. }
  238. /**
  239. * Specify custom features of input image
  240. *
  241. * Customized advanced variant of applyImage() call.
  242. *
  243. * param non_edge Specify cost of non-edge pixels. Type is CV_8UC1. Expected values are {code {0, 1}}.
  244. * param gradient_direction Specify gradient direction feature. Type is CV_32FC2. Values are expected to be normalized: {code x^2 + y^2 == 1}
  245. * param gradient_magnitude Specify cost of gradient magnitude function: Type is CV_32FC1. Values should be in range {code [0, 1]}.
  246. * return automatically generated
  247. */
  248. public IntelligentScissorsMB applyImageFeatures(Mat non_edge, Mat gradient_direction, Mat gradient_magnitude)
  249. {
  250. ThrowIfDisposed();
  251. if (non_edge != null) non_edge.ThrowIfDisposed();
  252. if (gradient_direction != null) gradient_direction.ThrowIfDisposed();
  253. if (gradient_magnitude != null) gradient_magnitude.ThrowIfDisposed();
  254. return new IntelligentScissorsMB(DisposableObject.ThrowIfNullIntPtr(imgproc_IntelligentScissorsMB_applyImageFeatures_11(nativeObj, non_edge.nativeObj, gradient_direction.nativeObj, gradient_magnitude.nativeObj)));
  255. }
  256. //
  257. // C++: void cv::segmentation::IntelligentScissorsMB::buildMap(Point sourcePt)
  258. //
  259. /**
  260. * Prepares a map of optimal paths for the given source point on the image
  261. *
  262. * <b>Note:</b> applyImage() / applyImageFeatures() must be called before this call
  263. *
  264. * param sourcePt The source point used to find the paths
  265. */
  266. public void buildMap(Point sourcePt)
  267. {
  268. ThrowIfDisposed();
  269. imgproc_IntelligentScissorsMB_buildMap_10(nativeObj, sourcePt.x, sourcePt.y);
  270. }
  271. //
  272. // C++: void cv::segmentation::IntelligentScissorsMB::getContour(Point targetPt, Mat& contour, bool backward = false)
  273. //
  274. /**
  275. * Extracts optimal contour for the given target point on the image
  276. *
  277. * <b>Note:</b> buildMap() must be called before this call
  278. *
  279. * param targetPt The target point
  280. * param contour The list of pixels which contains optimal path between the source and the target points of the image. Type is CV_32SC2 (compatible with {code std::vector&lt;Point&gt;})
  281. * param backward Flag to indicate reverse order of retrived pixels (use "true" value to fetch points from the target to the source point)
  282. */
  283. public void getContour(Point targetPt, Mat contour, bool backward)
  284. {
  285. ThrowIfDisposed();
  286. if (contour != null) contour.ThrowIfDisposed();
  287. imgproc_IntelligentScissorsMB_getContour_10(nativeObj, targetPt.x, targetPt.y, contour.nativeObj, backward);
  288. }
  289. /**
  290. * Extracts optimal contour for the given target point on the image
  291. *
  292. * <b>Note:</b> buildMap() must be called before this call
  293. *
  294. * param targetPt The target point
  295. * param contour The list of pixels which contains optimal path between the source and the target points of the image. Type is CV_32SC2 (compatible with {code std::vector&lt;Point&gt;})
  296. */
  297. public void getContour(Point targetPt, Mat contour)
  298. {
  299. ThrowIfDisposed();
  300. if (contour != null) contour.ThrowIfDisposed();
  301. imgproc_IntelligentScissorsMB_getContour_11(nativeObj, targetPt.x, targetPt.y, contour.nativeObj);
  302. }
  303. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  304. const string LIBNAME = "__Internal";
  305. #else
  306. const string LIBNAME = "opencvforunity";
  307. #endif
  308. // C++: cv::segmentation::IntelligentScissorsMB::IntelligentScissorsMB()
  309. [DllImport(LIBNAME)]
  310. private static extern IntPtr imgproc_IntelligentScissorsMB_IntelligentScissorsMB_10();
  311. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setWeights(float weight_non_edge, float weight_gradient_direction, float weight_gradient_magnitude)
  312. [DllImport(LIBNAME)]
  313. private static extern IntPtr imgproc_IntelligentScissorsMB_setWeights_10(IntPtr nativeObj, float weight_non_edge, float weight_gradient_direction, float weight_gradient_magnitude);
  314. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setGradientMagnitudeMaxLimit(float gradient_magnitude_threshold_max = 0.0f)
  315. [DllImport(LIBNAME)]
  316. private static extern IntPtr imgproc_IntelligentScissorsMB_setGradientMagnitudeMaxLimit_10(IntPtr nativeObj, float gradient_magnitude_threshold_max);
  317. [DllImport(LIBNAME)]
  318. private static extern IntPtr imgproc_IntelligentScissorsMB_setGradientMagnitudeMaxLimit_11(IntPtr nativeObj);
  319. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setEdgeFeatureZeroCrossingParameters(float gradient_magnitude_min_value = 0.0f)
  320. [DllImport(LIBNAME)]
  321. private static extern IntPtr imgproc_IntelligentScissorsMB_setEdgeFeatureZeroCrossingParameters_10(IntPtr nativeObj, float gradient_magnitude_min_value);
  322. [DllImport(LIBNAME)]
  323. private static extern IntPtr imgproc_IntelligentScissorsMB_setEdgeFeatureZeroCrossingParameters_11(IntPtr nativeObj);
  324. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::setEdgeFeatureCannyParameters(double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false)
  325. [DllImport(LIBNAME)]
  326. private static extern IntPtr imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_10(IntPtr nativeObj, double threshold1, double threshold2, int apertureSize, [MarshalAs(UnmanagedType.U1)] bool L2gradient);
  327. [DllImport(LIBNAME)]
  328. private static extern IntPtr imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_11(IntPtr nativeObj, double threshold1, double threshold2, int apertureSize);
  329. [DllImport(LIBNAME)]
  330. private static extern IntPtr imgproc_IntelligentScissorsMB_setEdgeFeatureCannyParameters_12(IntPtr nativeObj, double threshold1, double threshold2);
  331. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::applyImage(Mat image)
  332. [DllImport(LIBNAME)]
  333. private static extern IntPtr imgproc_IntelligentScissorsMB_applyImage_10(IntPtr nativeObj, IntPtr image_nativeObj);
  334. // C++: IntelligentScissorsMB cv::segmentation::IntelligentScissorsMB::applyImageFeatures(Mat non_edge, Mat gradient_direction, Mat gradient_magnitude, Mat image = Mat())
  335. [DllImport(LIBNAME)]
  336. private static extern IntPtr imgproc_IntelligentScissorsMB_applyImageFeatures_10(IntPtr nativeObj, IntPtr non_edge_nativeObj, IntPtr gradient_direction_nativeObj, IntPtr gradient_magnitude_nativeObj, IntPtr image_nativeObj);
  337. [DllImport(LIBNAME)]
  338. private static extern IntPtr imgproc_IntelligentScissorsMB_applyImageFeatures_11(IntPtr nativeObj, IntPtr non_edge_nativeObj, IntPtr gradient_direction_nativeObj, IntPtr gradient_magnitude_nativeObj);
  339. // C++: void cv::segmentation::IntelligentScissorsMB::buildMap(Point sourcePt)
  340. [DllImport(LIBNAME)]
  341. private static extern void imgproc_IntelligentScissorsMB_buildMap_10(IntPtr nativeObj, double sourcePt_x, double sourcePt_y);
  342. // C++: void cv::segmentation::IntelligentScissorsMB::getContour(Point targetPt, Mat& contour, bool backward = false)
  343. [DllImport(LIBNAME)]
  344. private static extern void imgproc_IntelligentScissorsMB_getContour_10(IntPtr nativeObj, double targetPt_x, double targetPt_y, IntPtr contour_nativeObj, [MarshalAs(UnmanagedType.U1)] bool backward);
  345. [DllImport(LIBNAME)]
  346. private static extern void imgproc_IntelligentScissorsMB_getContour_11(IntPtr nativeObj, double targetPt_x, double targetPt_y, IntPtr contour_nativeObj);
  347. // native support for java finalize()
  348. [DllImport(LIBNAME)]
  349. private static extern void imgproc_IntelligentScissorsMB_delete(IntPtr nativeObj);
  350. }
  351. }