static Mat asRowMatrix(InputArrayOfArrays src, int rtype, double alpha=1, double beta=0) { // make sure the input data is a vector of matrices or vector of vector if(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) { String error_message = "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< std::vector<...> >)."; CV_Error(Error::StsBadArg, error_message); } // number of samples size_t n = src.total(); // return empty matrix if no matrices given if(n == 0) return Mat(); // dimensionality of (reshaped) samples size_t d = src.getMat(0).total(); // create data matrix Mat data((int)n, (int)d, rtype); // now copy data for(unsigned int i = 0; i < n; i++) { // make sure data can be reshaped, throw exception if not! if(src.getMat(i).total() != d) { String error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total()); CV_Error(Error::StsBadArg, error_message); } // get a hold of the current row Mat xi = data.row(i); // make reshape happy by cloning for non-continuous matrices if(src.getMat(i).isContinuous()) { src.getMat(i).reshape(1, 1).convertTo(xi, rtype, alpha, beta); } else { src.getMat(i).clone().reshape(1, 1).convertTo(xi, rtype, alpha, beta); } } return data; }
void reconstruct(InputArrayOfArrays points2d, OutputArray Rs, OutputArray Ts, InputOutputArray K, OutputArray points3d, bool is_projective) { const int nviews = points2d.total(); CV_Assert( nviews >= 2 ); // Projective reconstruction if (is_projective) { // calls simple pipeline reconstruct_(points2d, Rs, Ts, K, points3d); } // Affine reconstruction else { // TODO: implement me CV_Error(Error::StsNotImplemented, "Affine reconstruction not yet implemented"); } }
//------------------------------------------------------------------------------ // libfacerec::asColumnMatrix //------------------------------------------------------------------------------ Mat libfacerec::asColumnMatrix(InputArrayOfArrays src, int rtype, double alpha, double beta) { // make sure the input data is a vector of matrices or vector of vector if(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) { CV_Error(CV_StsBadArg, "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."); } int n = (int) src.total(); // return empty matrix if no data given if(n == 0) return Mat(); // dimensionality of samples int d = src.getMat(0).total(); // create data matrix Mat data(d, n, rtype); // now copy data for(int i = 0; i < n; i++) { // make sure data can be reshaped, throw exception if not! if(src.getMat(i).total() != d) { string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total()); CV_Error(CV_StsBadArg, error_message); } // get a hold of the current row Mat yi = data.col(i); // make reshape happy by cloning for non-continuous matrices if(src.getMat(i).isContinuous()) { src.getMat(i).reshape(1, d).convertTo(yi, rtype, alpha, beta); } else { src.getMat(i).clone().reshape(1, d).convertTo(yi, rtype, alpha, beta); } } return data; }
void LBPHFaceRecognizer::update(InputArrayOfArrays _in_src, InputArray _inm_labels) { // got no data, just return if (_in_src.total() == 0) return; this->train(_in_src, _inm_labels, true); }
// Reconstruction function for API void reconstruct(const InputArrayOfArrays points2d, OutputArrayOfArrays projection_matrices, OutputArray points3d, bool is_projective, bool has_outliers, bool is_sequence) { int nviews = points2d.total(); cv::Mat F; // OpenCV data types std::vector<cv::Mat> pts2d; points2d.getMatVector(pts2d); int depth = pts2d[0].depth(); // Projective reconstruction if (is_projective) { // Two view reconstruction if (nviews == 2) { // Get fundamental matrix fundamental8Point(pts2d[0], pts2d[1], F, has_outliers); // Get Projection matrices cv::Mat P, Pp; projectionsFromFundamental(F, P, Pp); projection_matrices.create(2, 1, depth); P.copyTo(projection_matrices.getMatRef(0)); Pp.copyTo(projection_matrices.getMatRef(1)); // Triangulate and find 3D points using inliers triangulatePoints(points2d, projection_matrices, points3d); } } // Affine reconstruction else { // Two view reconstruction if (nviews == 2) { } else { } } }
double mycalibrateCamera( InputArrayOfArrays _objectPoints, InputArrayOfArrays _imagePoints, Size imageSize, InputOutputArray _cameraMatrix, InputOutputArray _distCoeffs, OutputArrayOfArrays _rvecs, OutputArrayOfArrays _tvecs, int flags, TermCriteria criteria ) { int rtype = CV_64F; Mat cameraMatrix = _cameraMatrix.getMat(); cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype); Mat distCoeffs = _distCoeffs.getMat(); distCoeffs = prepareDistCoeffs(distCoeffs, rtype); if( !(flags & CALIB_RATIONAL_MODEL) ) distCoeffs = distCoeffs.rows == 1 ? distCoeffs.colRange(0, 5) : distCoeffs.rowRange(0, 5); int i; size_t nimages = _objectPoints.total(); CV_Assert( nimages > 0 ); Mat objPt, imgPt, npoints, rvecM((int)nimages, 3, CV_64FC1), tvecM((int)nimages, 3, CV_64FC1); collectCalibrationData( _objectPoints, _imagePoints, noArray(), objPt, imgPt, 0, npoints ); CvMat c_objPt = objPt, c_imgPt = imgPt, c_npoints = npoints; CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs; CvMat c_rvecM = rvecM, c_tvecM = tvecM; double reprojErr = cvCalibrateCamera2(&c_objPt, &c_imgPt, &c_npoints, imageSize, &c_cameraMatrix, &c_distCoeffs, &c_rvecM, &c_tvecM, flags, criteria ); bool rvecs_needed = _rvecs.needed(), tvecs_needed = _tvecs.needed(); if( rvecs_needed ) _rvecs.create((int)nimages, 1, CV_64FC3); if( tvecs_needed ) _tvecs.create((int)nimages, 1, CV_64FC3); for( i = 0; i < (int)nimages; i++ ) { if( rvecs_needed ) { _rvecs.create(3, 1, CV_64F, i, true); Mat rv = _rvecs.getMat(i); memcpy(rv.data, rvecM.ptr<double>(i), 3*sizeof(double)); } if( tvecs_needed ) { _tvecs.create(3, 1, CV_64F, i, true); Mat tv = _tvecs.getMat(i); memcpy(tv.data, tvecM.ptr<double>(i), 3*sizeof(double)); } } cameraMatrix.copyTo(_cameraMatrix); distCoeffs.copyTo(_distCoeffs); return reprojErr; }
void cv::mixChannels(InputArrayOfArrays src, InputArrayOfArrays dst, const vector<int>& fromTo) { if(fromTo.empty()) return; bool src_is_mat = src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR; bool dst_is_mat = dst.kind() != _InputArray::STD_VECTOR_MAT && dst.kind() != _InputArray::STD_VECTOR_VECTOR; int i; int nsrc = src_is_mat ? 1 : (int)src.total(); int ndst = dst_is_mat ? 1 : (int)dst.total(); CV_Assert(fromTo.size()%2 == 0 && nsrc > 0 && ndst > 0); cv::AutoBuffer<Mat> _buf(nsrc + ndst); Mat* buf = _buf; for( i = 0; i < nsrc; i++ ) buf[i] = src.getMat(src_is_mat ? -1 : i); for( i = 0; i < ndst; i++ ) buf[nsrc + i] = dst.getMat(dst_is_mat ? -1 : i); mixChannels(&buf[0], nsrc, &buf[nsrc], ndst, &fromTo[0], fromTo.size()/2); }
void LBPH::train(InputArrayOfArrays _in_src, InputArray _in_labels, bool preserveData) { if(_in_src.kind() != _InputArray::STD_VECTOR_MAT && _in_src.kind() != _InputArray::STD_VECTOR_VECTOR) { String error_message = "The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< std::vector<...> >)."; CV_Error(Error::StsBadArg, error_message); } if(_in_src.total() == 0) { String error_message = format("Empty training data was given. You'll need more than one sample to learn a model."); CV_Error(Error::StsUnsupportedFormat, error_message); } else if(_in_labels.getMat().type() != CV_32SC1) { String error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _in_labels.type()); CV_Error(Error::StsUnsupportedFormat, error_message); } // get the vector of matrices std::vector<Mat> src; _in_src.getMatVector(src); // get the label matrix Mat labels = _in_labels.getMat(); // check if data is well- aligned if(labels.total() != src.size()) { String error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", src.size(), _labels.total()); CV_Error(Error::StsBadArg, error_message); } // if this model should be trained without preserving old data, delete old model data if(!preserveData) { _labels.release(); _histograms.clear(); } // append labels to _labels matrix for(size_t labelIdx = 0; labelIdx < labels.total(); labelIdx++) { _labels.push_back(labels.at<int>((int)labelIdx)); } // store the spatial histograms of the original data for(size_t sampleIdx = 0; sampleIdx < src.size(); sampleIdx++) { // calculate lbp image Mat lbp_image = elbp(src[sampleIdx], _radius, _neighbors); // get spatial histogram from this lbp image Mat p = spatial_histogram( lbp_image, /* lbp_image */ static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */ _grid_x, /* grid size x */ _grid_y, /* grid size y */ true); // add to templates _histograms.push_back(p); } }
static Mat asRowMatrix(InputArrayOfArrays src, int rtype, double alpha=1, double beta=0) { // number of samples int n = (int) src.total(); // return empty matrix if no data given if(n == 0) return Mat(); // dimensionality of samples int d = (int)src.getMat(0).total(); // create data matrix Mat data(n, d, rtype); // copy data for(int i = 0; i < n; i++) { Mat xi = data.row(i); src.getMat(i).reshape(1, 1).convertTo(xi, rtype, alpha, beta); } return data; }
void GaborLbp_Algorithm::train(InputArrayOfArrays _in_src, InputArray _in_labels) { if(_in_src.kind() != _InputArray::STD_VECTOR_MAT && _in_src.kind() != _InputArray::STD_VECTOR_VECTOR) { string error_message = "The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."; CV_Error(CV_StsBadArg, error_message); } if(_in_src.total() == 0) { string error_message = format("Empty training data was given. You'll need more than one sample to learn a model."); CV_Error(CV_StsUnsupportedFormat, error_message); } else if(_in_labels.getMat().type() != CV_32SC1) { string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _in_labels.type()); CV_Error(CV_StsUnsupportedFormat, error_message); } vector<Mat> src; _in_src.getMatVector(src); Mat labels = _in_labels.getMat(); if(labels.total() != src.size()) { string error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", src.size(), m_labels.total()); CV_Error(CV_StsBadArg, error_message); } int i=0,j=0; double m_kmax = CV_PI/2; double m_f = sqrt(double(2)); double m_sigma = 2*CV_PI; for (size_t sampleIdx = 0;sampleIdx<src.size();sampleIdx++) { int row = src[sampleIdx].rows; int col = src[sampleIdx].cols; if (row <= 0 || col <= 0) { continue; } m_labels.push_back(labels.at<int>((int)sampleIdx)); ZGabor m_gabor; m_gabor.InitGabor(); m_gabor.GetFeature(src[sampleIdx],1,8,8,8); cout<<sampleIdx<<endl; m_projection.push_back(m_gabor.m_eigenvector); } }
//------------------------------------------------------------------------------ // Fisherfaces //------------------------------------------------------------------------------ void Fisherfaces::train(InputArrayOfArrays src, InputArray _lbls) { if(src.total() == 0) { String error_message = format("Empty training data was given. You'll need more than one sample to learn a model."); CV_Error(Error::StsBadArg, error_message); } else if(_lbls.getMat().type() != CV_32SC1) { String error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _lbls.type()); CV_Error(Error::StsBadArg, error_message); } // make sure data has correct size if(src.total() > 1) { for(int i = 1; i < static_cast<int>(src.total()); i++) { if(src.getMat(i-1).total() != src.getMat(i).total()) { String error_message = format("In the Fisherfaces method all input samples (training images) must be of equal size! Expected %d pixels, but was %d pixels.", src.getMat(i-1).total(), src.getMat(i).total()); CV_Error(Error::StsUnsupportedFormat, error_message); } } } // get data Mat labels = _lbls.getMat(); Mat data = asRowMatrix(src, CV_64FC1); // number of samples int N = data.rows; // make sure labels are passed in correct shape if(labels.total() != (size_t) N) { String error_message = format("The number of samples (src) must equal the number of labels (labels)! len(src)=%d, len(labels)=%d.", N, labels.total()); CV_Error(Error::StsBadArg, error_message); } else if(labels.rows != 1 && labels.cols != 1) { String error_message = format("Expected the labels in a matrix with one row or column! Given dimensions are rows=%s, cols=%d.", labels.rows, labels.cols); CV_Error(Error::StsBadArg, error_message); } // clear existing model data _labels.release(); _projections.clear(); // safely copy from cv::Mat to std::vector std::vector<int> ll; for(unsigned int i = 0; i < labels.total(); i++) { ll.push_back(labels.at<int>(i)); } // get the number of unique classes int C = (int) remove_dups(ll).size(); // clip number of components to be a valid number if((_num_components <= 0) || (_num_components > (C-1))) _num_components = (C-1); // perform a PCA and keep (N-C) components PCA pca(data, Mat(), PCA::DATA_AS_ROW, (N-C)); // project the data and perform a LDA on it LDA lda(pca.project(data),labels, _num_components); // store the total mean vector _mean = pca.mean.reshape(1,1); // store labels _labels = labels.clone(); // store the eigenvalues of the discriminants lda.eigenvalues().convertTo(_eigenvalues, CV_64FC1); // Now calculate the projection matrix as pca.eigenvectors * lda.eigenvectors. // Note: OpenCV stores the eigenvectors by row, so we need to transpose it! gemm(pca.eigenvectors, lda.eigenvectors(), 1.0, Mat(), 0.0, _eigenvectors, GEMM_1_T); // store the projections of the original data for(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) { Mat p = LDA::subspaceProject(_eigenvectors, _mean, data.row(sampleIdx)); _projections.push_back(p); } }
void check(InputArrayOfArrays a) { cout << a.total() << endl; Mat b = a.getMat(); }
// Reconstruction function for API void reconstruct(InputArrayOfArrays points2d, OutputArray Ps, OutputArray points3d, InputOutputArray K, bool is_projective) { const int nviews = points2d.total(); CV_Assert( nviews >= 2 ); // OpenCV data types std::vector<Mat> pts2d; points2d.getMatVector(pts2d); const int depth = pts2d[0].depth(); Matx33d Ka = K.getMat(); // Projective reconstruction if (is_projective) { if ( nviews == 2 ) { // Get Projection matrices Matx33d F; Matx34d P, Pp; normalizedEightPointSolver(pts2d[0], pts2d[1], F); projectionsFromFundamental(F, P, Pp); Ps.create(2, 1, depth); Mat(P).copyTo(Ps.getMatRef(0)); Mat(Pp).copyTo(Ps.getMatRef(1)); // Triangulate and find 3D points using inliers triangulatePoints(points2d, Ps, points3d); } else { std::vector<Mat> Rs, Ts; reconstruct(points2d, Rs, Ts, Ka, points3d, is_projective); // From Rs and Ts, extract Ps const int nviews = Rs.size(); Ps.create(nviews, 1, depth); Matx34d P; for (size_t i = 0; i < nviews; ++i) { projectionFromKRt(Ka, Rs[i], Vec3d(Ts[i]), P); Mat(P).copyTo(Ps.getMatRef(i)); } Mat(Ka).copyTo(K.getMat()); } } // Affine reconstruction else { // TODO: implement me } }
void triangulatePoints(InputArrayOfArrays _points2d, InputArrayOfArrays _projection_matrices, OutputArray _points3d) { // check size_t nviews = (unsigned) _points2d.total(); CV_Assert(nviews >= 2 && nviews == _projection_matrices.total()); // inputs size_t n_points; vector<Mat_<double> > points2d(nviews); vector<Matx34d> projection_matrices(nviews); { vector<Mat> points2d_tmp; _points2d.getMatVector(points2d_tmp); n_points = points2d_tmp[0].cols; vector<Mat> projection_matrices_tmp; _projection_matrices.getMatVector(projection_matrices_tmp); // Make sure the dimensions are right for(size_t i=0; i<nviews; ++i) { CV_Assert(points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points); if (points2d_tmp[i].type() == CV_64F) points2d[i] = points2d_tmp[i]; else points2d_tmp[i].convertTo(points2d[i], CV_64F); CV_Assert(projection_matrices_tmp[i].rows == 3 && projection_matrices_tmp[i].cols == 4); if (projection_matrices_tmp[i].type() == CV_64F) projection_matrices[i] = projection_matrices_tmp[i]; else projection_matrices_tmp[i].convertTo(projection_matrices[i], CV_64F); } } // output _points3d.create(3, n_points, CV_64F); cv::Mat points3d = _points3d.getMat(); // Two view if( nviews == 2 ) { const Mat_<double> &xl = points2d[0], &xr = points2d[1]; const Matx34d & Pl = projection_matrices[0]; // left matrix projection const Matx34d & Pr = projection_matrices[1]; // right matrix projection // triangulate for( unsigned i = 0; i < n_points; ++i ) { Vec3d point3d; triangulateDLT( Vec2d(xl(0,i), xl(1,i)), Vec2d(xr(0,i), xr(1,i)), Pl, Pr, point3d ); for(char j=0; j<3; ++j) points3d.at<double>(j, i) = point3d[j]; } } else if( nviews > 2 ) { // triangulate for( unsigned i=0; i < n_points; ++i ) { // build x matrix (one point per view) Mat_<double> x( 2, nviews ); for( unsigned k=0; k < nviews; ++k ) { points2d.at(k).col(i).copyTo( x.col(k) ); } Vec3d point3d; nViewTriangulate( x, projection_matrices, point3d ); for(char j=0; j<3; ++j) points3d.at<double>(j, i) = point3d[j]; } } }
void FisherFaceRecognizer::train(InputArrayOfArrays _in_src, InputArray _inm_labels, bool preserveData) { if (_in_src.kind() != _InputArray::STD_VECTOR_MAT && _in_src.kind() != _InputArray::STD_VECTOR_VECTOR) { String error_message = "The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< std::vector<...> >)."; CV_Error(CV_StsBadArg, error_message); } if (_in_src.total() == 0) { String error_message = format("Empty training data was given. You'll need more than one sample to learn a model."); CV_Error(CV_StsUnsupportedFormat, error_message); } else if (_inm_labels.getMat().type() != CV_32SC1) { String error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _inm_labels.type()); CV_Error(CV_StsUnsupportedFormat, error_message); } // get the vector of matrices std::vector<Mat> src; _in_src.getMatVector(src); // get the label matrix Mat labels = _inm_labels.getMat(); // check if data is well- aligned if (labels.total() != src.size()) { String error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", src.size(), m_labels.total()); CV_Error(CV_StsBadArg, error_message); } // if this model should be trained without preserving old data, delete old model data if (!preserveData) { m_labels.release(); m_src.clear(); } // append labels to m_labels matrix for (size_t labelIdx = 0; labelIdx < labels.total(); labelIdx++) { m_labels.push_back(labels.at<int>((int)labelIdx)); m_src.push_back(src[(int)labelIdx]); } // observations in row Mat data = asRowMatrix(m_src, CV_64FC1); // number of samples int n = data.rows; /* LDA needs more than one class We have to check the labels first */ bool label_flag = false; for (int i = 1 ; i < m_labels.rows ; i++) { if (m_labels.at<int>(i, 0)!=m_labels.at<int>(i-1, 0)) { label_flag = true; break; } } if (!label_flag) { String error_message = format("The labels should contain more than one types."); CV_Error(CV_StsBadArg, error_message); } // clear existing model data m_projections.clear(); std::vector<int> ll; for (unsigned int i = 0 ; i < m_labels.total() ; i++) { ll.push_back(m_labels.at<int>(i)); } // get the number of unique classes int C = (int) remove_dups(ll).size(); // clip number of components to be valid m_num_components = (C-1); // perform the PCA PCA pca(data, Mat(), PCA::DATA_AS_ROW, (n-C)); LDA lda(pca.project(data),m_labels, m_num_components); // Now calculate the projection matrix as pca.eigenvectors * lda.eigenvectors. // Note: OpenCV stores the eigenvectors by row, so we need to transpose it! gemm(pca.eigenvectors, lda.eigenvectors(), 1.0, Mat(), 0.0, m_eigenvectors, GEMM_1_T); // store the projections of the original data for (int sampleIdx = 0 ; sampleIdx < data.rows ; sampleIdx++) { Mat p = LDA::subspaceProject(m_eigenvectors, m_mean, data.row(sampleIdx)); m_projections.push_back(p); } }