// static cv::Mat_<float> CrossValidator::createCrossValidationMatrix( const vector< const vector<cv::Mat_<float> >* >& rowVectors, cv::Mat_<int>& labels) { assert( !rowVectors.empty()); cv::Mat_<float> vecs; labels.create( 0,1); for ( int label = 0; label < rowVectors.size(); ++label) { assert( rowVectors[label] != 0); const vector<cv::Mat_<float> >& rvecs = *rowVectors[label]; assert( !rvecs.empty()); const int colDim = rvecs[0].cols; // Should be the length of each row vector in this class if ( vecs.empty()) vecs.create(0, colDim); assert( colDim == vecs.cols); // Ensure this class's row vector length matches what's already stored for ( int i = 0; i < rvecs.size(); ++i) { const cv::Mat_<float>& rv = rvecs[i]; if ( rv.rows != 1 || rv.cols != colDim) { std::cerr << "ERROR feature vector size: " << rv.size() << std::endl; assert( rv.rows == 1 && rv.cols == colDim); } // end if vecs.push_back( rv); // Append the row vector to the bottom of the matrix labels.push_back(label); // Set this vector's class label } // end for } // end for labels = labels.t(); // Make row vector return vecs; } // end createCrossValidationMatrix
bool from_file( const std::string& fname, cv::Mat_<T>& out_mat, std::function< void(const std::string&, const int line) > error_invalid_mat_callback = on_invalid_mat) { bool ret; out_mat.release(); ret = getlines_from_file( fname, [&]( string& line, uint line_nr) { std::vector<T> v = from_string<T,vector>( line); if( v.size()==0 || out_mat.rows != 0 && v.size() != out_mat.cols) { error_invalid_mat_callback(fname, line_nr); ret = false; } else { out_mat.push_back( cv::Mat_<T>(cv::Mat_<T>(v).t())); // not gonna love the MatExpr } }); return ret; }
// Pick only the more stable/rigid points under changes of expression void extract_rigid_points(cv::Mat_<double>& source_points, cv::Mat_<double>& destination_points) { if(source_points.rows == 68) { cv::Mat_<double> tmp_source = source_points.clone(); source_points = cv::Mat_<double>(); // Push back the rigid points (some face outline, eyes, and nose) source_points.push_back(tmp_source.row(1)); source_points.push_back(tmp_source.row(2)); source_points.push_back(tmp_source.row(3)); source_points.push_back(tmp_source.row(4)); source_points.push_back(tmp_source.row(12)); source_points.push_back(tmp_source.row(13)); source_points.push_back(tmp_source.row(14)); source_points.push_back(tmp_source.row(15)); source_points.push_back(tmp_source.row(27)); source_points.push_back(tmp_source.row(28)); source_points.push_back(tmp_source.row(29)); source_points.push_back(tmp_source.row(31)); source_points.push_back(tmp_source.row(32)); source_points.push_back(tmp_source.row(33)); source_points.push_back(tmp_source.row(34)); source_points.push_back(tmp_source.row(35)); source_points.push_back(tmp_source.row(36)); source_points.push_back(tmp_source.row(39)); source_points.push_back(tmp_source.row(40)); source_points.push_back(tmp_source.row(41)); source_points.push_back(tmp_source.row(42)); source_points.push_back(tmp_source.row(45)); source_points.push_back(tmp_source.row(46)); source_points.push_back(tmp_source.row(47)); cv::Mat_<double> tmp_dest = destination_points.clone(); destination_points = cv::Mat_<double>(); // Push back the rigid points destination_points.push_back(tmp_dest.row(1)); destination_points.push_back(tmp_dest.row(2)); destination_points.push_back(tmp_dest.row(3)); destination_points.push_back(tmp_dest.row(4)); destination_points.push_back(tmp_dest.row(12)); destination_points.push_back(tmp_dest.row(13)); destination_points.push_back(tmp_dest.row(14)); destination_points.push_back(tmp_dest.row(15)); destination_points.push_back(tmp_dest.row(27)); destination_points.push_back(tmp_dest.row(28)); destination_points.push_back(tmp_dest.row(29)); destination_points.push_back(tmp_dest.row(31)); destination_points.push_back(tmp_dest.row(32)); destination_points.push_back(tmp_dest.row(33)); destination_points.push_back(tmp_dest.row(34)); destination_points.push_back(tmp_dest.row(35)); destination_points.push_back(tmp_dest.row(36)); destination_points.push_back(tmp_dest.row(39)); destination_points.push_back(tmp_dest.row(40)); destination_points.push_back(tmp_dest.row(41)); destination_points.push_back(tmp_dest.row(42)); destination_points.push_back(tmp_dest.row(45)); destination_points.push_back(tmp_dest.row(46)); destination_points.push_back(tmp_dest.row(47)); } }