facerec.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. // Copyright (c) 2011,2012. Philipp Wagner <bytefish[at]gmx[dot]de>.
  5. // Third party copyrights are property of their respective owners.
  6. #ifndef __OPENCV_FACEREC_HPP__
  7. #define __OPENCV_FACEREC_HPP__
  8. #include "opencv2/face.hpp"
  9. #include "opencv2/core.hpp"
  10. namespace cv { namespace face {
  11. //! @addtogroup face
  12. //! @{
  13. // base for two classes
  14. class CV_EXPORTS_W BasicFaceRecognizer : public FaceRecognizer
  15. {
  16. public:
  17. /** @see setNumComponents */
  18. CV_WRAP int getNumComponents() const;
  19. /** @copybrief getNumComponents @see getNumComponents */
  20. CV_WRAP void setNumComponents(int val);
  21. /** @see setThreshold */
  22. CV_WRAP double getThreshold() const CV_OVERRIDE;
  23. /** @copybrief getThreshold @see getThreshold */
  24. CV_WRAP void setThreshold(double val) CV_OVERRIDE;
  25. CV_WRAP std::vector<cv::Mat> getProjections() const;
  26. CV_WRAP cv::Mat getLabels() const;
  27. CV_WRAP cv::Mat getEigenValues() const;
  28. CV_WRAP cv::Mat getEigenVectors() const;
  29. CV_WRAP cv::Mat getMean() const;
  30. virtual void read(const FileNode& fn) CV_OVERRIDE;
  31. virtual void write(FileStorage& fs) const CV_OVERRIDE;
  32. virtual bool empty() const CV_OVERRIDE;
  33. using FaceRecognizer::read;
  34. using FaceRecognizer::write;
  35. protected:
  36. int _num_components;
  37. double _threshold;
  38. std::vector<Mat> _projections;
  39. Mat _labels;
  40. Mat _eigenvectors;
  41. Mat _eigenvalues;
  42. Mat _mean;
  43. };
  44. class CV_EXPORTS_W EigenFaceRecognizer : public BasicFaceRecognizer
  45. {
  46. public:
  47. /**
  48. @param num_components The number of components (read: Eigenfaces) kept for this Principal
  49. Component Analysis. As a hint: There's no rule how many components (read: Eigenfaces) should be
  50. kept for good reconstruction capabilities. It is based on your input data, so experiment with the
  51. number. Keeping 80 components should almost always be sufficient.
  52. @param threshold The threshold applied in the prediction.
  53. ### Notes:
  54. - Training and prediction must be done on grayscale images, use cvtColor to convert between the
  55. color spaces.
  56. - **THE EIGENFACES METHOD MAKES THE ASSUMPTION, THAT THE TRAINING AND TEST IMAGES ARE OF EQUAL
  57. SIZE.** (caps-lock, because I got so many mails asking for this). You have to make sure your
  58. input data has the correct shape, else a meaningful exception is thrown. Use resize to resize
  59. the images.
  60. - This model does not support updating.
  61. ### Model internal data:
  62. - num_components see EigenFaceRecognizer::create.
  63. - threshold see EigenFaceRecognizer::create.
  64. - eigenvalues The eigenvalues for this Principal Component Analysis (ordered descending).
  65. - eigenvectors The eigenvectors for this Principal Component Analysis (ordered by their
  66. eigenvalue).
  67. - mean The sample mean calculated from the training data.
  68. - projections The projections of the training data.
  69. - labels The threshold applied in the prediction. If the distance to the nearest neighbor is
  70. larger than the threshold, this method returns -1.
  71. */
  72. CV_WRAP static Ptr<EigenFaceRecognizer> create(int num_components = 0, double threshold = DBL_MAX);
  73. };
  74. class CV_EXPORTS_W FisherFaceRecognizer : public BasicFaceRecognizer
  75. {
  76. public:
  77. /**
  78. @param num_components The number of components (read: Fisherfaces) kept for this Linear
  79. Discriminant Analysis with the Fisherfaces criterion. It's useful to keep all components, that
  80. means the number of your classes c (read: subjects, persons you want to recognize). If you leave
  81. this at the default (0) or set it to a value less-equal 0 or greater (c-1), it will be set to the
  82. correct number (c-1) automatically.
  83. @param threshold The threshold applied in the prediction. If the distance to the nearest neighbor
  84. is larger than the threshold, this method returns -1.
  85. ### Notes:
  86. - Training and prediction must be done on grayscale images, use cvtColor to convert between the
  87. color spaces.
  88. - **THE FISHERFACES METHOD MAKES THE ASSUMPTION, THAT THE TRAINING AND TEST IMAGES ARE OF EQUAL
  89. SIZE.** (caps-lock, because I got so many mails asking for this). You have to make sure your
  90. input data has the correct shape, else a meaningful exception is thrown. Use resize to resize
  91. the images.
  92. - This model does not support updating.
  93. ### Model internal data:
  94. - num_components see FisherFaceRecognizer::create.
  95. - threshold see FisherFaceRecognizer::create.
  96. - eigenvalues The eigenvalues for this Linear Discriminant Analysis (ordered descending).
  97. - eigenvectors The eigenvectors for this Linear Discriminant Analysis (ordered by their
  98. eigenvalue).
  99. - mean The sample mean calculated from the training data.
  100. - projections The projections of the training data.
  101. - labels The labels corresponding to the projections.
  102. */
  103. CV_WRAP static Ptr<FisherFaceRecognizer> create(int num_components = 0, double threshold = DBL_MAX);
  104. };
  105. class CV_EXPORTS_W LBPHFaceRecognizer : public FaceRecognizer
  106. {
  107. public:
  108. /** @see setGridX */
  109. CV_WRAP virtual int getGridX() const = 0;
  110. /** @copybrief getGridX @see getGridX */
  111. CV_WRAP virtual void setGridX(int val) = 0;
  112. /** @see setGridY */
  113. CV_WRAP virtual int getGridY() const = 0;
  114. /** @copybrief getGridY @see getGridY */
  115. CV_WRAP virtual void setGridY(int val) = 0;
  116. /** @see setRadius */
  117. CV_WRAP virtual int getRadius() const = 0;
  118. /** @copybrief getRadius @see getRadius */
  119. CV_WRAP virtual void setRadius(int val) = 0;
  120. /** @see setNeighbors */
  121. CV_WRAP virtual int getNeighbors() const = 0;
  122. /** @copybrief getNeighbors @see getNeighbors */
  123. CV_WRAP virtual void setNeighbors(int val) = 0;
  124. /** @see setThreshold */
  125. CV_WRAP virtual double getThreshold() const CV_OVERRIDE = 0;
  126. /** @copybrief getThreshold @see getThreshold */
  127. CV_WRAP virtual void setThreshold(double val) CV_OVERRIDE = 0;
  128. CV_WRAP virtual std::vector<cv::Mat> getHistograms() const = 0;
  129. CV_WRAP virtual cv::Mat getLabels() const = 0;
  130. /**
  131. @param radius The radius used for building the Circular Local Binary Pattern. The greater the
  132. radius, the smoother the image but more spatial information you can get.
  133. @param neighbors The number of sample points to build a Circular Local Binary Pattern from. An
  134. appropriate value is to use `8` sample points. Keep in mind: the more sample points you include,
  135. the higher the computational cost.
  136. @param grid_x The number of cells in the horizontal direction, 8 is a common value used in
  137. publications. The more cells, the finer the grid, the higher the dimensionality of the resulting
  138. feature vector.
  139. @param grid_y The number of cells in the vertical direction, 8 is a common value used in
  140. publications. The more cells, the finer the grid, the higher the dimensionality of the resulting
  141. feature vector.
  142. @param threshold The threshold applied in the prediction. If the distance to the nearest neighbor
  143. is larger than the threshold, this method returns -1.
  144. ### Notes:
  145. - The Circular Local Binary Patterns (used in training and prediction) expect the data given as
  146. grayscale images, use cvtColor to convert between the color spaces.
  147. - This model supports updating.
  148. ### Model internal data:
  149. - radius see LBPHFaceRecognizer::create.
  150. - neighbors see LBPHFaceRecognizer::create.
  151. - grid_x see LLBPHFaceRecognizer::create.
  152. - grid_y see LBPHFaceRecognizer::create.
  153. - threshold see LBPHFaceRecognizer::create.
  154. - histograms Local Binary Patterns Histograms calculated from the given training data (empty if
  155. none was given).
  156. - labels Labels corresponding to the calculated Local Binary Patterns Histograms.
  157. */
  158. CV_WRAP static Ptr<LBPHFaceRecognizer> create(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold = DBL_MAX);
  159. };
  160. //! @}
  161. }} //namespace cv::face
  162. #endif //__OPENCV_FACEREC_HPP__