123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921 |
- using System;
- using System.Collections.Generic;
- using OpenCVForUnity.CoreModule;
- #if !UNITY_WSA_10_0
- using OpenCVForUnity.DnnModule;
- #endif
- namespace OpenCVForUnity.UtilsModule {
- public class Converters {
- public static Mat vector_Point_to_Mat(List<Point> pts) {
- return vector_Point_to_Mat(pts, CvType.CV_32S);
- }
- public static Mat vector_Point2f_to_Mat(List<Point> pts) {
- return vector_Point_to_Mat(pts, CvType.CV_32F);
- }
- public static Mat vector_Point2d_to_Mat(List<Point> pts) {
- return vector_Point_to_Mat(pts, CvType.CV_64F);
- }
- public static Mat vector_Point_to_Mat(List<Point> pts, int typeDepth) {
- Mat res;
- int count = (pts != null) ? pts.Count : 0;
- if (count > 0) {
- switch (typeDepth) {
- case CvType.CV_32S: {
- res = new Mat(count, 1, CvType.CV_32SC2);
- int[] buff = new int[count * 2];
- for (int i = 0; i < count; i++) {
- Point p = pts[i];
- buff[i * 2] = (int)p.x;
- buff[i * 2 + 1] = (int)p.y;
- }
- res.put(0, 0, buff);
- }
- break;
- case CvType.CV_32F: {
- res = new Mat(count, 1, CvType.CV_32FC2);
- float[] buff = new float[count * 2];
- for (int i = 0; i < count; i++) {
- Point p = pts[i];
- buff[i * 2] = (float)p.x;
- buff[i * 2 + 1] = (float)p.y;
- }
- res.put(0, 0, buff);
- }
- break;
- case CvType.CV_64F: {
- res = new Mat(count, 1, CvType.CV_64FC2);
- double[] buff = new double[count * 2];
- for (int i = 0; i < count; i++) {
- Point p = pts[i];
- buff[i * 2] = p.x;
- buff[i * 2 + 1] = p.y;
- }
- res.put(0, 0, buff);
- }
- break;
- default:
- throw new CvException("'typeDepth' can be CV_32S, CV_32F or CV_64F");
- }
- } else {
- res = new Mat();
- }
- return res;
- }
- public static Mat vector_Point3i_to_Mat(List<Point3> pts) {
- return vector_Point3_to_Mat(pts, CvType.CV_32S);
- }
- public static Mat vector_Point3f_to_Mat(List<Point3> pts) {
- return vector_Point3_to_Mat(pts, CvType.CV_32F);
- }
- public static Mat vector_Point3d_to_Mat(List<Point3> pts) {
- return vector_Point3_to_Mat(pts, CvType.CV_64F);
- }
- public static Mat vector_Point3_to_Mat(List<Point3> pts, int typeDepth) {
- Mat res;
- int count = (pts != null) ? pts.Count : 0;
- if (count > 0) {
- switch (typeDepth) {
- case CvType.CV_32S: {
- res = new Mat(count, 1, CvType.CV_32SC3);
- int[] buff = new int[count * 3];
- for (int i = 0; i < count; i++) {
- Point3 p = pts[i];
- buff[i * 3] = (int)p.x;
- buff[i * 3 + 1] = (int)p.y;
- buff[i * 3 + 2] = (int)p.z;
- }
- res.put(0, 0, buff);
- }
- break;
- case CvType.CV_32F: {
- res = new Mat(count, 1, CvType.CV_32FC3);
- float[] buff = new float[count * 3];
- for (int i = 0; i < count; i++) {
- Point3 p = pts[i];
- buff[i * 3] = (float)p.x;
- buff[i * 3 + 1] = (float)p.y;
- buff[i * 3 + 2] = (float)p.z;
- }
- res.put(0, 0, buff);
- }
- break;
- case CvType.CV_64F: {
- res = new Mat(count, 1, CvType.CV_64FC3);
- double[] buff = new double[count * 3];
- for (int i = 0; i < count; i++) {
- Point3 p = pts[i];
- buff[i * 3] = p.x;
- buff[i * 3 + 1] = p.y;
- buff[i * 3 + 2] = p.z;
- }
- res.put(0, 0, buff);
- }
- break;
- default:
- throw new CvException("'typeDepth' can be CV_32S, CV_32F or CV_64F");
- }
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_Point2f(Mat m, List<Point> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- Mat_to_vector_Point(m, pts);
- }
- public static void Mat_to_vector_Point2d(Mat m, List<Point> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- Mat_to_vector_Point(m, pts);
- }
- public static void Mat_to_vector_Point(Mat m, List<Point> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- if (pts == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- int type = m.type();
- if (m.cols() != 1)
- throw new CvException("Input Mat should have one column\n" + m);
- pts.Clear();
- if (type == CvType.CV_32SC2) {
- int[] buff = new int[2 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point(buff[i * 2], buff[i * 2 + 1]));
- }
- } else if (type == CvType.CV_32FC2) {
- float[] buff = new float[2 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point(buff[i * 2], buff[i * 2 + 1]));
- }
- } else if (type == CvType.CV_64FC2) {
- double[] buff = new double[2 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point(buff[i * 2], buff[i * 2 + 1]));
- }
- } else {
- throw new CvException(
- "Input Mat should be of CV_32SC2, CV_32FC2 or CV_64FC2 type\n" + m);
- }
- }
- public static void Mat_to_vector_Point3i(Mat m, List<Point3> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- Mat_to_vector_Point3(m, pts);
- }
- public static void Mat_to_vector_Point3f(Mat m, List<Point3> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- Mat_to_vector_Point3(m, pts);
- }
- public static void Mat_to_vector_Point3d(Mat m, List<Point3> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- Mat_to_vector_Point3(m, pts);
- }
- public static void Mat_to_vector_Point3(Mat m, List<Point3> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- if (pts == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- int type = m.type();
- if (m.cols() != 1)
- throw new CvException("Input Mat should have one column\n" + m);
- pts.Clear();
- if (type == CvType.CV_32SC3) {
- int[] buff = new int[3 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2]));
- }
- } else if (type == CvType.CV_32FC3) {
- float[] buff = new float[3 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2]));
- }
- } else if (type == CvType.CV_64FC3) {
- double[] buff = new double[3 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- pts.Add(new Point3(buff[i * 3], buff[i * 3 + 1], buff[i * 3 + 2]));
- }
- } else {
- throw new CvException(
- "Input Mat should be of CV_32SC3, CV_32FC3 or CV_64FC3 type\n" + m);
- }
- }
- public static Mat vector_Mat_to_Mat(List<Mat> mats) {
- Mat res;
- int count = (mats != null) ? mats.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32SC2);
- int[] buff = new int[count * 2];
- for (int i = 0; i < count; i++) {
- long addr = mats[i].nativeObj.ToInt64();//TODO:@check
- buff[i * 2] = (int)(addr >> 32);
- buff[i * 2 + 1] = (int)(addr & 0xffffffff);
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {
- if (m != null)
- m.ThrowIfDisposed();
- if (mats == null)
- throw new CvException("mats == null");
- int count = m.rows();
- if (CvType.CV_32SC2 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m);
- mats.Clear();
- int[] buff = new int[count * 2];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- long addr = (((long)buff[i * 2]) << 32) | (((long)buff[i * 2 + 1]) & 0xffffffffL);
- // mats.add(new Mat(addr));
- mats.Add(new Mat(new IntPtr(addr)));//TODO:@check
- }
- }
- public static Mat vector_float_to_Mat(List<float> fs) {
- Mat res;
- int count = (fs != null) ? fs.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32FC1);
- float[] buff = new float[count];
- for (int i = 0; i < count; i++) {
- float f = fs[i];
- buff[i] = f;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_float(Mat m, List<float> fs) {
- if (m != null)
- m.ThrowIfDisposed();
- if (fs == null)
- throw new CvException("fs == null");
- int count = m.rows();
- if (CvType.CV_32FC1 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32FC1 != m.type() || m.cols()!=1\n" + m);
- fs.Clear();
- float[] buff = new float[count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- fs.Add(buff[i]);
- }
- }
- public static Mat vector_uchar_to_Mat(List<byte> bs) {
- Mat res;
- int count = (bs != null) ? bs.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_8UC1);
- byte[] buff = new byte[count];
- for (int i = 0; i < count; i++) {
- byte b = bs[i];
- buff[i] = b;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_uchar(Mat m, List<byte> us) {
- if (m != null)
- m.ThrowIfDisposed();
- if (us == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- if (CvType.CV_8UC1 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_8UC1 != m.type() || m.cols()!=1\n" + m);
- us.Clear();
- byte[] buff = new byte[count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- us.Add(buff[i]);
- }
- }
- public static Mat vector_char_to_Mat(List<byte> bs) {
- Mat res;
- int count = (bs != null) ? bs.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_8SC1);
- byte[] buff = new byte[count];
- for (int i = 0; i < count; i++) {
- byte b = bs[i];
- buff[i] = b;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static Mat vector_int_to_Mat(List<int> _is) {
- Mat res;
- int count = (_is != null) ? _is.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32SC1);
- int[] buff = new int[count];
- for (int i = 0; i < count; i++) {
- int v = _is[i];
- buff[i] = v;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_int(Mat m, List<int> _is) {
- if (m != null)
- m.ThrowIfDisposed();
- if (_is == null)
- throw new CvException("is == null");
- int count = m.rows();
- if (CvType.CV_32SC1 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32SC1 != m.type() || m.cols()!=1\n" + m);
- _is.Clear();
- int[] buff = new int[count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- _is.Add(buff[i]);
- }
- }
- public static void Mat_to_vector_char(Mat m, List<byte> bs) {
- if (m != null)
- m.ThrowIfDisposed();
- if (bs == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- if (CvType.CV_8SC1 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_8SC1 != m.type() || m.cols()!=1\n" + m);
- bs.Clear();
- byte[] buff = new byte[count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- bs.Add(buff[i]);
- }
- }
- public static Mat vector_Rect_to_Mat(List<Rect> rs) {
- Mat res;
- int count = (rs != null) ? rs.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32SC4);
- int[] buff = new int[4 * count];
- for (int i = 0; i < count; i++) {
- Rect r = rs[i];
- buff[4 * i] = r.x;
- buff[4 * i + 1] = r.y;
- buff[4 * i + 2] = r.width;
- buff[4 * i + 3] = r.height;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) {
- if (m != null)
- m.ThrowIfDisposed();
- if (rs == null)
- throw new CvException("rs == null");
- int count = m.rows();
- if (CvType.CV_32SC4 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32SC4 != m.type() || m.rows()!=1\n" + m);
- rs.Clear();
- int[] buff = new int[4 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- rs.Add(new Rect(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3]));
- }
- }
- public static Mat vector_KeyPoint_to_Mat(List<KeyPoint> kps) {
- Mat res;
- int count = (kps != null) ? kps.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_64FC(7));
- double[] buff = new double[count * 7];
- for (int i = 0; i < count; i++) {
- KeyPoint kp = kps[i];
- buff[7 * i] = kp.pt.x;
- buff[7 * i + 1] = kp.pt.y;
- buff[7 * i + 2] = kp.size;
- buff[7 * i + 3] = kp.angle;
- buff[7 * i + 4] = kp.response;
- buff[7 * i + 5] = kp.octave;
- buff[7 * i + 6] = kp.class_id;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) {
- if (m != null)
- m.ThrowIfDisposed();
- if (kps == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- if (CvType.CV_64FC(7) != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_64FC(7) != m.type() || m.cols()!=1\n" + m);
- kps.Clear();
- double[] buff = new double[7 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- kps.Add(new KeyPoint((float)buff[7 * i], (float)buff[7 * i + 1], (float)buff[7 * i + 2], (float)buff[7 * i + 3],
- (float)buff[7 * i + 4], (int)buff[7 * i + 5], (int)buff[7 * i + 6]));
- }
- }
- // vector_vector_Point
- public static Mat vector_vector_Point_to_Mat(List<MatOfPoint> pts, List<Mat> mats) {
- Mat res;
- int lCount = (pts != null) ? pts.Count : 0;
- if (lCount > 0) {
- foreach (MatOfPoint vpt in pts)
- mats.Add(vpt);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_vector_Point(Mat m, List<MatOfPoint> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- if (pts == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- foreach (Mat mi in mats) {
- MatOfPoint pt = new MatOfPoint(mi);
- pts.Add(pt);
- mi.release();
- }
- mats.Clear();
- }
- // vector_vector_Point2f
- public static void Mat_to_vector_vector_Point2f(Mat m, List<MatOfPoint2f> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- if (pts == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- foreach (Mat mi in mats) {
- MatOfPoint2f pt = new MatOfPoint2f(mi);
- pts.Add(pt);
- mi.release();
- }
- mats.Clear();
- }
- // vector_vector_Point2f
- public static Mat vector_vector_Point2f_to_Mat(List<MatOfPoint2f> pts, List<Mat> mats) {
- Mat res;
- int lCount = (pts != null) ? pts.Count : 0;
- if (lCount > 0) {
- foreach (MatOfPoint2f vpt in pts)
- mats.Add(vpt);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- // vector_vector_Point3f
- public static void Mat_to_vector_vector_Point3f(Mat m, List<MatOfPoint3f> pts) {
- if (m != null)
- m.ThrowIfDisposed();
- if (pts == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- foreach (Mat mi in mats) {
- MatOfPoint3f pt = new MatOfPoint3f(mi);
- pts.Add(pt);
- mi.release();
- }
- mats.Clear();
- }
- // vector_vector_Point3f
- public static Mat vector_vector_Point3f_to_Mat(List<MatOfPoint3f> pts, List<Mat> mats) {
- Mat res;
- int lCount = (pts != null) ? pts.Count : 0;
- if (lCount > 0) {
- foreach (MatOfPoint3f vpt in pts)
- mats.Add(vpt);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- // vector_vector_KeyPoint
- public static Mat vector_vector_KeyPoint_to_Mat(List<MatOfKeyPoint> kps, List<Mat> mats) {
- Mat res;
- int lCount = (kps != null) ? kps.Count : 0;
- if (lCount > 0) {
- foreach (MatOfKeyPoint vkp in kps)
- mats.Add(vkp);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_vector_KeyPoint(Mat m, List<MatOfKeyPoint> kps) {
- if (m != null)
- m.ThrowIfDisposed();
- if (kps == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- foreach (Mat mi in mats) {
- MatOfKeyPoint vkp = new MatOfKeyPoint(mi);
- kps.Add(vkp);
- mi.release();
- }
- mats.Clear();
- }
- public static Mat vector_double_to_Mat(List<double> ds) {
- Mat res;
- int count = (ds != null) ? ds.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_64FC1);
- double[] buff = new double[count];
- for (int i = 0; i < count; i++) {
- double v = ds[i];
- buff[i] = v;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_double(Mat m, List<double> ds) {
- if (m != null)
- m.ThrowIfDisposed();
- if (ds == null)
- throw new CvException("ds == null");
- int count = m.rows();
- if (CvType.CV_64FC1 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_64FC1 != m.type() || m.cols()!=1\n" + m);
- ds.Clear();
- double[] buff = new double[count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- ds.Add(buff[i]);
- }
- }
- public static Mat vector_DMatch_to_Mat(List<DMatch> matches) {
- Mat res;
- int count = (matches != null) ? matches.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_64FC4);
- double[] buff = new double[count * 4];
- for (int i = 0; i < count; i++) {
- DMatch m = matches[i];
- buff[4 * i] = m.queryIdx;
- buff[4 * i + 1] = m.trainIdx;
- buff[4 * i + 2] = m.imgIdx;
- buff[4 * i + 3] = m.distance;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) {
- if (m != null)
- m.ThrowIfDisposed();
- if (matches == null)
- throw new CvException("Output List can't be null");
- int count = m.rows();
- if (CvType.CV_64FC4 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_64FC4 != m.type() || m.cols()!=1\n" + m);
- matches.Clear();
- double[] buff = new double[4 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- matches.Add(new DMatch((int)buff[4 * i], (int)buff[4 * i + 1], (int)buff[4 * i + 2], (float)buff[4 * i + 3]));
- }
- }
- // vector_vector_DMatch
- public static Mat vector_vector_DMatch_to_Mat(List<MatOfDMatch> lvdm, List<Mat> mats) {
- Mat res;
- int lCount = (lvdm != null) ? lvdm.Count : 0;
- if (lCount > 0) {
- foreach (MatOfDMatch vdm in lvdm)
- mats.Add(vdm);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_vector_DMatch(Mat m, List<MatOfDMatch> lvdm) {
- if (m != null)
- m.ThrowIfDisposed();
- if (lvdm == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- lvdm.Clear();
- foreach (Mat mi in mats) {
- MatOfDMatch vdm = new MatOfDMatch(mi);
- lvdm.Add(vdm);
- mi.release();
- }
- mats.Clear();
- }
- // vector_vector_char
- public static Mat vector_vector_char_to_Mat(List<MatOfByte> lvb, List<Mat> mats) {
- Mat res;
- int lCount = (lvb != null) ? lvb.Count : 0;
- if (lCount > 0) {
- foreach (MatOfByte vb in lvb)
- mats.Add(vb);
- res = vector_Mat_to_Mat(mats);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_vector_char(Mat m, List<List<byte>> llb) {
- if (m != null)
- m.ThrowIfDisposed();
- if (llb == null)
- throw new CvException("Output List can't be null");
- if (m == null)
- throw new CvException("Input Mat can't be null");
- List<Mat> mats = new List<Mat>(m.rows());
- Mat_to_vector_Mat(m, mats);
- foreach (Mat mi in mats) {
- List<byte> lb = new List<byte>();
- Mat_to_vector_char(mi, lb);
- llb.Add(lb);
- mi.release();
- }
- mats.Clear();
- }
- public static Mat vector_RotatedRect_to_Mat(List<RotatedRect> rs) {
- Mat res;
- int count = (rs != null) ? rs.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32FC(5));
- float[] buff = new float[5 * count];
- for (int i = 0; i < count; i++) {
- RotatedRect r = rs[i];
- buff[5 * i] = (float)r.center.x;
- buff[5 * i + 1] = (float)r.center.y;
- buff[5 * i + 2] = (float)r.size.width;
- buff[5 * i + 3] = (float)r.size.height;
- buff[5 * i + 4] = (float)r.angle;
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_RotatedRect(Mat m, List<RotatedRect> rs) {
- if (rs == null)
- throw new CvException("rs == null");
- int count = m.rows();
- if (CvType.CV_32FC(5) != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32FC5 != m.type() || m.rows()!=1\n" + m);
- rs.Clear();
- float[] buff = new float[5 * count];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- 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]));
- }
- }
- public static Mat vector_String_to_Mat(List<string> strings) {
- Mat res;
- int count = (strings != null) ? strings.Count : 0;
- if (count > 0) {
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- for (int i = 0; i < count; i++) {
- if (i > 0)
- sb.Append(",");
- sb.Append(strings[i]);
- }
- // byte[] buff = System.Text.Encoding.ASCII.GetBytes (sb.ToString ());
- byte[] buff = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
- res = new Mat(1, buff.Length, CvType.CV_8SC1);
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_String(Mat m, List<string> strings) {
- if (m != null)
- m.ThrowIfDisposed();
- if (strings == null)
- throw new CvException("strings == null");
- int count = m.cols();
- if (CvType.CV_8SC1 != m.type() || m.rows() != 1)
- throw new CvException(
- "CvType.CV_8SC1 != m.type() || m.rows()!=1\n" + m);
- strings.Clear();
- if (count > 0) {
- byte[] buff = new byte[count];
- m.get(0, 0, buff);
- // string str = System.Text.Encoding.ASCII.GetString (buff);
- // string str = System.Text.Encoding.UTF8.GetString (buff);
- // strings.AddRange (System.Text.Encoding.ASCII.GetString (buff).Split (','));
- strings.AddRange(System.Text.Encoding.UTF8.GetString(buff).Split(','));
- }
- }
- #if !UNITY_WSA_10_0
- public static Mat vector_MatShape_to_Mat(List<MatOfInt> matshapes) {
- Mat res;
- int count = (matshapes != null) ? matshapes.Count : 0;
- if (count > 0) {
- res = new Mat(count, 1, CvType.CV_32SC2);
- int[] buff = new int[count * 2];
- for (int i = 0; i < count; i++) {
- long addr = matshapes[i].nativeObj.ToInt64();//TODO:@check
- buff[i * 2] = (int)(addr >> 32);
- buff[i * 2 + 1] = (int)(addr & 0xffffffff);
- }
- res.put(0, 0, buff);
- } else {
- res = new Mat();
- }
- return res;
- }
- public static void Mat_to_vector_Ptr_Layer(Mat m, List<Layer> ptr_layers) {
- if (m != null)
- m.ThrowIfDisposed();
- if (ptr_layers == null)
- throw new CvException("ptr_layers == null");
- int count = m.rows();
- if (CvType.CV_32SC2 != m.type() || m.cols() != 1)
- throw new CvException(
- "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m);
- ptr_layers.Clear();
- int[] buff = new int[count * 2];
- m.get(0, 0, buff);
- for (int i = 0; i < count; i++) {
- long addr = (((long)buff[i * 2]) << 32) | (((long)buff[i * 2 + 1]) & 0xffffffffL);
- ptr_layers.Add(new Layer(new IntPtr(addr)));//TODO:@check
- }
- }
- #endif
- }
- }
|