Converters.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. using System;
  2. using System.Collections.Generic;
  3. using OpenCVForUnity.CoreModule;
  4. #if !UNITY_WSA_10_0
  5. using OpenCVForUnity.DnnModule;
  6. #endif
  7. namespace OpenCVForUnity.UtilsModule
  8. {
  9. public class Converters
  10. {
  11. public static Mat vector_Point_to_Mat (List<Point> pts)
  12. {
  13. return vector_Point_to_Mat (pts, CvType.CV_32S);
  14. }
  15. public static Mat vector_Point2f_to_Mat (List<Point> pts)
  16. {
  17. return vector_Point_to_Mat (pts, CvType.CV_32F);
  18. }
  19. public static Mat vector_Point2d_to_Mat (List<Point> pts)
  20. {
  21. return vector_Point_to_Mat (pts, CvType.CV_64F);
  22. }
  23. public static Mat vector_Point_to_Mat (List<Point> pts, int typeDepth)
  24. {
  25. Mat res;
  26. int count = (pts != null) ? pts.Count : 0;
  27. if (count > 0) {
  28. switch (typeDepth) {
  29. case CvType.CV_32S:
  30. {
  31. res = new Mat (count, 1, CvType.CV_32SC2);
  32. int[] buff = new int[count * 2];
  33. for (int i = 0; i < count; i++) {
  34. Point p = pts [i];
  35. buff [i * 2] = (int)p.x;
  36. buff [i * 2 + 1] = (int)p.y;
  37. }
  38. res.put (0, 0, buff);
  39. }
  40. break;
  41. case CvType.CV_32F:
  42. {
  43. res = new Mat (count, 1, CvType.CV_32FC2);
  44. float[] buff = new float[count * 2];
  45. for (int i = 0; i < count; i++) {
  46. Point p = pts [i];
  47. buff [i * 2] = (float)p.x;
  48. buff [i * 2 + 1] = (float)p.y;
  49. }
  50. res.put (0, 0, buff);
  51. }
  52. break;
  53. case CvType.CV_64F:
  54. {
  55. res = new Mat (count, 1, CvType.CV_64FC2);
  56. double[] buff = new double[count * 2];
  57. for (int i = 0; i < count; i++) {
  58. Point p = pts [i];
  59. buff [i * 2] = p.x;
  60. buff [i * 2 + 1] = p.y;
  61. }
  62. res.put (0, 0, buff);
  63. }
  64. break;
  65. default:
  66. throw new CvException ("'typeDepth' can be CV_32S, CV_32F or CV_64F");
  67. }
  68. } else {
  69. res = new Mat ();
  70. }
  71. return res;
  72. }
  73. public static Mat vector_Point3i_to_Mat (List<Point3> pts)
  74. {
  75. return vector_Point3_to_Mat (pts, CvType.CV_32S);
  76. }
  77. public static Mat vector_Point3f_to_Mat (List<Point3> pts)
  78. {
  79. return vector_Point3_to_Mat (pts, CvType.CV_32F);
  80. }
  81. public static Mat vector_Point3d_to_Mat (List<Point3> pts)
  82. {
  83. return vector_Point3_to_Mat (pts, CvType.CV_64F);
  84. }
  85. public static Mat vector_Point3_to_Mat (List<Point3> pts, int typeDepth)
  86. {
  87. Mat res;
  88. int count = (pts != null) ? pts.Count : 0;
  89. if (count > 0) {
  90. switch (typeDepth) {
  91. case CvType.CV_32S:
  92. {
  93. res = new Mat (count, 1, CvType.CV_32SC3);
  94. int[] buff = new int[count * 3];
  95. for (int i = 0; i < count; i++) {
  96. Point3 p = pts [i];
  97. buff [i * 3] = (int)p.x;
  98. buff [i * 3 + 1] = (int)p.y;
  99. buff [i * 3 + 2] = (int)p.z;
  100. }
  101. res.put (0, 0, buff);
  102. }
  103. break;
  104. case CvType.CV_32F:
  105. {
  106. res = new Mat (count, 1, CvType.CV_32FC3);
  107. float[] buff = new float[count * 3];
  108. for (int i = 0; i < count; i++) {
  109. Point3 p = pts [i];
  110. buff [i * 3] = (float)p.x;
  111. buff [i * 3 + 1] = (float)p.y;
  112. buff [i * 3 + 2] = (float)p.z;
  113. }
  114. res.put (0, 0, buff);
  115. }
  116. break;
  117. case CvType.CV_64F:
  118. {
  119. res = new Mat (count, 1, CvType.CV_64FC3);
  120. double[] buff = new double[count * 3];
  121. for (int i = 0; i < count; i++) {
  122. Point3 p = pts [i];
  123. buff [i * 3] = p.x;
  124. buff [i * 3 + 1] = p.y;
  125. buff [i * 3 + 2] = p.z;
  126. }
  127. res.put (0, 0, buff);
  128. }
  129. break;
  130. default:
  131. throw new CvException ("'typeDepth' can be CV_32S, CV_32F or CV_64F");
  132. }
  133. } else {
  134. res = new Mat ();
  135. }
  136. return res;
  137. }
  138. public static void Mat_to_vector_Point2f (Mat m, List<Point> pts)
  139. {
  140. if (m != null)
  141. m.ThrowIfDisposed ();
  142. Mat_to_vector_Point (m, pts);
  143. }
  144. public static void Mat_to_vector_Point2d (Mat m, List<Point> pts)
  145. {
  146. if (m != null)
  147. m.ThrowIfDisposed ();
  148. Mat_to_vector_Point (m, pts);
  149. }
  150. public static void Mat_to_vector_Point (Mat m, List<Point> pts)
  151. {
  152. if (m != null)
  153. m.ThrowIfDisposed ();
  154. if (pts == null)
  155. throw new CvException ("Output List can't be null");
  156. int count = m.rows ();
  157. int type = m.type ();
  158. if (m.cols () != 1)
  159. throw new CvException ("Input Mat should have one column\n" + m);
  160. pts.Clear ();
  161. if (type == CvType.CV_32SC2) {
  162. int[] buff = new int[2 * count];
  163. m.get (0, 0, buff);
  164. for (int i = 0; i < count; i++) {
  165. pts.Add (new Point (buff [i * 2], buff [i * 2 + 1]));
  166. }
  167. } else if (type == CvType.CV_32FC2) {
  168. float[] buff = new float[2 * count];
  169. m.get (0, 0, buff);
  170. for (int i = 0; i < count; i++) {
  171. pts.Add (new Point (buff [i * 2], buff [i * 2 + 1]));
  172. }
  173. } else if (type == CvType.CV_64FC2) {
  174. double[] buff = new double[2 * count];
  175. m.get (0, 0, buff);
  176. for (int i = 0; i < count; i++) {
  177. pts.Add (new Point (buff [i * 2], buff [i * 2 + 1]));
  178. }
  179. } else {
  180. throw new CvException (
  181. "Input Mat should be of CV_32SC2, CV_32FC2 or CV_64FC2 type\n" + m);
  182. }
  183. }
  184. public static void Mat_to_vector_Point3i (Mat m, List<Point3> pts)
  185. {
  186. if (m != null)
  187. m.ThrowIfDisposed ();
  188. Mat_to_vector_Point3 (m, pts);
  189. }
  190. public static void Mat_to_vector_Point3f (Mat m, List<Point3> pts)
  191. {
  192. if (m != null)
  193. m.ThrowIfDisposed ();
  194. Mat_to_vector_Point3 (m, pts);
  195. }
  196. public static void Mat_to_vector_Point3d (Mat m, List<Point3> pts)
  197. {
  198. if (m != null)
  199. m.ThrowIfDisposed ();
  200. Mat_to_vector_Point3 (m, pts);
  201. }
  202. public static void Mat_to_vector_Point3 (Mat m, List<Point3> pts)
  203. {
  204. if (m != null)
  205. m.ThrowIfDisposed ();
  206. if (pts == null)
  207. throw new CvException ("Output List can't be null");
  208. int count = m.rows ();
  209. int type = m.type ();
  210. if (m.cols () != 1)
  211. throw new CvException ("Input Mat should have one column\n" + m);
  212. pts.Clear ();
  213. if (type == CvType.CV_32SC3) {
  214. int[] buff = new int[3 * count];
  215. m.get (0, 0, buff);
  216. for (int i = 0; i < count; i++) {
  217. pts.Add (new Point3 (buff [i * 3], buff [i * 3 + 1], buff [i * 3 + 2]));
  218. }
  219. } else if (type == CvType.CV_32FC3) {
  220. float[] buff = new float[3 * count];
  221. m.get (0, 0, buff);
  222. for (int i = 0; i < count; i++) {
  223. pts.Add (new Point3 (buff [i * 3], buff [i * 3 + 1], buff [i * 3 + 2]));
  224. }
  225. } else if (type == CvType.CV_64FC3) {
  226. double[] buff = new double[3 * count];
  227. m.get (0, 0, buff);
  228. for (int i = 0; i < count; i++) {
  229. pts.Add (new Point3 (buff [i * 3], buff [i * 3 + 1], buff [i * 3 + 2]));
  230. }
  231. } else {
  232. throw new CvException (
  233. "Input Mat should be of CV_32SC3, CV_32FC3 or CV_64FC3 type\n" + m);
  234. }
  235. }
  236. public static Mat vector_Mat_to_Mat (List<Mat> mats)
  237. {
  238. Mat res;
  239. int count = (mats != null) ? mats.Count : 0;
  240. if (count > 0) {
  241. res = new Mat (count, 1, CvType.CV_32SC2);
  242. int[] buff = new int[count * 2];
  243. for (int i = 0; i < count; i++) {
  244. long addr = mats [i].nativeObj.ToInt64 ();//TODO:@check
  245. buff [i * 2] = (int)(addr >> 32);
  246. buff [i * 2 + 1] = (int)(addr & 0xffffffff);
  247. }
  248. res.put (0, 0, buff);
  249. } else {
  250. res = new Mat ();
  251. }
  252. return res;
  253. }
  254. public static void Mat_to_vector_Mat (Mat m, List<Mat> mats)
  255. {
  256. if (m != null)
  257. m.ThrowIfDisposed ();
  258. if (mats == null)
  259. throw new CvException ("mats == null");
  260. int count = m.rows ();
  261. if (CvType.CV_32SC2 != m.type () || m.cols () != 1)
  262. throw new CvException (
  263. "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m);
  264. mats.Clear ();
  265. int[] buff = new int[count * 2];
  266. m.get (0, 0, buff);
  267. for (int i = 0; i < count; i++) {
  268. long addr = (((long)buff [i * 2]) << 32) | (((long)buff [i * 2 + 1]) & 0xffffffffL);
  269. // mats.add(new Mat(addr));
  270. mats.Add (new Mat (new IntPtr (addr)));//TODO:@check
  271. }
  272. }
  273. public static Mat vector_float_to_Mat (List<float> fs)
  274. {
  275. Mat res;
  276. int count = (fs != null) ? fs.Count : 0;
  277. if (count > 0) {
  278. res = new Mat (count, 1, CvType.CV_32FC1);
  279. float[] buff = new float[count];
  280. for (int i = 0; i < count; i++) {
  281. float f = fs [i];
  282. buff [i] = f;
  283. }
  284. res.put (0, 0, buff);
  285. } else {
  286. res = new Mat ();
  287. }
  288. return res;
  289. }
  290. public static void Mat_to_vector_float (Mat m, List<float> fs)
  291. {
  292. if (m != null)
  293. m.ThrowIfDisposed ();
  294. if (fs == null)
  295. throw new CvException ("fs == null");
  296. int count = m.rows ();
  297. if (CvType.CV_32FC1 != m.type () || m.cols () != 1)
  298. throw new CvException (
  299. "CvType.CV_32FC1 != m.type() || m.cols()!=1\n" + m);
  300. fs.Clear ();
  301. float[] buff = new float[count];
  302. m.get (0, 0, buff);
  303. for (int i = 0; i < count; i++) {
  304. fs.Add (buff [i]);
  305. }
  306. }
  307. public static Mat vector_uchar_to_Mat (List<byte> bs)
  308. {
  309. Mat res;
  310. int count = (bs != null) ? bs.Count : 0;
  311. if (count > 0) {
  312. res = new Mat (count, 1, CvType.CV_8UC1);
  313. byte[] buff = new byte[count];
  314. for (int i = 0; i < count; i++) {
  315. byte b = bs [i];
  316. buff [i] = b;
  317. }
  318. res.put (0, 0, buff);
  319. } else {
  320. res = new Mat ();
  321. }
  322. return res;
  323. }
  324. public static void Mat_to_vector_uchar (Mat m, List<byte> us)
  325. {
  326. if (m != null)
  327. m.ThrowIfDisposed ();
  328. if (us == null)
  329. throw new CvException ("Output List can't be null");
  330. int count = m.rows ();
  331. if (CvType.CV_8UC1 != m.type () || m.cols () != 1)
  332. throw new CvException (
  333. "CvType.CV_8UC1 != m.type() || m.cols()!=1\n" + m);
  334. us.Clear ();
  335. byte[] buff = new byte[count];
  336. m.get (0, 0, buff);
  337. for (int i = 0; i < count; i++) {
  338. us.Add (buff [i]);
  339. }
  340. }
  341. public static Mat vector_char_to_Mat (List<byte> bs)
  342. {
  343. Mat res;
  344. int count = (bs != null) ? bs.Count : 0;
  345. if (count > 0) {
  346. res = new Mat (count, 1, CvType.CV_8SC1);
  347. byte[] buff = new byte[count];
  348. for (int i = 0; i < count; i++) {
  349. byte b = bs [i];
  350. buff [i] = b;
  351. }
  352. res.put (0, 0, buff);
  353. } else {
  354. res = new Mat ();
  355. }
  356. return res;
  357. }
  358. public static Mat vector_int_to_Mat (List<int> _is)
  359. {
  360. Mat res;
  361. int count = (_is != null) ? _is.Count : 0;
  362. if (count > 0) {
  363. res = new Mat (count, 1, CvType.CV_32SC1);
  364. int[] buff = new int[count];
  365. for (int i = 0; i < count; i++) {
  366. int v = _is [i];
  367. buff [i] = v;
  368. }
  369. res.put (0, 0, buff);
  370. } else {
  371. res = new Mat ();
  372. }
  373. return res;
  374. }
  375. public static void Mat_to_vector_int (Mat m, List<int> _is)
  376. {
  377. if (m != null)
  378. m.ThrowIfDisposed ();
  379. if (_is == null)
  380. throw new CvException ("is == null");
  381. int count = m.rows ();
  382. if (CvType.CV_32SC1 != m.type () || m.cols () != 1)
  383. throw new CvException (
  384. "CvType.CV_32SC1 != m.type() || m.cols()!=1\n" + m);
  385. _is.Clear ();
  386. int[] buff = new int[count];
  387. m.get (0, 0, buff);
  388. for (int i = 0; i < count; i++) {
  389. _is.Add (buff [i]);
  390. }
  391. }
  392. public static void Mat_to_vector_char (Mat m, List<byte> bs)
  393. {
  394. if (m != null)
  395. m.ThrowIfDisposed ();
  396. if (bs == null)
  397. throw new CvException ("Output List can't be null");
  398. int count = m.rows ();
  399. if (CvType.CV_8SC1 != m.type () || m.cols () != 1)
  400. throw new CvException (
  401. "CvType.CV_8SC1 != m.type() || m.cols()!=1\n" + m);
  402. bs.Clear ();
  403. byte[] buff = new byte[count];
  404. m.get (0, 0, buff);
  405. for (int i = 0; i < count; i++) {
  406. bs.Add (buff [i]);
  407. }
  408. }
  409. public static Mat vector_Rect_to_Mat (List<Rect> rs)
  410. {
  411. Mat res;
  412. int count = (rs != null) ? rs.Count : 0;
  413. if (count > 0) {
  414. res = new Mat (count, 1, CvType.CV_32SC4);
  415. int[] buff = new int[4 * count];
  416. for (int i = 0; i < count; i++) {
  417. Rect r = rs [i];
  418. buff [4 * i] = r.x;
  419. buff [4 * i + 1] = r.y;
  420. buff [4 * i + 2] = r.width;
  421. buff [4 * i + 3] = r.height;
  422. }
  423. res.put (0, 0, buff);
  424. } else {
  425. res = new Mat ();
  426. }
  427. return res;
  428. }
  429. public static void Mat_to_vector_Rect (Mat m, List<Rect> rs)
  430. {
  431. if (m != null)
  432. m.ThrowIfDisposed ();
  433. if (rs == null)
  434. throw new CvException ("rs == null");
  435. int count = m.rows ();
  436. if (CvType.CV_32SC4 != m.type () || m.cols () != 1)
  437. throw new CvException (
  438. "CvType.CV_32SC4 != m.type() || m.rows()!=1\n" + m);
  439. rs.Clear ();
  440. int[] buff = new int[4 * count];
  441. m.get (0, 0, buff);
  442. for (int i = 0; i < count; i++) {
  443. rs.Add (new Rect (buff [4 * i], buff [4 * i + 1], buff [4 * i + 2], buff [4 * i + 3]));
  444. }
  445. }
  446. public static Mat vector_KeyPoint_to_Mat (List<KeyPoint> kps)
  447. {
  448. Mat res;
  449. int count = (kps != null) ? kps.Count : 0;
  450. if (count > 0) {
  451. res = new Mat (count, 1, CvType.CV_64FC (7));
  452. double[] buff = new double[count * 7];
  453. for (int i = 0; i < count; i++) {
  454. KeyPoint kp = kps [i];
  455. buff [7 * i] = kp.pt.x;
  456. buff [7 * i + 1] = kp.pt.y;
  457. buff [7 * i + 2] = kp.size;
  458. buff [7 * i + 3] = kp.angle;
  459. buff [7 * i + 4] = kp.response;
  460. buff [7 * i + 5] = kp.octave;
  461. buff [7 * i + 6] = kp.class_id;
  462. }
  463. res.put (0, 0, buff);
  464. } else {
  465. res = new Mat ();
  466. }
  467. return res;
  468. }
  469. public static void Mat_to_vector_KeyPoint (Mat m, List<KeyPoint> kps)
  470. {
  471. if (m != null)
  472. m.ThrowIfDisposed ();
  473. if (kps == null)
  474. throw new CvException ("Output List can't be null");
  475. int count = m.rows ();
  476. if (CvType.CV_64FC (7) != m.type () || m.cols () != 1)
  477. throw new CvException (
  478. "CvType.CV_64FC(7) != m.type() || m.cols()!=1\n" + m);
  479. kps.Clear ();
  480. double[] buff = new double[7 * count];
  481. m.get (0, 0, buff);
  482. for (int i = 0; i < count; i++) {
  483. kps.Add (new KeyPoint ((float)buff [7 * i], (float)buff [7 * i + 1], (float)buff [7 * i + 2], (float)buff [7 * i + 3],
  484. (float)buff [7 * i + 4], (int)buff [7 * i + 5], (int)buff [7 * i + 6]));
  485. }
  486. }
  487. // vector_vector_Point
  488. public static Mat vector_vector_Point_to_Mat (List<MatOfPoint> pts, List<Mat> mats)
  489. {
  490. Mat res;
  491. int lCount = (pts != null) ? pts.Count : 0;
  492. if (lCount > 0) {
  493. foreach (MatOfPoint vpt in pts)
  494. mats.Add (vpt);
  495. res = vector_Mat_to_Mat (mats);
  496. } else {
  497. res = new Mat ();
  498. }
  499. return res;
  500. }
  501. public static void Mat_to_vector_vector_Point (Mat m, List<MatOfPoint> pts)
  502. {
  503. if (m != null)
  504. m.ThrowIfDisposed ();
  505. if (pts == null)
  506. throw new CvException ("Output List can't be null");
  507. if (m == null)
  508. throw new CvException ("Input Mat can't be null");
  509. List<Mat> mats = new List<Mat> (m.rows ());
  510. Mat_to_vector_Mat (m, mats);
  511. foreach (Mat mi in mats) {
  512. MatOfPoint pt = new MatOfPoint (mi);
  513. pts.Add (pt);
  514. mi.release ();
  515. }
  516. mats.Clear ();
  517. }
  518. // vector_vector_Point2f
  519. public static void Mat_to_vector_vector_Point2f (Mat m, List<MatOfPoint2f> pts)
  520. {
  521. if (m != null)
  522. m.ThrowIfDisposed ();
  523. if (pts == null)
  524. throw new CvException ("Output List can't be null");
  525. if (m == null)
  526. throw new CvException ("Input Mat can't be null");
  527. List<Mat> mats = new List<Mat> (m.rows ());
  528. Mat_to_vector_Mat (m, mats);
  529. foreach (Mat mi in mats) {
  530. MatOfPoint2f pt = new MatOfPoint2f (mi);
  531. pts.Add (pt);
  532. mi.release ();
  533. }
  534. mats.Clear ();
  535. }
  536. // vector_vector_Point2f
  537. public static Mat vector_vector_Point2f_to_Mat (List<MatOfPoint2f> pts, List<Mat> mats)
  538. {
  539. Mat res;
  540. int lCount = (pts != null) ? pts.Count : 0;
  541. if (lCount > 0) {
  542. foreach (MatOfPoint2f vpt in pts)
  543. mats.Add (vpt);
  544. res = vector_Mat_to_Mat (mats);
  545. } else {
  546. res = new Mat ();
  547. }
  548. return res;
  549. }
  550. // vector_vector_Point3f
  551. public static void Mat_to_vector_vector_Point3f (Mat m, List<MatOfPoint3f> pts)
  552. {
  553. if (m != null)
  554. m.ThrowIfDisposed ();
  555. if (pts == null)
  556. throw new CvException ("Output List can't be null");
  557. if (m == null)
  558. throw new CvException ("Input Mat can't be null");
  559. List<Mat> mats = new List<Mat> (m.rows ());
  560. Mat_to_vector_Mat (m, mats);
  561. foreach (Mat mi in mats) {
  562. MatOfPoint3f pt = new MatOfPoint3f (mi);
  563. pts.Add (pt);
  564. mi.release ();
  565. }
  566. mats.Clear ();
  567. }
  568. // vector_vector_Point3f
  569. public static Mat vector_vector_Point3f_to_Mat (List<MatOfPoint3f> pts, List<Mat> mats)
  570. {
  571. Mat res;
  572. int lCount = (pts != null) ? pts.Count : 0;
  573. if (lCount > 0) {
  574. foreach (MatOfPoint3f vpt in pts)
  575. mats.Add (vpt);
  576. res = vector_Mat_to_Mat (mats);
  577. } else {
  578. res = new Mat ();
  579. }
  580. return res;
  581. }
  582. // vector_vector_KeyPoint
  583. public static Mat vector_vector_KeyPoint_to_Mat (List<MatOfKeyPoint> kps, List<Mat> mats)
  584. {
  585. Mat res;
  586. int lCount = (kps != null) ? kps.Count : 0;
  587. if (lCount > 0) {
  588. foreach (MatOfKeyPoint vkp in kps)
  589. mats.Add (vkp);
  590. res = vector_Mat_to_Mat (mats);
  591. } else {
  592. res = new Mat ();
  593. }
  594. return res;
  595. }
  596. public static void Mat_to_vector_vector_KeyPoint (Mat m, List<MatOfKeyPoint> kps)
  597. {
  598. if (m != null)
  599. m.ThrowIfDisposed ();
  600. if (kps == null)
  601. throw new CvException ("Output List can't be null");
  602. if (m == null)
  603. throw new CvException ("Input Mat can't be null");
  604. List<Mat> mats = new List<Mat> (m.rows ());
  605. Mat_to_vector_Mat (m, mats);
  606. foreach (Mat mi in mats) {
  607. MatOfKeyPoint vkp = new MatOfKeyPoint (mi);
  608. kps.Add (vkp);
  609. mi.release ();
  610. }
  611. mats.Clear ();
  612. }
  613. public static Mat vector_double_to_Mat (List<double> ds)
  614. {
  615. Mat res;
  616. int count = (ds != null) ? ds.Count : 0;
  617. if (count > 0) {
  618. res = new Mat (count, 1, CvType.CV_64FC1);
  619. double[] buff = new double[count];
  620. for (int i = 0; i < count; i++) {
  621. double v = ds [i];
  622. buff [i] = v;
  623. }
  624. res.put (0, 0, buff);
  625. } else {
  626. res = new Mat ();
  627. }
  628. return res;
  629. }
  630. public static void Mat_to_vector_double (Mat m, List<double> ds)
  631. {
  632. if (m != null)
  633. m.ThrowIfDisposed ();
  634. if (ds == null)
  635. throw new CvException ("ds == null");
  636. int count = m.rows ();
  637. if (CvType.CV_64FC1 != m.type () || m.cols () != 1)
  638. throw new CvException (
  639. "CvType.CV_64FC1 != m.type() || m.cols()!=1\n" + m);
  640. ds.Clear ();
  641. double[] buff = new double[count];
  642. m.get (0, 0, buff);
  643. for (int i = 0; i < count; i++) {
  644. ds.Add (buff [i]);
  645. }
  646. }
  647. public static Mat vector_DMatch_to_Mat (List<DMatch> matches)
  648. {
  649. Mat res;
  650. int count = (matches != null) ? matches.Count : 0;
  651. if (count > 0) {
  652. res = new Mat (count, 1, CvType.CV_64FC4);
  653. double[] buff = new double[count * 4];
  654. for (int i = 0; i < count; i++) {
  655. DMatch m = matches [i];
  656. buff [4 * i] = m.queryIdx;
  657. buff [4 * i + 1] = m.trainIdx;
  658. buff [4 * i + 2] = m.imgIdx;
  659. buff [4 * i + 3] = m.distance;
  660. }
  661. res.put (0, 0, buff);
  662. } else {
  663. res = new Mat ();
  664. }
  665. return res;
  666. }
  667. public static void Mat_to_vector_DMatch (Mat m, List<DMatch> matches)
  668. {
  669. if (m != null)
  670. m.ThrowIfDisposed ();
  671. if (matches == null)
  672. throw new CvException ("Output List can't be null");
  673. int count = m.rows ();
  674. if (CvType.CV_64FC4 != m.type () || m.cols () != 1)
  675. throw new CvException (
  676. "CvType.CV_64FC4 != m.type() || m.cols()!=1\n" + m);
  677. matches.Clear ();
  678. double[] buff = new double[4 * count];
  679. m.get (0, 0, buff);
  680. for (int i = 0; i < count; i++) {
  681. matches.Add (new DMatch ((int)buff [4 * i], (int)buff [4 * i + 1], (int)buff [4 * i + 2], (float)buff [4 * i + 3]));
  682. }
  683. }
  684. // vector_vector_DMatch
  685. public static Mat vector_vector_DMatch_to_Mat (List<MatOfDMatch> lvdm, List<Mat> mats)
  686. {
  687. Mat res;
  688. int lCount = (lvdm != null) ? lvdm.Count : 0;
  689. if (lCount > 0) {
  690. foreach (MatOfDMatch vdm in lvdm)
  691. mats.Add (vdm);
  692. res = vector_Mat_to_Mat (mats);
  693. } else {
  694. res = new Mat ();
  695. }
  696. return res;
  697. }
  698. public static void Mat_to_vector_vector_DMatch (Mat m, List<MatOfDMatch> lvdm)
  699. {
  700. if (m != null)
  701. m.ThrowIfDisposed ();
  702. if (lvdm == null)
  703. throw new CvException ("Output List can't be null");
  704. if (m == null)
  705. throw new CvException ("Input Mat can't be null");
  706. List<Mat> mats = new List<Mat> (m.rows ());
  707. Mat_to_vector_Mat (m, mats);
  708. lvdm.Clear ();
  709. foreach (Mat mi in mats) {
  710. MatOfDMatch vdm = new MatOfDMatch (mi);
  711. lvdm.Add (vdm);
  712. mi.release ();
  713. }
  714. mats.Clear ();
  715. }
  716. // vector_vector_char
  717. public static Mat vector_vector_char_to_Mat (List<MatOfByte> lvb, List<Mat> mats)
  718. {
  719. Mat res;
  720. int lCount = (lvb != null) ? lvb.Count : 0;
  721. if (lCount > 0) {
  722. foreach (MatOfByte vb in lvb)
  723. mats.Add (vb);
  724. res = vector_Mat_to_Mat (mats);
  725. } else {
  726. res = new Mat ();
  727. }
  728. return res;
  729. }
  730. public static void Mat_to_vector_vector_char (Mat m, List<List<byte>> llb)
  731. {
  732. if (m != null)
  733. m.ThrowIfDisposed ();
  734. if (llb == null)
  735. throw new CvException ("Output List can't be null");
  736. if (m == null)
  737. throw new CvException ("Input Mat can't be null");
  738. List<Mat> mats = new List<Mat> (m.rows ());
  739. Mat_to_vector_Mat (m, mats);
  740. foreach (Mat mi in mats) {
  741. List<byte> lb = new List<byte> ();
  742. Mat_to_vector_char (mi, lb);
  743. llb.Add (lb);
  744. mi.release ();
  745. }
  746. mats.Clear ();
  747. }
  748. public static Mat vector_RotatedRect_to_Mat (List<RotatedRect> rs)
  749. {
  750. Mat res;
  751. int count = (rs != null) ? rs.Count : 0;
  752. if (count > 0) {
  753. res = new Mat (count, 1, CvType.CV_32FC (5));
  754. float[] buff = new float[5 * count];
  755. for (int i = 0; i < count; i++) {
  756. RotatedRect r = rs [i];
  757. buff [5 * i] = (float)r.center.x;
  758. buff [5 * i + 1] = (float)r.center.y;
  759. buff [5 * i + 2] = (float)r.size.width;
  760. buff [5 * i + 3] = (float)r.size.height;
  761. buff [5 * i + 4] = (float)r.angle;
  762. }
  763. res.put (0, 0, buff);
  764. } else {
  765. res = new Mat ();
  766. }
  767. return res;
  768. }
  769. public static void Mat_to_vector_RotatedRect (Mat m, List<RotatedRect> rs)
  770. {
  771. if (rs == null)
  772. throw new CvException ("rs == null");
  773. int count = m.rows ();
  774. if (CvType.CV_32FC (5) != m.type () || m.cols () != 1)
  775. throw new CvException (
  776. "CvType.CV_32FC5 != m.type() || m.rows()!=1\n" + m);
  777. rs.Clear ();
  778. float[] buff = new float[5 * count];
  779. m.get (0, 0, buff);
  780. for (int i = 0; i < count; i++) {
  781. rs.Add (new RotatedRect (new Point (buff [5 * i], buff [5 * i + 1]), new Size (buff [5 * i + 2], buff [5 * i + 3]), buff [5 * i + 4]));
  782. }
  783. }
  784. public static Mat vector_String_to_Mat (List<string> strings)
  785. {
  786. Mat res;
  787. int count = (strings != null) ? strings.Count : 0;
  788. if (count > 0) {
  789. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  790. for (int i = 0; i < count; i++) {
  791. if (i > 0)
  792. sb.Append (",");
  793. sb.Append (strings [i]);
  794. }
  795. // byte[] buff = System.Text.Encoding.ASCII.GetBytes (sb.ToString ());
  796. byte[] buff = System.Text.Encoding.UTF8.GetBytes (sb.ToString ());
  797. res = new Mat (1, buff.Length, CvType.CV_8SC1);
  798. res.put (0, 0, buff);
  799. } else {
  800. res = new Mat ();
  801. }
  802. return res;
  803. }
  804. public static void Mat_to_vector_String (Mat m, List<string> strings)
  805. {
  806. if (m != null)
  807. m.ThrowIfDisposed ();
  808. if (strings == null)
  809. throw new CvException ("strings == null");
  810. int count = m.cols ();
  811. if (CvType.CV_8SC1 != m.type () || m.rows () != 1)
  812. throw new CvException (
  813. "CvType.CV_8SC1 != m.type() || m.rows()!=1\n" + m);
  814. strings.Clear ();
  815. if (count > 0) {
  816. byte[] buff = new byte[count];
  817. m.get (0, 0, buff);
  818. // string str = System.Text.Encoding.ASCII.GetString (buff);
  819. // string str = System.Text.Encoding.UTF8.GetString (buff);
  820. // strings.AddRange (System.Text.Encoding.ASCII.GetString (buff).Split (','));
  821. strings.AddRange (System.Text.Encoding.UTF8.GetString (buff).Split (','));
  822. }
  823. }
  824. #if !UNITY_WSA_10_0
  825. public static Mat vector_MatShape_to_Mat (List<MatOfInt> matshapes)
  826. {
  827. Mat res;
  828. int count = (matshapes != null) ? matshapes.Count : 0;
  829. if (count > 0) {
  830. res = new Mat (count, 1, CvType.CV_32SC2);
  831. int[] buff = new int[count * 2];
  832. for (int i = 0; i < count; i++) {
  833. long addr = matshapes [i].nativeObj.ToInt64 ();//TODO:@check
  834. buff [i * 2] = (int)(addr >> 32);
  835. buff [i * 2 + 1] = (int)(addr & 0xffffffff);
  836. }
  837. res.put (0, 0, buff);
  838. } else {
  839. res = new Mat ();
  840. }
  841. return res;
  842. }
  843. public static void Mat_to_vector_Ptr_Layer (Mat m, List<Layer> ptr_layers)
  844. {
  845. if (m != null)
  846. m.ThrowIfDisposed ();
  847. if (ptr_layers == null)
  848. throw new CvException ("ptr_layers == null");
  849. int count = m.rows ();
  850. if (CvType.CV_32SC2 != m.type () || m.cols () != 1)
  851. throw new CvException (
  852. "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m);
  853. ptr_layers.Clear ();
  854. int[] buff = new int[count * 2];
  855. m.get (0, 0, buff);
  856. for (int i = 0; i < count; i++) {
  857. long addr = (((long)buff [i * 2]) << 32) | (((long)buff [i * 2 + 1]) & 0xffffffffL);
  858. ptr_layers.Add (new Layer (new IntPtr (addr)));//TODO:@check
  859. }
  860. }
  861. #endif
  862. }
  863. }