Converters.cs 32 KB

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