converters.cpp 3.8 KB
Newer Older
1
#include "converters.h"
2 3

#ifdef DEBUG
4
#include <android/log.h>
5
#define MODULE_LOG_TAG "OpenCV.converters"
6
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
7 8 9
#else //DEBUG
#define LOGD(...)
#endif //DEBUG
10

11 12
using namespace cv;

13 14 15
#define CHECK_MAT(cond) if(cond){ LOGD(#cond); return; }


16 17 18 19
// vector_int

void Mat_to_vector_int(Mat& mat, vector<int>& v_int)
{
20
	v_int.clear();
21
	CHECK_MAT(mat.type()!= CV_32SC1 || mat.rows!=1);
22
	v_int = (vector<int>) mat;
23 24 25 26
}

void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
{
27
	mat = Mat(v_int);
28 29 30 31 32 33 34
}


//vector_double

void Mat_to_vector_double(Mat& mat, vector<double>& v_double)
{
35
	v_double.clear();
36
	CHECK_MAT(mat.type()!= CV_64FC1 || mat.rows!=1);
37
	v_double = (vector<double>) mat;
38 39 40 41
}

void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
{
42
	mat = Mat(v_double);
43 44 45 46 47 48 49
}


// vector_float

void Mat_to_vector_float(Mat& mat, vector<float>& v_float)
{
50
	v_float.clear();
51
	CHECK_MAT(mat.type()!= CV_32FC1 || mat.rows!=1);
52
	v_float = (vector<float>) mat;
53 54 55 56
}

void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
{
57
	mat = Mat(v_float);
58 59 60 61 62
}


//vector_uchar

63
void Mat_to_vector_uchar(Mat& mat, vector<uchar>& v_uchar)
64
{
65
	v_uchar.clear();
66
	CHECK_MAT(mat.type()!= CV_8UC1 || mat.rows!=1);
67
	v_uchar = (vector<uchar>) mat;
68 69 70 71 72 73 74
}


//vector_Rect

void Mat_to_vector_Rect(Mat& mat, vector<Rect>& v_rect)
{
75
	v_rect.clear();
76 77 78
	CHECK_MAT(mat.type()!= CV_32SC4 || mat.rows!=1);
	v_rect = (vector<Rect>) mat;
	/*for(int i=0; i<mat.cols; i++) {
79 80
		Vec<int, 4> v=mat.at< Vec<int, 4> >(0, i);
		v_rect.push_back( Rect(v[0], v[1], v[2], v[3]) );
81
	}*/
82 83 84 85
}

void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{
86 87
	mat = Mat(v_rect);
	/*mat.create(1, v_rect.size(), CV_32SC4);
88 89
	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);
90
	}*/
91 92 93 94 95 96
}


//vector_Point
void Mat_to_vector_Point(Mat& mat, vector<Point>& v_point)
{
97
	v_point.clear();
98 99 100 101
	CHECK_MAT(mat.type()!= CV_32SC2 || mat.rows!=1);
	v_point = (vector<Point>) mat;
	/*for(int i=0; i<mat.cols; i++)
		v_point.push_back( Point( mat.at< Vec<int, 2> >(0, i) ) );*/
102 103 104 105 106
}


void vector_Point_to_Mat(vector<Point>& v_point, Mat& mat)
{
107 108
	mat = Mat(v_point);
	/*mat.create(1, v_point.size(), CV_32SC2);
109
	for(size_t i=0; i<v_point.size(); i++)
110
		mat.at< Vec<int, 2> >(0, i) = Vec<int, 2>(v_point[i].x, v_point[i].y);*/
111 112 113 114 115 116
}


//vector_KeyPoint
void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& v_kp)
{
117 118 119 120 121 122 123 124
    v_kp.clear();
    CHECK_MAT(mat.type()!= CV_64FC(7) || mat.rows!=1);
	for(int i=0; i<mat.cols; i++)
	{
		Vec<double, 7> v = mat.at< Vec<double, 7> >(0, i);
		KeyPoint kp((float)v[0], (float)v[1], (float)v[2], (float)v[3], (float)v[4], (int)v[5], (int)v[6]);
		v_kp.push_back(kp);
	}
125 126 127 128 129 130
    return;
}


void vector_KeyPoint_to_Mat(vector<KeyPoint>& v_kp, Mat& mat)
{
131 132 133 134 135 136 137
	int count = v_kp.size();
	mat.create(1, count, CV_64FC(7));
	for(int i=0; i<count; i++)
	{
		KeyPoint kp = v_kp[i];
		mat.at< Vec<double, 7> >(0, i) = Vec<double, 7>(kp.pt.x, kp.pt.y, kp.size, kp.angle, kp.response, kp.octave, kp.class_id);
	}
138 139 140 141 142 143
}


//vector_Mat
void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat)
{
144 145 146 147 148 149 150 151 152 153 154
	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);
		}
	}
155 156 157 158 159
}


void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)
{
160 161 162 163
	int count = v_mat.size();
	mat.create(1, count, CV_32SC2);
	for(int i=0; i<count; i++)
	{
164
		long long addr = (long long) new Mat(v_mat[i]);
165 166
		mat.at< Vec<int, 2> >(0, i) = Vec<int, 2>(addr>>32, addr&0xffffffff);
	}
167
}