c++ - OpenCV memory error occurs while tring to calculate the sum of 2 matrices -
i found code , try compile it, not work. error brought @ line:
q_matrix_tilde += q_tilde * q_tilde_transposed;
it told me there error related memory, not understand why. me?
// alessandro gentilini #include <cv.h> #include <cxcore.h> #include <vector> using namespace std; using namespace cv; // estimation of affine 2d transformation means of least squares method. // reference: // spÄth, helmuth. fitting affine , orthogonal transformations between 2 sets of points. mathematical communications, 2004, 9.1: 27-34. // http://hrcak.srce.hr/file/1425 template < typename point2d_t, typename point3d_t > class leastsquare2daffinetransformationestimator { public: // solves linear systems descripted formula (17) static cv::mat estimate( const std::vector<point2d_t>& p, const std::vector<point2d_t>& q ) { mat q_tilde = q_set_to_q_matrix_tilde(p); mat c_tilde_0 = c_j_tilde(0,p,q); mat c_tilde_1 = c_j_tilde(1,p,q); mat q_tilde_inv = q_tilde.inv(); mat a_tilde_0 = q_tilde_inv * c_tilde_0; mat a_tilde_1 = q_tilde_inv * c_tilde_1; cv::mat t = cv::mat::zeros( 3, 2, cv::datatype<point2d_t::value_type>::type ); cv::mat(a_tilde_0).copyto(t.col(0)); cv::mat(a_tilde_1).copyto(t.col(1)); cv::transpose(t,t); return t; } private: // implements formula (12) static cv::mat q_to_q_tilde( const point2d_t& q ) { vector<point2d_t> v; v.push_back(point2d_t(q.x)); v.push_back(point2d_t(q.y)); v.push_back(point2d_t(1)); return cv::mat(v,true); } // implements formula (14) static cv::mat q_set_to_q_matrix_tilde( const std::vector<point2d_t>& q_set ) { size_t m = q_set.size(); cv::mat q_matrix_tilde = cv::mat::zeros( 3, 3, cv::datatype<point2d_t::value_type>::type ); cv::mat temp= cv::mat::zeros( 3, 3, cv::datatype<point2d_t::value_type>::type ); cv::mat temp1= cv::mat::zeros( 3, 3, cv::datatype<point2d_t::value_type>::type ); cv::mat q_tilde = cv::mat::zeros( 3, 1, cv::datatype<point2d_t::value_type>::type ); cv::mat q_tilde_transposed = cv::mat::zeros( 1, 3, cv::datatype<point2d_t::value_type>::type ); ( size_t = 0; < m; i++ ) { q_tilde = q_to_q_tilde(q_set[i]); cv::transpose( q_tilde, q_tilde_transposed ); /*cout<<q_tilde_transposed.col<<" "<<q_tilde_transposed.row<<endl;*/ temp = q_tilde * q_tilde_transposed; cv::add(temp,q_matrix_tilde,temp1); } return q_matrix_tilde; } // implements formula (16) static cv::mat c_j_tilde( const size_t& j, const std::vector<point2d_t>& q_set, const std::vector<point2d_t>& p_set ) { if ( q_set.size() != p_set.size() ) { throw 0; } if ( j > 2 ) { throw 1; } size_t m = q_set.size(); point2d_t::value_type p_ji; point2d_t::value_type c_j0 = 0; ( size_t = 0; < m; i++ ) { switch( j ) { case 0: p_ji = p_set[i].x; break; case 1: p_ji = p_set[i].y; break; } c_j0 += q_set[i].x * p_ji; } point2d_t::value_type c_j1 = 0; ( size_t = 0; < m; i++ ) { switch( j ) { case 0: p_ji = p_set[i].x; break; case 1: p_ji = p_set[i].y; break; } c_j1 += q_set[i].y * p_ji; } point2d_t::value_type c_j2 = 0; ( size_t = 0; < m; i++ ) { switch( j ) { case 0: p_ji = p_set[i].x; break; case 1: p_ji = p_set[i].y; break; } c_j2 += 1 * p_ji; } vector<point2d_t> v; v.push_back(point2d_t(c_j0)); v.push_back(point2d_t(c_j1)); v.push_back(point2d_t(c_j2)); cv::mat vv = mat(v,true); return vv; } }; #include <vector> #include <iostream> int main( int argc, char** argv ) { std::vector<cv::point2f> p,q; p.push_back(cv::point2f( 1, 0)); p.push_back(cv::point2f( 0, 1)); p.push_back(cv::point2f(-1, 0)); p.push_back(cv::point2f( 0,-1)); q.push_back(cv::point2f(1+sqrtf(2)/2, 1+sqrtf(2)/2)); q.push_back(cv::point2f(1-sqrtf(2)/2, 1+sqrtf(2)/2)); q.push_back(cv::point2f(1-sqrtf(2)/2, 1-sqrtf(2)/2)); q.push_back(cv::point2f(1+sqrtf(2)/2, 1-sqrtf(2)/2)); //std::cout << leastsquare2daffinetransformationestimator<cv::point2f,cv::point3f>::estimate(p,q); return 0; }
your code works perfect. have run , result:
[0.70710683, -0.70710683 1, 0.70710683, 0.70710683 1]
Comments
Post a Comment