SVMExample.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.MlModule;
  6. using OpenCVForUnity.ImgprocModule;
  7. using OpenCVForUnity.UnityUtils;
  8. namespace OpenCVForUnityExample
  9. {
  10. /// <summary>
  11. /// SVM Example
  12. /// An example of find a separating straight line using the Support Vector Machines (SVM).
  13. /// Referring to http://docs.opencv.org/3.1.0/d1/d73/tutorial_introduction_to_svm.html#gsc.tab=0.
  14. /// </summary>
  15. public class SVMExample : MonoBehaviour
  16. {
  17. // Use this for initialization
  18. void Start ()
  19. {
  20. // Data for visual representation
  21. int width = 512, height = 512;
  22. Mat image = Mat.zeros (height, width, CvType.CV_8UC4);
  23. // Set up training data
  24. int[] labels = { 1, -1, -1, -1 };
  25. float[] trainingData = { 501, 10, 255, 10, 501, 255, 10, 501 };
  26. Mat trainingDataMat = new Mat (4, 2, CvType.CV_32FC1);
  27. trainingDataMat.put (0, 0, trainingData);
  28. Mat labelsMat = new Mat (4, 1, CvType.CV_32SC1);
  29. labelsMat.put (0, 0, labels);
  30. // Train the SVM
  31. SVM svm = SVM.create ();
  32. svm.setType (SVM.C_SVC);
  33. svm.setKernel (SVM.LINEAR);
  34. svm.setTermCriteria (new TermCriteria (TermCriteria.MAX_ITER, 100, 1e-6));
  35. svm.train (trainingDataMat, Ml.ROW_SAMPLE, labelsMat);
  36. // Show the decision regions given by the SVM
  37. byte[] green = { 0, 255, 0, 255 };
  38. byte[] blue = { 0, 0, 255, 255 };
  39. for (int i = 0; i < image.rows (); ++i)
  40. for (int j = 0; j < image.cols (); ++j) {
  41. Mat sampleMat = new Mat (1, 2, CvType.CV_32FC1);
  42. sampleMat.put (0, 0, j, i);
  43. float response = svm.predict (sampleMat);
  44. if (response == 1)
  45. image.put (i, j, green);
  46. else if (response == -1)
  47. image.put (i, j, blue);
  48. }
  49. // Show the training data
  50. int thickness = -1;
  51. int lineType = 8;
  52. Imgproc.circle (image, new Point (501, 10), 5, new Scalar (0, 0, 0, 255), thickness, lineType, 0);
  53. Imgproc.circle (image, new Point (255, 10), 5, new Scalar (255, 255, 255, 255), thickness, lineType, 0);
  54. Imgproc.circle (image, new Point (501, 255), 5, new Scalar (255, 255, 255, 255), thickness, lineType, 0);
  55. Imgproc.circle (image, new Point (10, 501), 5, new Scalar (255, 255, 255, 255), thickness, lineType, 0);
  56. // Show support vectors
  57. thickness = 2;
  58. lineType = 8;
  59. Mat sv = svm.getUncompressedSupportVectors ();
  60. // Debug.Log ("sv.ToString() " + sv.ToString ());
  61. // Debug.Log ("sv.dump() " + sv.dump ());
  62. for (int i = 0; i < sv.rows (); ++i) {
  63. Imgproc.circle (image, new Point ((int)sv.get (i, 0) [0], (int)sv.get (i, 1) [0]), 6, new Scalar (128, 128, 128, 255), thickness, lineType, 0);
  64. }
  65. Texture2D texture = new Texture2D (image.width (), image.height (), TextureFormat.RGBA32, false);
  66. Utils.matToTexture2D (image, texture);
  67. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  68. }
  69. // Update is called once per frame
  70. void Update ()
  71. {
  72. }
  73. /// <summary>
  74. /// Raises the back button click event.
  75. /// </summary>
  76. public void OnBackButtonClick ()
  77. {
  78. SceneManager.LoadScene ("OpenCVForUnityExample");
  79. }
  80. }
  81. }