Added Java functions, converting vectors to Mats and Mats to vectors.
authorLeonid Beynenson <no@email>
Fri, 15 Jul 2011 16:25:47 +0000 (16:25 +0000)
committerLeonid Beynenson <no@email>
Fri, 15 Jul 2011 16:25:47 +0000 (16:25 +0000)
modules/java/src/java/utils.java

index 2c73d1f..58748d4 100644 (file)
@@ -9,12 +9,12 @@ public class utils {
                Mat res;\r
                int count = (pts!=null) ? pts.size() : 0;\r
                if(count>0){\r
-                       res = new Mat(1, count, CvType.CV_64FC2); //Point can be saved into double[2]\r
-                       double[] buff = new double[count*2];\r
+                       res = new Mat(1, count, CvType.CV_32SC2); //Point can be saved into double[2]\r
+                       int[] buff = new int[count*2];\r
                        for(int i=0; i<count; i++) {\r
                                Point p = pts.get(i);\r
-                               buff[i*2]   = p.x;\r
-                               buff[i*2+1] = p.y;\r
+                               buff[i*2]   = (int)p.x;\r
+                               buff[i*2+1] = (int)p.y;\r
                        }\r
                        res.put(0, 0, buff);\r
                } else {\r
@@ -25,15 +25,15 @@ public class utils {
 \r
        public static void Mat_to_vector_Point(Mat m, List<Point> pts) {\r
                if(pts == null)\r
-                       return;\r
+                       throw new java.lang.IllegalArgumentException();\r
                int cols = m.cols();\r
-               if(!CvType.CV_64FC2.equals(m.type()) ||  m.rows()!=1 || cols%2!=0)\r
-                       return;\r
+               if(!CvType.CV_32SC2.equals(m.type()) ||  m.rows()!=1 )\r
+                       throw new java.lang.IllegalArgumentException();\r
                \r
                pts.clear();\r
-               double[] buff = new double[cols];\r
+               int[] buff = new int[2*cols];\r
                m.get(0, 0, buff);\r
-               for(int i=0; i<cols/2; i++) {\r
+               for(int i=0; i<cols; i++) {\r
                        pts.add( new Point(buff[i*2], buff[i*2+1]) );\r
                }\r
        }\r
@@ -58,15 +58,15 @@ public class utils {
 \r
        public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {\r
                if(mats == null)\r
-                       return;\r
+                       throw new java.lang.IllegalArgumentException();\r
                int cols = m.cols();\r
-               if(!CvType.CV_32SC2.equals(m.type()) ||  m.rows()!=1 || cols%2!=0)\r
-                       return;\r
+               if(!CvType.CV_32SC2.equals(m.type()) ||  m.rows()!=1 )\r
+                       throw new java.lang.IllegalArgumentException();\r
                \r
                mats.clear();\r
-               int[] buff = new int[cols];\r
+               int[] buff = new int[cols*2];\r
                m.get(0, 0, buff);\r
-               for(int i=0; i<cols/2; i++) {\r
+               for(int i=0; i<cols; i++) {\r
                        long addr = (((long)buff[i*2])<<32) | ((long)buff[i*2+1]);\r
                        mats.add( new Mat(addr) );\r
                }\r
@@ -77,42 +77,136 @@ public class utils {
        }\r
 \r
        public static Mat vector_float_to_Mat(List<Float> fs) {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
+               Mat res;\r
+               int count = (fs!=null) ? fs.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_32FC1); //Point can be saved into double[2]\r
+                       float[] buff = new float[count];\r
+                       for(int i=0; i<count; i++) {\r
+                               float f = fs.get(i);\r
+                               buff[i]   = f;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
        }\r
 \r
        public static void Mat_to_vector_float(Mat m, List<Float> fs) {\r
-               // TODO Auto-generated method stub\r
+               if(fs == null)\r
+                       throw new java.lang.IllegalArgumentException();\r
+               int cols = m.cols();\r
+               if(!CvType.CV_32FC1.equals(m.type()) ||  m.rows()!=1 )\r
+                       throw new java.lang.IllegalArgumentException();\r
+               \r
+               fs.clear();\r
+               float[] buff = new float[cols];\r
+               m.get(0, 0, buff);\r
+               for(int i=0; i<cols; i++) {\r
+                       fs.add( new Float(buff[i]) );\r
+               }\r
        }\r
 \r
        public static Mat vector_uchar_to_Mat(List<Byte> bs) {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
+               Mat res;\r
+               int count = (bs!=null) ? bs.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_8UC1); //Point can be saved into double[2]\r
+                       byte[] buff = new byte[count];\r
+                       for(int i=0; i<count; i++) {\r
+                               byte b = bs.get(i);\r
+                               buff[i]   = b;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
        }\r
 \r
        public static Mat vector_int_to_Mat(List<Integer> is) {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
+               Mat res;\r
+               int count = (is!=null) ? is.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_32SC1); //Point can be saved into double[2]\r
+                       int[] buff = new int[count];\r
+                       for(int i=0; i<count; i++) {\r
+                               int v = is.get(i);\r
+                               buff[i]   = v;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
        }\r
 \r
        public static void Mat_to_vector_int(Mat m, List<Integer> is) {\r
-               // TODO Auto-generated method stub\r
+               if(is == null)\r
+                       throw new java.lang.IllegalArgumentException();\r
+               int cols = m.cols();\r
+               if(!CvType.CV_32SC1.equals(m.type()) ||  m.rows()!=1 )\r
+                       throw new java.lang.IllegalArgumentException();\r
                \r
+               is.clear();\r
+               int[] buff = new int[cols];\r
+               m.get(0, 0, buff);\r
+               for(int i=0; i<cols; i++) {\r
+                       is.add( new Integer(buff[i]) );\r
+               }\r
        }\r
 \r
        public static Mat vector_Rect_to_Mat(List<Rect> rs) {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
+               Mat res;\r
+               int count = (rs!=null) ? rs.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_32SC4); //Point can be saved into double[2]\r
+                       int[] buff = new int[4*count];\r
+                       for(int i=0; i<count; i++) {\r
+                               Rect r = rs.get(i);\r
+                               buff[4*i  ]   = r.x;\r
+                               buff[4*i+1]   = r.y;\r
+                               buff[4*i+2]   = r.width;\r
+                               buff[4*i+3]   = r.height;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
        }\r
 \r
        public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) {\r
-               // TODO Auto-generated method stub\r
+               if(rs == null)\r
+                       throw new java.lang.IllegalArgumentException();\r
+               int cols = m.cols();\r
+               if(!CvType.CV_32SC4.equals(m.type()) ||  m.rows()!=1 )\r
+                       throw new java.lang.IllegalArgumentException();\r
                \r
+               rs.clear();\r
+               int[] buff = new int[4*cols];\r
+               m.get(0, 0, buff);\r
+               for(int i=0; i<cols; i++) {\r
+                       rs.add( new Rect(buff[4*i], buff[4*i+1], buff[4*i+2], buff[4*i+3]) );\r
+               }\r
        }\r
 \r
        public static Mat vector_double_to_Mat(List<Double> ds) {\r
-               // TODO Auto-generated method stub\r
-               return null;\r
+               Mat res;\r
+               int count = (ds!=null) ? ds.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_64FC1); //Point can be saved into double[2]\r
+                       double[] buff = new double[count];\r
+                       for(int i=0; i<count; i++) {\r
+                               double v = ds.get(i);\r
+                               buff[i]   = v;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
        }\r
 \r
 }\r