1 #define LOG_TAG "org.opencv.utils.Converters"
2 #include "common.h"
3 #include "features2d_converters.hpp"
4 
5 using namespace cv;
6 
7 #define CHECK_MAT(cond) if(!(cond)){ LOGD("FAILED: " #cond); return; }
8 
9 
10 //vector_KeyPoint
Mat_to_vector_KeyPoint(Mat & mat,std::vector<KeyPoint> & v_kp)11 void Mat_to_vector_KeyPoint(Mat& mat, std::vector<KeyPoint>& v_kp)
12 {
13     v_kp.clear();
14     CHECK_MAT(mat.type()==CV_32FC(7) && mat.cols==1);
15     for(int i=0; i<mat.rows; i++)
16     {
17         Vec<float, 7> v = mat.at< Vec<float, 7> >(i, 0);
18         KeyPoint kp(v[0], v[1], v[2], v[3], v[4], (int)v[5], (int)v[6]);
19         v_kp.push_back(kp);
20     }
21     return;
22 }
23 
24 
vector_KeyPoint_to_Mat(std::vector<KeyPoint> & v_kp,Mat & mat)25 void vector_KeyPoint_to_Mat(std::vector<KeyPoint>& v_kp, Mat& mat)
26 {
27     int count = (int)v_kp.size();
28     mat.create(count, 1, CV_32FC(7));
29     for(int i=0; i<count; i++)
30     {
31         KeyPoint kp = v_kp[i];
32         mat.at< Vec<float, 7> >(i, 0) = Vec<float, 7>(kp.pt.x, kp.pt.y, kp.size, kp.angle, kp.response, (float)kp.octave, (float)kp.class_id);
33     }
34 }
35 
36 //vector_DMatch
Mat_to_vector_DMatch(Mat & mat,std::vector<DMatch> & v_dm)37 void Mat_to_vector_DMatch(Mat& mat, std::vector<DMatch>& v_dm)
38 {
39     v_dm.clear();
40     CHECK_MAT(mat.type()==CV_32FC4 && mat.cols==1);
41     for(int i=0; i<mat.rows; i++)
42     {
43         Vec<float, 4> v = mat.at< Vec<float, 4> >(i, 0);
44         DMatch dm((int)v[0], (int)v[1], (int)v[2], v[3]);
45         v_dm.push_back(dm);
46     }
47     return;
48 }
49 
50 
vector_DMatch_to_Mat(std::vector<DMatch> & v_dm,Mat & mat)51 void vector_DMatch_to_Mat(std::vector<DMatch>& v_dm, Mat& mat)
52 {
53     int count = (int)v_dm.size();
54     mat.create(count, 1, CV_32FC4);
55     for(int i=0; i<count; i++)
56     {
57         DMatch dm = v_dm[i];
58         mat.at< Vec<float, 4> >(i, 0) = Vec<float, 4>((float)dm.queryIdx, (float)dm.trainIdx, (float)dm.imgIdx, dm.distance);
59     }
60 }
61 
Mat_to_vector_vector_KeyPoint(Mat & mat,std::vector<std::vector<KeyPoint>> & vv_kp)62 void Mat_to_vector_vector_KeyPoint(Mat& mat, std::vector< std::vector< KeyPoint > >& vv_kp)
63 {
64     std::vector<Mat> vm;
65     vm.reserve( mat.rows );
66     Mat_to_vector_Mat(mat, vm);
67     for(size_t i=0; i<vm.size(); i++)
68     {
69         std::vector<KeyPoint> vkp;
70         Mat_to_vector_KeyPoint(vm[i], vkp);
71         vv_kp.push_back(vkp);
72     }
73 }
74 
vector_vector_KeyPoint_to_Mat(std::vector<std::vector<KeyPoint>> & vv_kp,Mat & mat)75 void vector_vector_KeyPoint_to_Mat(std::vector< std::vector< KeyPoint > >& vv_kp, Mat& mat)
76 {
77     std::vector<Mat> vm;
78     vm.reserve( vv_kp.size() );
79     for(size_t i=0; i<vv_kp.size(); i++)
80     {
81         Mat m;
82         vector_KeyPoint_to_Mat(vv_kp[i], m);
83         vm.push_back(m);
84     }
85     vector_Mat_to_Mat(vm, mat);
86 }
87 
Mat_to_vector_vector_DMatch(Mat & mat,std::vector<std::vector<DMatch>> & vv_dm)88 void Mat_to_vector_vector_DMatch(Mat& mat, std::vector< std::vector< DMatch > >& vv_dm)
89 {
90     std::vector<Mat> vm;
91     vm.reserve( mat.rows );
92     Mat_to_vector_Mat(mat, vm);
93     for(size_t i=0; i<vm.size(); i++)
94     {
95         std::vector<DMatch> vdm;
96         Mat_to_vector_DMatch(vm[i], vdm);
97         vv_dm.push_back(vdm);
98     }
99 }
100 
vector_vector_DMatch_to_Mat(std::vector<std::vector<DMatch>> & vv_dm,Mat & mat)101 void vector_vector_DMatch_to_Mat(std::vector< std::vector< DMatch > >& vv_dm, Mat& mat)
102 {
103     std::vector<Mat> vm;
104     vm.reserve( vv_dm.size() );
105     for(size_t i=0; i<vv_dm.size(); i++)
106     {
107         Mat m;
108         vector_DMatch_to_Mat(vv_dm[i], m);
109         vm.push_back(m);
110     }
111     vector_Mat_to_Mat(vm, mat);
112 }
113