1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "converters.h"
#ifdef DEBUG
#include <android/log.h>
#define MODULE_LOG_TAG "OpenCV.converters"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
#else //DEBUG
#define LOGD(...)
#endif //DEBUG
using namespace cv;
// vector_int
void Mat_to_vector_int(Mat& mat, vector<int>& v_int)
{
v_int.clear();
if(mat.type()!= CV_32SC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_int.push_back( mat.at< int >(0, i) );
*/
v_int = (vector<int>) mat;
}
void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
{
/*
mat.create(1, v_int.size(), CV_32SC1);
for(size_t i=0; i<v_int.size(); i++)
mat.at< int >(0, i) = v_int[i];
*/
mat = Mat(v_int);
}
//vector_double
void Mat_to_vector_double(Mat& mat, vector<double>& v_double)
{
v_double.clear();
if(mat.type()!= CV_64FC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_double.push_back( mat.at< double >(0, i) );
*/
v_double = (vector<double>) mat;
}
void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
{
/*
mat.create(1, v_double.size(), CV_64FC1);
for(size_t i=0; i<v_double.size(); i++)
mat.at< double >(0, i) = v_double[i];
*/
mat = Mat(v_double);
}
// vector_float
void Mat_to_vector_float(Mat& mat, vector<float>& v_float)
{
v_float.clear();
if(mat.type()!= CV_32FC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_float.push_back( mat.at< float >(0, i) );
*/
v_float = (vector<float>) mat;
}
void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
{
/*
mat.create(1, v_float.size(), CV_32FC1);
for(size_t i=0; i<v_float.size(); i++)
mat.at< float >(0, i) = v_float[i];
*/
mat = Mat(v_float);
}
//vector_uchar
void Mat_to_vector_uchar(Mat& mat, vector<uchar>& v_uchar)
{
v_uchar.clear();
if(mat.type()!= CV_8UC1 || mat.rows!=1)
return;
/*
for(int i=0; i<mat.cols; i++)
v_uchar.push_back( mat.at< uchar >(0, i) );
*/
v_uchar = (vector<uchar>) mat;
}
//vector_Rect
void Mat_to_vector_Rect(Mat& mat, vector<Rect>& v_rect)
{
v_rect.clear();
if(mat.type()!= CV_32SC4 || mat.rows!=1) {
LOGD("ERROR mat.type()!= CV_32SC4 || mat.rows!=1");
return;
}
for(int i=0; i<mat.cols; i++) {
Vec<int, 4> v=mat.at< Vec<int, 4> >(0, i);
v_rect.push_back( Rect(v[0], v[1], v[2], v[3]) );
}
}
void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{
mat.create(1, v_rect.size(), CV_32SC4);
for(size_t i=0; i<v_rect.size(); i++) {
mat.at< Vec<int, 4> >(0, i) = Vec<int, 4>(v_rect[i].x, v_rect[i].y, v_rect[i].width, v_rect[i].height);
}
}
//vector_Point
void Mat_to_vector_Point(Mat& mat, vector<Point>& v_point)
{
v_point.clear();
if(mat.type()!= CV_32SC2 || mat.rows!=1)
return;
for(int i=0; i<mat.cols; i++)
v_point.push_back( Point( mat.at< Vec<int, 2> >(0, i) ) );
}
void vector_Point_to_Mat(vector<Point>& v_point, Mat& mat)
{
mat.create(1, v_point.size(), CV_32SC2);
for(size_t i=0; i<v_point.size(); i++)
mat.at< Vec<int, 2> >(0, i) = Vec<int, 2>(v_point[i].x, v_point[i].y);
}
//vector_KeyPoint
void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& v_kp)
{
return;
}
void vector_KeyPoint_to_Mat(vector<KeyPoint>& v_kp, Mat& mat)
{
return;
}
//vector_Mat
void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat)
{
v_mat.clear();
if(mat.type() == CV_32SC2 && mat.rows == 1)
{
for(int i=0; i<mat.cols; i++)
{
Vec<int, 2> a = mat.at< Vec<int, 2> >(0, i);
long long addr = (((long long)a[0])<<32) | a[1];
Mat& m = *( (Mat*) addr );
v_mat.push_back(m);
}
}
}
void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)
{
int count = v_mat.size();
mat.create(1, count, CV_32SC2);
for(int i=0; i<count; i++)
{
long long addr = (long long) new Mat(v_mat[i]);
mat.at< Vec<int, 2> >(0, i) = Vec<int, 2>(addr>>32, addr&0xffffffff);
}
return;
}