SVMExample.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. {
  42. Mat sampleMat = new Mat(1, 2, CvType.CV_32FC1);
  43. sampleMat.put(0, 0, j, i);
  44. float response = svm.predict(sampleMat);
  45. if (response == 1)
  46. image.put(i, j, green);
  47. else if (response == -1)
  48. image.put(i, j, blue);
  49. }
  50. // Show the training data
  51. int thickness = -1;
  52. int lineType = 8;
  53. Imgproc.circle(image, new Point(501, 10), 5, new Scalar(0, 0, 0, 255), thickness, lineType, 0);
  54. Imgproc.circle(image, new Point(255, 10), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0);
  55. Imgproc.circle(image, new Point(501, 255), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0);
  56. Imgproc.circle(image, new Point(10, 501), 5, new Scalar(255, 255, 255, 255), thickness, lineType, 0);
  57. // Show support vectors
  58. thickness = 2;
  59. lineType = 8;
  60. Mat sv = svm.getUncompressedSupportVectors();
  61. //Debug.Log ("sv.ToString() " + sv.ToString ());
  62. //Debug.Log ("sv.dump() " + sv.dump ());
  63. for (int i = 0; i < sv.rows(); ++i)
  64. {
  65. 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);
  66. }
  67. Texture2D texture = new Texture2D(image.width(), image.height(), TextureFormat.RGBA32, false);
  68. Utils.matToTexture2D(image, texture);
  69. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  70. }
  71. // Update is called once per frame
  72. void Update()
  73. {
  74. }
  75. /// <summary>
  76. /// Raises the back button click event.
  77. /// </summary>
  78. public void OnBackButtonClick()
  79. {
  80. SceneManager.LoadScene("OpenCVForUnityExample");
  81. }
  82. }
  83. }