ImageOptimizationHelper.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. using OpenCVForUnity.CoreModule;
  5. using OpenCVForUnity.ImgprocModule;
  6. namespace OpenCVForUnity.UnityUtils.Helper
  7. {
  8. /// <summary>
  9. /// Image optimization helper.
  10. /// v 1.1.0
  11. /// </summary>
  12. public class ImageOptimizationHelper : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// The downscale ratio.
  16. /// </summary>
  17. [SerializeField, FormerlySerializedAs ("downscaleRatio"), TooltipAttribute ("Set the ratio of down scaling.")]
  18. protected float _downscaleRatio = 2f;
  19. public float downscaleRatio {
  20. get { return _downscaleRatio; }
  21. set { _downscaleRatio = Mathf.Clamp (value, 1f, float.MaxValue); }
  22. }
  23. /// <summary>
  24. /// The frame skipping ratio.
  25. /// </summary>
  26. [SerializeField, FormerlySerializedAs ("frameSkippingRatio"), TooltipAttribute ("Set the ratio of frame skipping.")]
  27. protected int _frameSkippingRatio = 2;
  28. public int frameSkippingRatio {
  29. get { return _frameSkippingRatio; }
  30. set { _frameSkippingRatio = (int)Mathf.Clamp (value, 1f, float.MaxValue); }
  31. }
  32. /// <summary>
  33. /// The frame count.
  34. /// </summary>
  35. protected int frameCount = 0;
  36. /// <summary>
  37. /// The downscale frame mat.
  38. /// </summary>
  39. protected Mat downScaleFrameMat;
  40. protected void OnValidate ()
  41. {
  42. _downscaleRatio = Mathf.Clamp (_downscaleRatio, 1f, float.MaxValue);
  43. _frameSkippingRatio = (int)Mathf.Clamp (_frameSkippingRatio, 1f, float.MaxValue);
  44. }
  45. /// <summary>
  46. /// Indicates whether the current frame is skipped.
  47. /// </summary>
  48. /// <returns><c>true</c>, if the current frame is skipped, <c>false</c> otherwise.</returns>
  49. public virtual bool IsCurrentFrameSkipped ()
  50. {
  51. frameCount++;
  52. if (frameCount % frameSkippingRatio == 0) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. /// <summary>
  58. /// Gets the mat that downscaled the original mat.
  59. /// if downscaleRatio == 1 , return originalMat.
  60. /// </summary>
  61. /// <returns>The downscale mat.</returns>
  62. /// <param name="originalMat">Original mat.</param>
  63. public virtual Mat GetDownScaleMat (Mat originalMat)
  64. {
  65. if (Mathf.Approximately (_downscaleRatio, 1f))
  66. return originalMat;
  67. if (downScaleFrameMat == null) {
  68. downScaleFrameMat = new Mat ();
  69. }
  70. Imgproc.resize (originalMat, downScaleFrameMat, new Size (), 1.0 / _downscaleRatio, 1.0 / _downscaleRatio, Imgproc.INTER_LINEAR);
  71. return downScaleFrameMat;
  72. }
  73. /// <summary>
  74. /// To release the resources for the initialized method.
  75. /// </summary>
  76. public virtual void Dispose ()
  77. {
  78. frameCount = 0;
  79. if (downScaleFrameMat != null) {
  80. downScaleFrameMat.Dispose ();
  81. downScaleFrameMat = null;
  82. }
  83. }
  84. }
  85. }