private:
int _num_components;
vector<Mat> _projections;
- vector<int> _labels;
+ Mat _labels;
Mat _eigenvectors;
Mat _eigenvalues;
Mat _mean;
// See FaceRecognizer::save.
void save(FileStorage& fs) const;
-
- // Returns the eigenvectors of this PCA.
- Mat eigenvectors() const { return _eigenvectors; }
-
- // Returns the eigenvalues of this PCA.
- Mat eigenvalues() const { return _eigenvalues; }
-
- // Returns the sample mean of this PCA.
- Mat mean() const { return _mean; }
-
- // Returns the number of components used in this PCA.
- int num_components() const { return _num_components; }
+
+ AlgorithmInfo* info() const;
};
-
+
// Belhumeur, P. N., Hespanha, J., and Kriegman, D. "Eigenfaces vs. Fisher-
// faces: Recognition using class specific linear projection.". IEEE
// Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997),
Mat _eigenvalues;
Mat _mean;
vector<Mat> _projections;
- vector<int> _labels;
+ Mat _labels;
public:
using FaceRecognizer::save;
// See FaceRecognizer::save.
virtual void save(FileStorage& fs) const;
- // Returns the eigenvectors of this Fisherfaces model.
- Mat eigenvectors() const { return _eigenvectors; }
-
- // Returns the eigenvalues of this Fisherfaces model.
- Mat eigenvalues() const { return _eigenvalues; }
-
- // Returns the sample mean of this Fisherfaces model.
- Mat mean() const { return _eigenvalues; }
-
- // Returns the number of components used in this Fisherfaces model.
- int num_components() const { return _num_components; }
+ AlgorithmInfo* info() const;
};
// Face Recognition based on Local Binary Patterns.
int _neighbors;
vector<Mat> _histograms;
- vector<int> _labels;
+ Mat _labels;
public:
using FaceRecognizer::save;
int grid_x() const { return _grid_x; }
int grid_y() const { return _grid_y; }
+ AlgorithmInfo* info() const;
};
if(_lbls.getMat().type() != CV_32SC1)
CV_Error(CV_StsUnsupportedFormat, "Labels must be given as integer (CV_32SC1).");
// get labels
- vector<int> labels = _lbls.getMat();
+ Mat labels = _lbls.getMat();
+ CV_Assert( labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1));
// observations in row
Mat data = asRowMatrix(src, CV_64FC1);
// number of samples
// dimensionality of data
//int d = data.cols;
// assert there are as much samples as labels
- if((size_t)n != labels.size())
+ if((size_t)n != labels.total())
CV_Error(CV_StsBadArg, "The number of samples must equal the number of labels!");
// clip number of components to be valid
if((_num_components <= 0) || (_num_components > n))
_mean = pca.mean.reshape(1,1); // store the mean vector
_eigenvalues = pca.eigenvalues.clone(); // eigenvalues by row
transpose(pca.eigenvectors, _eigenvectors); // eigenvectors by column
- _labels = labels; // store labels for prediction
+ labels.copyTo(_labels); // store labels for prediction
// save projections
for(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {
Mat p = subspaceProject(_eigenvectors, _mean, data.row(sampleIdx));
double dist = norm(_projections[sampleIdx], q, NORM_L2);
if(dist < minDist) {
minDist = dist;
- minClass = _labels[sampleIdx];
+ minClass = _labels.at<int>(sampleIdx);
}
}
return minClass;
fs["eigenvectors"] >> _eigenvectors;
// read sequences
readFileNodeList(fs["projections"], _projections);
- readFileNodeList(fs["labels"], _labels);
+ fs["labels"] >> _labels;
}
void Eigenfaces::save(FileStorage& fs) const {
fs << "eigenvectors" << _eigenvectors;
// write sequences
writeFileNodeList(fs, "projections", _projections);
- writeFileNodeList(fs, "labels", _labels);
+ fs << "labels" << _labels;
}
//------------------------------------------------------------------------------
if(_lbls.getMat().type() != CV_32SC1)
CV_Error(CV_StsUnsupportedFormat, "Labels must be given as integer (CV_32SC1).");
// get data
- vector<int> labels = _lbls.getMat();
+ Mat labels = _lbls.getMat();
Mat data = asRowMatrix(src, CV_64FC1);
+
+ CV_Assert( labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1));
+
// dimensionality
int N = data.rows; // number of samples
//int D = data.cols; // dimension of samples
// assert correct data alignment
- if(labels.size() != (size_t)N)
+ if(labels.total() != (size_t)N)
CV_Error(CV_StsUnsupportedFormat, "Labels must be given as integer (CV_32SC1).");
// compute the Fisherfaces
- int C = (int)remove_dups(labels).size(); // number of unique classes
+
+ vector<int> ll;
+ labels.copyTo(ll);
+ int C = (int)remove_dups(ll).size(); // number of unique classes
// clip number of components to be a valid number
if((_num_components <= 0) || (_num_components > (C-1)))
_num_components = (C-1);
// store the total mean vector
_mean = pca.mean.reshape(1,1);
// store labels
- _labels = labels;
+ labels.copyTo(_labels);
// store the eigenvalues of the discriminants
lda.eigenvalues().convertTo(_eigenvalues, CV_64FC1);
// Now calculate the projection matrix as pca.eigenvectors * lda.eigenvectors.
double dist = norm(_projections[sampleIdx], q, NORM_L2);
if(dist < minDist) {
minDist = dist;
- minClass = _labels[sampleIdx];
+ minClass = _labels.at<int>(sampleIdx);
}
}
return minClass;
fs["eigenvectors"] >> _eigenvectors;
// read sequences
readFileNodeList(fs["projections"], _projections);
- readFileNodeList(fs["labels"], _labels);
+ fs["labels"] >> _labels;
}
// See FaceRecognizer::save.
fs << "eigenvectors" << _eigenvectors;
// write sequences
writeFileNodeList(fs, "projections", _projections);
- writeFileNodeList(fs, "labels", _labels);
+ fs << "labels" << _labels;
}
//------------------------------------------------------------------------------
// LBPH
fs["grid_y"] >> _grid_y;
//read matrices
readFileNodeList(fs["histograms"], _histograms);
- readFileNodeList(fs["labels"], _labels);
+ fs["labels"] >> _labels;
}
// See FaceRecognizer::save.
fs << "grid_y" << _grid_y;
// write matrices
writeFileNodeList(fs, "histograms", _histograms);
- writeFileNodeList(fs, "labels", _labels);
+ fs << "labels" << _labels;
}
void LBPH::train(InputArray _src, InputArray _lbls) {
vector<Mat> src;
_src.getMatVector(src);
// turn the label matrix into a vector
- vector<int> labels = _lbls.getMat();
- if(labels.size() != src.size())
+ Mat labels = _lbls.getMat();
+ CV_Assert( labels.type() == CV_32S && (labels.cols == 1 || labels.rows == 1));
+ if(labels.total() != src.size())
CV_Error(CV_StsUnsupportedFormat, "The number of labels must equal the number of samples.");
// store given labels
- _labels = labels;
+ labels.copyTo(_labels);
// store the spatial histograms of the original data
for(size_t sampleIdx = 0; sampleIdx < src.size(); sampleIdx++) {
// calculate lbp image
double dist = compareHist(_histograms[sampleIdx], query, CV_COMP_CHISQR);
if(dist < minDist) {
minDist = dist;
- minClass = _labels[sampleIdx];
+ minClass = _labels.at<int>(sampleIdx);
}
}
return minClass;
{
return new LBPH(radius, neighbors, grid_x, grid_y);
}
+
+CV_INIT_ALGORITHM(Eigenfaces, "FaceRecognizer.Eigenfaces",
+ obj.info()->addParam(obj, "ncomponents", obj._num_components);
+ obj.info()->addParam(obj, "projections", obj._projections, true);
+ obj.info()->addParam(obj, "labels", obj._labels, true);
+ obj.info()->addParam(obj, "eigenvectors", obj._eigenvectors, true);
+ obj.info()->addParam(obj, "eigenvalues", obj._eigenvalues, true);
+ obj.info()->addParam(obj, "mean", obj._mean, true));
+
+CV_INIT_ALGORITHM(Fisherfaces, "FaceRecognizer.Fisherfaces",
+ obj.info()->addParam(obj, "ncomponents", obj._num_components);
+ obj.info()->addParam(obj, "projections", obj._projections, true);
+ obj.info()->addParam(obj, "labels", obj._labels, true);
+ obj.info()->addParam(obj, "eigenvectors", obj._eigenvectors, true);
+ obj.info()->addParam(obj, "eigenvalues", obj._eigenvalues, true);
+ obj.info()->addParam(obj, "mean", obj._mean, true));
+
+CV_INIT_ALGORITHM(LBPH, "FaceRecognizer.LBPH",
+ obj.info()->addParam(obj, "radius", obj._radius);
+ obj.info()->addParam(obj, "neighbors", obj._neighbors);
+ obj.info()->addParam(obj, "grid_x", obj._grid_x);
+ obj.info()->addParam(obj, "grid_y", obj._grid_y);
+ obj.info()->addParam(obj, "histograms", obj._histograms, true);
+ obj.info()->addParam(obj, "labels", obj._labels, true));
+
+bool initModule_contrib()
+{
+ Ptr<Algorithm> efaces = createEigenfaces(), ffaces = createFisherfaces(), lbph = createLBPH();
+ return efaces->info() != 0 && ffaces->info() != 0 && lbph->info() != 0;
+}
}