DrawingExample.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. using OpenCVForUnity.ImgprocModule;
  5. using OpenCVForUnity.CoreModule;
  6. using OpenCVForUnity.UnityUtils;
  7. namespace OpenCVForUnityExample
  8. {
  9. /// <summary>
  10. /// Drawing Example
  11. /// An example of drawing to an image using the Imgproc class.
  12. /// </summary>
  13. public class DrawingExample : MonoBehaviour
  14. {
  15. // Use this for initialization
  16. void Start()
  17. {
  18. Texture2D imgTexture = Resources.Load("chessboard") as Texture2D;
  19. Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3);
  20. Utils.texture2DToMat(imgTexture, imgMat);
  21. Debug.Log("imgMat.ToString() " + imgMat.ToString());
  22. Imgproc.line(imgMat, new Point(50, 50), new Point(400, 105), new Scalar(0, 0, 200), 3);
  23. Imgproc.rectangle(imgMat, new Point(150, 200), new Point(300, 300), new Scalar(0, 200, 0), 5);
  24. Imgproc.circle(imgMat, new Point(500, 300), 80, new Scalar(200, 0, 0), 1);
  25. Imgproc.arrowedLine(imgMat, new Point(100, 500), new Point(550, 350), new Scalar(255, 255, 0), 4, Imgproc.LINE_8, 0, 0.1);
  26. double angle = 100;
  27. Imgproc.ellipse(imgMat, new Point(200, 400), new Size(80, 150), angle, angle - 200, angle + 100, new Scalar(255, 255, 255), -1);
  28. int[] face = {Imgproc.FONT_HERSHEY_SIMPLEX, Imgproc.FONT_HERSHEY_PLAIN, Imgproc.FONT_HERSHEY_DUPLEX, Imgproc.FONT_HERSHEY_COMPLEX,
  29. Imgproc.FONT_HERSHEY_TRIPLEX, Imgproc.FONT_HERSHEY_COMPLEX_SMALL, Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX,
  30. Imgproc.FONT_HERSHEY_SCRIPT_COMPLEX, Imgproc.FONT_ITALIC
  31. };
  32. Imgproc.putText(imgMat, "OpenCV", new Point(50, 50), face[0], 1.2, new Scalar(0, 0, 200), 2, Imgproc.LINE_AA, false);
  33. Imgproc.putText(imgMat, "OpenCV", new Point(50, 100), face[1], 1.2, new Scalar(0, 200, 0), 2, Imgproc.LINE_AA, false);
  34. Imgproc.putText(imgMat, "OpenCV", new Point(50, 150), face[2], 1.2, new Scalar(200, 0, 0), 2, Imgproc.LINE_AA, false);
  35. Imgproc.putText(imgMat, "OpenCV", new Point(50, 200), face[3], 1.2, new Scalar(0, 100, 100), 2, Imgproc.LINE_AA, false);
  36. Imgproc.putText(imgMat, "OpenCV", new Point(50, 250), face[4], 1.2, new Scalar(100, 100, 0), 2, Imgproc.LINE_AA, false);
  37. Imgproc.putText(imgMat, "OpenCV", new Point(50, 300), face[5], 1.2, new Scalar(100, 0, 100), 2, Imgproc.LINE_AA, false);
  38. Imgproc.putText(imgMat, "OpenCV", new Point(50, 350), face[6], 1.2, new Scalar(100, 100, 100), 2, Imgproc.LINE_AA, false);
  39. Imgproc.putText(imgMat, "OpenCV", new Point(50, 400), face[7], 1.2, new Scalar(100, 100, 200), 2, Imgproc.LINE_AA, false);
  40. Imgproc.putText(imgMat, "OpenCV", new Point(300, 50), face[0] | face[8], 1.2, new Scalar(100, 200, 100), 2, Imgproc.LINE_AA, false);
  41. Imgproc.putText(imgMat, "OpenCV", new Point(300, 100), face[1] | face[8], 1.2, new Scalar(200, 100, 100), 2, Imgproc.LINE_AA, false);
  42. Imgproc.putText(imgMat, "OpenCV", new Point(300, 150), face[2] | face[8], 1.2, new Scalar(200, 200, 100), 2, Imgproc.LINE_AA, false);
  43. Imgproc.putText(imgMat, "OpenCV", new Point(300, 200), face[3] | face[8], 1.2, new Scalar(200, 100, 200), 2, Imgproc.LINE_AA, false);
  44. Imgproc.putText(imgMat, "OpenCV", new Point(300, 250), face[4] | face[8], 1.2, new Scalar(100, 200, 200), 2, Imgproc.LINE_AA, false);
  45. Imgproc.putText(imgMat, "OpenCV", new Point(300, 300), face[5] | face[8], 1.2, new Scalar(100, 200, 255), 2, Imgproc.LINE_AA, false);
  46. Imgproc.putText(imgMat, "OpenCV", new Point(300, 350), face[6] | face[8], 1.2, new Scalar(100, 255, 200), 2, Imgproc.LINE_AA, false);
  47. Imgproc.putText(imgMat, "OpenCV", new Point(300, 400), face[7] | face[8], 1.2, new Scalar(255, 200, 100), 2, Imgproc.LINE_AA, false);
  48. Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
  49. Utils.matToTexture2D(imgMat, texture);
  50. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  51. }
  52. // Update is called once per frame
  53. void Update()
  54. {
  55. }
  56. /// <summary>
  57. /// Raises the back button click event.
  58. /// </summary>
  59. public void OnBackButtonClick()
  60. {
  61. SceneManager.LoadScene("OpenCVForUnityExample");
  62. }
  63. }
  64. }