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.

99 lines
4.2 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. using Emgu.Util;
  6. namespace Emgu.CV.ML
  7. {
  8. /// <summary>
  9. /// A Normal Bayes Classifier
  10. /// </summary>
  11. public class NormalBayesClassifier : UnmanagedObject, IStatModel
  12. {
  13. private IntPtr _statModelPtr;
  14. private IntPtr _algorithmPtr;
  15. /// <summary>
  16. /// Create a normal Bayes classifier
  17. /// </summary>
  18. public NormalBayesClassifier()
  19. {
  20. _ptr = MlInvoke.CvNormalBayesClassifierDefaultCreate(ref _statModelPtr, ref _algorithmPtr);
  21. }
  22. /*
  23. /// <summary>
  24. /// Create a normal bayes classifier using the specific training data
  25. /// </summary>
  26. /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param>
  27. /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param>
  28. /// <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>
  29. /// <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>
  30. public NormalBayesClassifier(Matrix<float> trainData, Matrix<int> responses, Matrix<Byte> varIdx, Matrix<Byte> sampleIdx)
  31. {
  32. _ptr = MlInvoke.CvNormalBayesClassifierCreate(
  33. trainData.Ptr,
  34. responses.Ptr,
  35. varIdx == null ? IntPtr.Zero : varIdx.Ptr,
  36. sampleIdx == null ? IntPtr.Zero : sampleIdx.Ptr);
  37. }*/
  38. /// <summary>
  39. /// Release the memory associated with this classifier
  40. /// </summary>
  41. protected override void DisposeObject()
  42. {
  43. MlInvoke.CvNormalBayesClassifierRelease(ref _ptr);
  44. _statModelPtr = IntPtr.Zero;
  45. _algorithmPtr = IntPtr.Zero;
  46. }
  47. /*
  48. /// <summary>
  49. /// Train the classifier using the specific data
  50. /// </summary>
  51. /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param>
  52. /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param>
  53. /// <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>
  54. /// <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>
  55. /// <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>
  56. /// <returns>The number of done iterations</returns>
  57. public bool Train(Matrix<float> trainData, Matrix<int> responses, Matrix<Byte> varIdx, Matrix<Byte> sampleIdx, bool update)
  58. {
  59. return MlInvoke.CvNormalBayesClassifierTrain(
  60. _ptr,
  61. trainData.Ptr,
  62. responses.Ptr,
  63. varIdx == null ? IntPtr.Zero : varIdx.Ptr,
  64. sampleIdx == null ? IntPtr.Zero : sampleIdx.Ptr,
  65. update);
  66. }
  67. /// <summary>
  68. /// Given the NormalBayesClassifier, predit the probability of the <paramref name="samples"/>
  69. /// </summary>
  70. /// <param name="samples">The input samples</param>
  71. /// <param name="results">The prediction results, should have the same # of rows as the <paramref name="samples"/></param>
  72. /// <returns>In case of classification the method returns the class label, in case of regression - the output function value</returns>
  73. public float Predict(Matrix<float> samples, Matrix<int> results)
  74. {
  75. return MlInvoke.CvNormalBayesClassifierPredict(
  76. _ptr,
  77. samples.Ptr,
  78. results == null? IntPtr.Zero: results.Ptr);
  79. }*/
  80. IntPtr IStatModel.StatModelPtr
  81. {
  82. get { return _statModelPtr; }
  83. }
  84. IntPtr IAlgorithm.AlgorithmPtr
  85. {
  86. get { return _algorithmPtr; }
  87. }
  88. }
  89. }