//------------------------------------------------------------------------------ // 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; }
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 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); } }
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); } }
void descriptorExtractor::extract(InputArrayOfArrays inputimg, OutputArray feature, String feature_blob) { if (net_ready) { Blob<float>* input_layer = convnet->input_blobs()[0]; input_layer->Reshape(1, num_channels, input_geometry.height, input_geometry.width); /* Forward dimension change to all layers. */ convnet->Reshape(); std::vector<cv::Mat> input_channels; wrapInput(&input_channels); if (inputimg.kind() == 65536) {/* this is a Mat */ Mat img = inputimg.getMat(); preprocess(img, &input_channels); convnet->ForwardPrefilled(); /* Copy the output layer to a std::vector */ Blob<float>* output_layer = convnet->blob_by_name(feature_blob).get(); const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); std::vector<float> featureVec = std::vector<float>(begin, end); cv::Mat feature_mat = cv::Mat(featureVec, true).t(); feature_mat.copyTo(feature); } else {/* This is a vector<Mat> */ vector<Mat> img; inputimg.getMatVector(img); Mat feature_vector; for (unsigned int i = 0; i < img.size(); ++i) { preprocess(img[i], &input_channels); convnet->ForwardPrefilled(); /* Copy the output layer to a std::vector */ Blob<float>* output_layer = convnet->blob_by_name(feature_blob).get(); const float* begin = output_layer->cpu_data(); const float* end = begin + output_layer->channels(); std::vector<float> featureVec = std::vector<float>(begin, end); if (i == 0) { feature_vector = cv::Mat(featureVec, true).t(); int dim_feature = feature_vector.cols; feature_vector.resize(img.size(), dim_feature); } feature_vector.row(i) = cv::Mat(featureVec, true).t(); } feature_vector.copyTo(feature); } } else std::cout << "Device must be set properly using constructor and the net must be set in advance using loadNet."; };
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 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); } }