Path: blob/master/modules/java/generator/src/cpp/converters.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html34#define LOG_TAG "org.opencv.utils.Converters"5#include "common.h"67using namespace cv;89// vector_int1011void Mat_to_vector_int(Mat& mat, std::vector<int>& v_int)12{13v_int.clear();14CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1);15v_int = (std::vector<int>) mat;16}1718void vector_int_to_Mat(std::vector<int>& v_int, Mat& mat)19{20mat = Mat(v_int, true);21}222324//vector_double2526void Mat_to_vector_double(Mat& mat, std::vector<double>& v_double)27{28v_double.clear();29CHECK_MAT(mat.type()==CV_64FC1 && mat.cols==1);30v_double = (std::vector<double>) mat;31}3233void vector_double_to_Mat(std::vector<double>& v_double, Mat& mat)34{35mat = Mat(v_double, true);36}373839// vector_float4041void Mat_to_vector_float(Mat& mat, std::vector<float>& v_float)42{43v_float.clear();44CHECK_MAT(mat.type()==CV_32FC1 && mat.cols==1);45v_float = (std::vector<float>) mat;46}4748void vector_float_to_Mat(std::vector<float>& v_float, Mat& mat)49{50mat = Mat(v_float, true);51}525354//vector_uchar5556void Mat_to_vector_uchar(Mat& mat, std::vector<uchar>& v_uchar)57{58v_uchar.clear();59CHECK_MAT(mat.type()==CV_8UC1 && mat.cols==1);60v_uchar = (std::vector<uchar>) mat;61}6263void vector_uchar_to_Mat(std::vector<uchar>& v_uchar, Mat& mat)64{65mat = Mat(v_uchar, true);66}6768void Mat_to_vector_char(Mat& mat, std::vector<char>& v_char)69{70v_char.clear();71CHECK_MAT(mat.type()==CV_8SC1 && mat.cols==1);72v_char = (std::vector<char>) mat;73}7475void vector_char_to_Mat(std::vector<char>& v_char, Mat& mat)76{77mat = Mat(v_char, true);78}798081//vector_Rect8283void Mat_to_vector_Rect(Mat& mat, std::vector<Rect>& v_rect)84{85v_rect.clear();86CHECK_MAT(mat.type()==CV_32SC4 && mat.cols==1);87v_rect = (std::vector<Rect>) mat;88}8990void vector_Rect_to_Mat(std::vector<Rect>& v_rect, Mat& mat)91{92mat = Mat(v_rect, true);93}9495//vector_Rect2d9697void Mat_to_vector_Rect2d(Mat& mat, std::vector<Rect2d>& v_rect)98{99v_rect.clear();100CHECK_MAT(mat.type()==CV_64FC4 && mat.cols==1);101v_rect = (std::vector<Rect2d>) mat;102}103104void vector_Rect2d_to_Mat(std::vector<Rect2d>& v_rect, Mat& mat)105{106mat = Mat(v_rect, true);107}108109//vector_Point110void Mat_to_vector_Point(Mat& mat, std::vector<Point>& v_point)111{112v_point.clear();113CHECK_MAT(mat.type()==CV_32SC2 && mat.cols==1);114v_point = (std::vector<Point>) mat;115}116117//vector_Point2f118void Mat_to_vector_Point2f(Mat& mat, std::vector<Point2f>& v_point)119{120v_point.clear();121CHECK_MAT(mat.type()==CV_32FC2 && mat.cols==1);122v_point = (std::vector<Point2f>) mat;123}124125//vector_Point2d126void Mat_to_vector_Point2d(Mat& mat, std::vector<Point2d>& v_point)127{128v_point.clear();129CHECK_MAT(mat.type()==CV_64FC2 && mat.cols==1);130v_point = (std::vector<Point2d>) mat;131}132133134//vector_Point3i135void Mat_to_vector_Point3i(Mat& mat, std::vector<Point3i>& v_point)136{137v_point.clear();138CHECK_MAT(mat.type()==CV_32SC3 && mat.cols==1);139v_point = (std::vector<Point3i>) mat;140}141142//vector_Point3f143void Mat_to_vector_Point3f(Mat& mat, std::vector<Point3f>& v_point)144{145v_point.clear();146CHECK_MAT(mat.type()==CV_32FC3 && mat.cols==1);147v_point = (std::vector<Point3f>) mat;148}149150//vector_Point3d151void Mat_to_vector_Point3d(Mat& mat, std::vector<Point3d>& v_point)152{153v_point.clear();154CHECK_MAT(mat.type()==CV_64FC3 && mat.cols==1);155v_point = (std::vector<Point3d>) mat;156}157158159void vector_Point_to_Mat(std::vector<Point>& v_point, Mat& mat)160{161mat = Mat(v_point, true);162}163164void vector_Point2f_to_Mat(std::vector<Point2f>& v_point, Mat& mat)165{166mat = Mat(v_point, true);167}168169void vector_Point2d_to_Mat(std::vector<Point2d>& v_point, Mat& mat)170{171mat = Mat(v_point, true);172}173174void vector_Point3i_to_Mat(std::vector<Point3i>& v_point, Mat& mat)175{176mat = Mat(v_point, true);177}178179void vector_Point3f_to_Mat(std::vector<Point3f>& v_point, Mat& mat)180{181mat = Mat(v_point, true);182}183184void vector_Point3d_to_Mat(std::vector<Point3d>& v_point, Mat& mat)185{186mat = Mat(v_point, true);187}188189//vector_Mat190void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat)191{192v_mat.clear();193if(mat.type() == CV_32SC2 && mat.cols == 1)194{195v_mat.reserve(mat.rows);196for(int i=0; i<mat.rows; i++)197{198Vec<int, 2> a = mat.at< Vec<int, 2> >(i, 0);199long long addr = (((long long)a[0])<<32) | (a[1]&0xffffffff);200Mat& m = *( (Mat*) addr );201v_mat.push_back(m);202}203} else {204LOGD("Mat_to_vector_Mat() FAILED: mat.type() == CV_32SC2 && mat.cols == 1");205}206}207208209void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)210{211int count = (int)v_mat.size();212mat.create(count, 1, CV_32SC2);213for(int i=0; i<count; i++)214{215long long addr = (long long) new Mat(v_mat[i]);216mat.at< Vec<int, 2> >(i, 0) = Vec<int, 2>(addr>>32, addr&0xffffffff);217}218}219220void Mat_to_vector_vector_Point(Mat& mat, std::vector< std::vector< Point > >& vv_pt)221{222std::vector<Mat> vm;223vm.reserve( mat.rows );224Mat_to_vector_Mat(mat, vm);225for(size_t i=0; i<vm.size(); i++)226{227std::vector<Point> vpt;228Mat_to_vector_Point(vm[i], vpt);229vv_pt.push_back(vpt);230}231}232233void Mat_to_vector_vector_Point2f(Mat& mat, std::vector< std::vector< Point2f > >& vv_pt)234{235std::vector<Mat> vm;236vm.reserve( mat.rows );237Mat_to_vector_Mat(mat, vm);238for(size_t i=0; i<vm.size(); i++)239{240std::vector<Point2f> vpt;241Mat_to_vector_Point2f(vm[i], vpt);242vv_pt.push_back(vpt);243}244}245246void Mat_to_vector_vector_Point3f(Mat& mat, std::vector< std::vector< Point3f > >& vv_pt)247{248std::vector<Mat> vm;249vm.reserve( mat.rows );250Mat_to_vector_Mat(mat, vm);251for(size_t i=0; i<vm.size(); i++)252{253std::vector<Point3f> vpt;254Mat_to_vector_Point3f(vm[i], vpt);255vv_pt.push_back(vpt);256}257}258259void Mat_to_vector_vector_char(Mat& mat, std::vector< std::vector< char > >& vv_ch)260{261std::vector<Mat> vm;262vm.reserve( mat.rows );263Mat_to_vector_Mat(mat, vm);264for(size_t i=0; i<vm.size(); i++)265{266std::vector<char> vch;267Mat_to_vector_char(vm[i], vch);268vv_ch.push_back(vch);269}270}271272void vector_vector_char_to_Mat(std::vector< std::vector< char > >& vv_ch, Mat& mat)273{274std::vector<Mat> vm;275vm.reserve( vv_ch.size() );276for(size_t i=0; i<vv_ch.size(); i++)277{278Mat m;279vector_char_to_Mat(vv_ch[i], m);280vm.push_back(m);281}282vector_Mat_to_Mat(vm, mat);283}284285void vector_vector_Point_to_Mat(std::vector< std::vector< Point > >& vv_pt, Mat& mat)286{287std::vector<Mat> vm;288vm.reserve( vv_pt.size() );289for(size_t i=0; i<vv_pt.size(); i++)290{291Mat m;292vector_Point_to_Mat(vv_pt[i], m);293vm.push_back(m);294}295vector_Mat_to_Mat(vm, mat);296}297298void vector_vector_Point2f_to_Mat(std::vector< std::vector< Point2f > >& vv_pt, Mat& mat)299{300std::vector<Mat> vm;301vm.reserve( vv_pt.size() );302for(size_t i=0; i<vv_pt.size(); i++)303{304Mat m;305vector_Point2f_to_Mat(vv_pt[i], m);306vm.push_back(m);307}308vector_Mat_to_Mat(vm, mat);309}310311void vector_vector_Point3f_to_Mat(std::vector< std::vector< Point3f > >& vv_pt, Mat& mat)312{313std::vector<Mat> vm;314vm.reserve( vv_pt.size() );315for(size_t i=0; i<vv_pt.size(); i++)316{317Mat m;318vector_Point3f_to_Mat(vv_pt[i], m);319vm.push_back(m);320}321vector_Mat_to_Mat(vm, mat);322}323324void vector_Vec4i_to_Mat(std::vector<Vec4i>& v_vec, Mat& mat)325{326mat = Mat(v_vec, true);327}328329void vector_Vec4f_to_Mat(std::vector<Vec4f>& v_vec, Mat& mat)330{331mat = Mat(v_vec, true);332}333334void vector_Vec6f_to_Mat(std::vector<Vec6f>& v_vec, Mat& mat)335{336mat = Mat(v_vec, true);337}338339340