Moments.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. namespace OpenCVForUnity.ImgprocModule
  5. {
  6. //javadoc:Moments
  7. [System.Serializable]
  8. public class Moments
  9. {
  10. public double m00;
  11. public double m10;
  12. public double m01;
  13. public double m20;
  14. public double m11;
  15. public double m02;
  16. public double m30;
  17. public double m21;
  18. public double m12;
  19. public double m03;
  20. public double mu20;
  21. public double mu11;
  22. public double mu02;
  23. public double mu30;
  24. public double mu21;
  25. public double mu12;
  26. public double mu03;
  27. public double nu20;
  28. public double nu11;
  29. public double nu02;
  30. public double nu30;
  31. public double nu21;
  32. public double nu12;
  33. public double nu03;
  34. public Moments(
  35. double m00,
  36. double m10,
  37. double m01,
  38. double m20,
  39. double m11,
  40. double m02,
  41. double m30,
  42. double m21,
  43. double m12,
  44. double m03)
  45. {
  46. this.m00 = m00;
  47. this.m10 = m10;
  48. this.m01 = m01;
  49. this.m20 = m20;
  50. this.m11 = m11;
  51. this.m02 = m02;
  52. this.m30 = m30;
  53. this.m21 = m21;
  54. this.m12 = m12;
  55. this.m03 = m03;
  56. this.completeState();
  57. }
  58. public Moments()
  59. : this(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  60. {
  61. }
  62. public Moments(double[] vals)
  63. {
  64. set(vals);
  65. }
  66. public void set(double[] vals)
  67. {
  68. if (vals != null)
  69. {
  70. m00 = vals.Length > 0 ? vals[0] : 0;
  71. m10 = vals.Length > 1 ? vals[1] : 0;
  72. m01 = vals.Length > 2 ? vals[2] : 0;
  73. m20 = vals.Length > 3 ? vals[3] : 0;
  74. m11 = vals.Length > 4 ? vals[4] : 0;
  75. m02 = vals.Length > 5 ? vals[5] : 0;
  76. m30 = vals.Length > 6 ? vals[6] : 0;
  77. m21 = vals.Length > 7 ? vals[7] : 0;
  78. m12 = vals.Length > 8 ? vals[8] : 0;
  79. m03 = vals.Length > 9 ? vals[9] : 0;
  80. this.completeState();
  81. }
  82. else
  83. {
  84. m00 = 0;
  85. m10 = 0;
  86. m01 = 0;
  87. m20 = 0;
  88. m11 = 0;
  89. m02 = 0;
  90. m30 = 0;
  91. m21 = 0;
  92. m12 = 0;
  93. m03 = 0;
  94. mu20 = 0;
  95. mu11 = 0;
  96. mu02 = 0;
  97. mu30 = 0;
  98. mu21 = 0;
  99. mu12 = 0;
  100. mu03 = 0;
  101. nu20 = 0;
  102. nu11 = 0;
  103. nu02 = 0;
  104. nu30 = 0;
  105. nu21 = 0;
  106. nu12 = 0;
  107. nu03 = 0;
  108. }
  109. }
  110. //@Override
  111. public override string ToString()
  112. {
  113. return "Moments [ " +
  114. "\n" +
  115. "m00=" + m00 + ", " +
  116. "\n" +
  117. "m10=" + m10 + ", " +
  118. "m01=" + m01 + ", " +
  119. "\n" +
  120. "m20=" + m20 + ", " +
  121. "m11=" + m11 + ", " +
  122. "m02=" + m02 + ", " +
  123. "\n" +
  124. "m30=" + m30 + ", " +
  125. "m21=" + m21 + ", " +
  126. "m12=" + m12 + ", " +
  127. "m03=" + m03 + ", " +
  128. "\n" +
  129. "mu20=" + mu20 + ", " +
  130. "mu11=" + mu11 + ", " +
  131. "mu02=" + mu02 + ", " +
  132. "\n" +
  133. "mu30=" + mu30 + ", " +
  134. "mu21=" + mu21 + ", " +
  135. "mu12=" + mu12 + ", " +
  136. "mu03=" + mu03 + ", " +
  137. "\n" +
  138. "nu20=" + nu20 + ", " +
  139. "nu11=" + nu11 + ", " +
  140. "nu02=" + nu02 + ", " +
  141. "\n" +
  142. "nu30=" + nu30 + ", " +
  143. "nu21=" + nu21 + ", " +
  144. "nu12=" + nu12 + ", " +
  145. "nu03=" + nu03 + ", " +
  146. "\n]";
  147. }
  148. protected void completeState()
  149. {
  150. double cx = 0, cy = 0;
  151. double mu20, mu11, mu02;
  152. double inv_m00 = 0.0;
  153. if (System.Math.Abs(this.m00) > 0.00000001)
  154. {
  155. inv_m00 = 1.0 / this.m00;
  156. cx = this.m10 * inv_m00;
  157. cy = this.m01 * inv_m00;
  158. }
  159. // mu20 = m20 - m10*cx
  160. mu20 = this.m20 - this.m10 * cx;
  161. // mu11 = m11 - m10*cy
  162. mu11 = this.m11 - this.m10 * cy;
  163. // mu02 = m02 - m01*cy
  164. mu02 = this.m02 - this.m01 * cy;
  165. this.mu20 = mu20;
  166. this.mu11 = mu11;
  167. this.mu02 = mu02;
  168. // mu30 = m30 - cx*(3*mu20 + cx*m10)
  169. this.mu30 = this.m30 - cx * (3 * mu20 + cx * this.m10);
  170. mu11 += mu11;
  171. // mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20
  172. this.mu21 = this.m21 - cx * (mu11 + cx * this.m01) - cy * mu20;
  173. // mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02
  174. this.mu12 = this.m12 - cy * (mu11 + cy * this.m10) - cx * mu02;
  175. // mu03 = m03 - cy*(3*mu02 + cy*m01)
  176. this.mu03 = this.m03 - cy * (3 * mu02 + cy * this.m01);
  177. double inv_sqrt_m00 = System.Math.Sqrt(System.Math.Abs(inv_m00));
  178. double s2 = inv_m00 * inv_m00, s3 = s2 * inv_sqrt_m00;
  179. this.nu20 = this.mu20 * s2;
  180. this.nu11 = this.mu11 * s2;
  181. this.nu02 = this.mu02 * s2;
  182. this.nu30 = this.mu30 * s3;
  183. this.nu21 = this.mu21 * s3;
  184. this.nu12 = this.mu12 * s3;
  185. this.nu03 = this.mu03 * s3;
  186. }
  187. public double get_m00()
  188. {
  189. return this.m00;
  190. }
  191. public double get_m10()
  192. {
  193. return this.m10;
  194. }
  195. public double get_m01()
  196. {
  197. return this.m01;
  198. }
  199. public double get_m20()
  200. {
  201. return this.m20;
  202. }
  203. public double get_m11()
  204. {
  205. return this.m11;
  206. }
  207. public double get_m02()
  208. {
  209. return this.m02;
  210. }
  211. public double get_m30()
  212. {
  213. return this.m30;
  214. }
  215. public double get_m21()
  216. {
  217. return this.m21;
  218. }
  219. public double get_m12()
  220. {
  221. return this.m12;
  222. }
  223. public double get_m03()
  224. {
  225. return this.m03;
  226. }
  227. public double get_mu20()
  228. {
  229. return this.mu20;
  230. }
  231. public double get_mu11()
  232. {
  233. return this.mu11;
  234. }
  235. public double get_mu02()
  236. {
  237. return this.mu02;
  238. }
  239. public double get_mu30()
  240. {
  241. return this.mu30;
  242. }
  243. public double get_mu21()
  244. {
  245. return this.mu21;
  246. }
  247. public double get_mu12()
  248. {
  249. return this.mu12;
  250. }
  251. public double get_mu03()
  252. {
  253. return this.mu03;
  254. }
  255. public double get_nu20()
  256. {
  257. return this.nu20;
  258. }
  259. public double get_nu11()
  260. {
  261. return this.nu11;
  262. }
  263. public double get_nu02()
  264. {
  265. return this.nu02;
  266. }
  267. public double get_nu30()
  268. {
  269. return this.nu30;
  270. }
  271. public double get_nu21()
  272. {
  273. return this.nu21;
  274. }
  275. public double get_nu12()
  276. {
  277. return this.nu12;
  278. }
  279. public double get_nu03()
  280. {
  281. return this.nu03;
  282. }
  283. public void set_m00(double m00)
  284. {
  285. this.m00 = m00;
  286. }
  287. public void set_m10(double m10)
  288. {
  289. this.m10 = m10;
  290. }
  291. public void set_m01(double m01)
  292. {
  293. this.m01 = m01;
  294. }
  295. public void set_m20(double m20)
  296. {
  297. this.m20 = m20;
  298. }
  299. public void set_m11(double m11)
  300. {
  301. this.m11 = m11;
  302. }
  303. public void set_m02(double m02)
  304. {
  305. this.m02 = m02;
  306. }
  307. public void set_m30(double m30)
  308. {
  309. this.m30 = m30;
  310. }
  311. public void set_m21(double m21)
  312. {
  313. this.m21 = m21;
  314. }
  315. public void set_m12(double m12)
  316. {
  317. this.m12 = m12;
  318. }
  319. public void set_m03(double m03)
  320. {
  321. this.m03 = m03;
  322. }
  323. public void set_mu20(double mu20)
  324. {
  325. this.mu20 = mu20;
  326. }
  327. public void set_mu11(double mu11)
  328. {
  329. this.mu11 = mu11;
  330. }
  331. public void set_mu02(double mu02)
  332. {
  333. this.mu02 = mu02;
  334. }
  335. public void set_mu30(double mu30)
  336. {
  337. this.mu30 = mu30;
  338. }
  339. public void set_mu21(double mu21)
  340. {
  341. this.mu21 = mu21;
  342. }
  343. public void set_mu12(double mu12)
  344. {
  345. this.mu12 = mu12;
  346. }
  347. public void set_mu03(double mu03)
  348. {
  349. this.mu03 = mu03;
  350. }
  351. public void set_nu20(double nu20)
  352. {
  353. this.nu20 = nu20;
  354. }
  355. public void set_nu11(double nu11)
  356. {
  357. this.nu11 = nu11;
  358. }
  359. public void set_nu02(double nu02)
  360. {
  361. this.nu02 = nu02;
  362. }
  363. public void set_nu30(double nu30)
  364. {
  365. this.nu30 = nu30;
  366. }
  367. public void set_nu21(double nu21)
  368. {
  369. this.nu21 = nu21;
  370. }
  371. public void set_nu12(double nu12)
  372. {
  373. this.nu12 = nu12;
  374. }
  375. public void set_nu03(double nu03)
  376. {
  377. this.nu03 = nu03;
  378. }
  379. }
  380. }