You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.8 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2012 by EMGU. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. namespace Emgu.CV.ML
  6. {
  7. /// <summary>
  8. /// A Normal Bayes Classifier
  9. /// </summary>
  10. public class NormalBayesClassifier : StatModel
  11. {
  12. /// <summary>
  13. /// Create a normal Bayes classifier
  14. /// </summary>
  15. public NormalBayesClassifier()
  16. {
  17. _ptr = MlInvoke.CvNormalBayesClassifierDefaultCreate();
  18. }
  19. /*
  20. /// <summary>
  21. /// Create a normal bayes classifier using the specific training data
  22. /// </summary>
  23. /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param>
  24. /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param>
  25. /// <param name="varIdx">Can be null if not needed. When specified, identifies variables (features) of interest. It is a Matrix&gt;int&lt; of nx1</param>
  26. /// <param name="sampleIdx">Can be null if not needed. When specified, identifies samples of interest. It is a Matrix&gt;int&lt; of nx1</param>
  27. public NormalBayesClassifier(Matrix<float> trainData, Matrix<int> responses, Matrix<Byte> varIdx, Matrix<Byte> sampleIdx)
  28. {
  29. _ptr = MlInvoke.CvNormalBayesClassifierCreate(
  30. trainData.Ptr,
  31. responses.Ptr,
  32. varIdx == null ? IntPtr.Zero : varIdx.Ptr,
  33. sampleIdx == null ? IntPtr.Zero : sampleIdx.Ptr);
  34. }*/
  35. /// <summary>
  36. /// Release the memory associated with this classifier
  37. /// </summary>
  38. protected override void DisposeObject()
  39. {
  40. MlInvoke.CvNormalBayesClassifierRelease(ref _ptr);
  41. }
  42. /// <summary>
  43. /// Train the classifier using the specific data
  44. /// </summary>
  45. /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param>
  46. /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param>
  47. /// <param name="varIdx">Can be null if not needed. When specified, identifies variables (features) of interest. It is a Matrix&gt;int&lt; of nx1</param>
  48. /// <param name="sampleIdx">Can be null if not needed. When specified, identifies samples of interest. It is a Matrix&gt;int&lt; of nx1</param>
  49. /// <param name="update">If true, the training data is used to update the classifier; Otherwise, the data in the classifier are cleared before training is performed</param>
  50. /// <returns>The number of done iterations</returns>
  51. public bool Train(Matrix<float> trainData, Matrix<int> responses, Matrix<Byte> varIdx, Matrix<Byte> sampleIdx, bool update)
  52. {
  53. return MlInvoke.CvNormalBayesClassifierTrain(
  54. _ptr,
  55. trainData.Ptr,
  56. responses.Ptr,
  57. varIdx == null ? IntPtr.Zero : varIdx.Ptr,
  58. sampleIdx == null ? IntPtr.Zero : sampleIdx.Ptr,
  59. update);
  60. }
  61. /// <summary>
  62. /// Given the NormalBayesClassifier, predit the probability of the <paramref name="samples"/>
  63. /// </summary>
  64. /// <param name="samples">The input samples</param>
  65. /// <param name="results">The prediction results, should have the same # of rows as the <paramref name="samples"/></param>
  66. /// <returns>In case of classification the method returns the class label, in case of regression - the output function value</returns>
  67. public float Predict(Matrix<float> samples, Matrix<int> results)
  68. {
  69. return MlInvoke.CvNormalBayesClassifierPredict(
  70. _ptr,
  71. samples.Ptr,
  72. results == null? IntPtr.Zero: results.Ptr);
  73. }
  74. }
  75. }