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.

121 lines
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. namespace Emgu.CV
  6. {
  7. /// <summary>
  8. /// The kernel that can be use as the parameter of the Convulution function in Image class
  9. /// </summary>
  10. public class ConvolutionKernelF : Matrix<float>
  11. {
  12. /// <summary>
  13. /// The center of the convolution kernel
  14. /// </summary>
  15. protected Point2D<int> _center;
  16. /// <summary>
  17. /// Create a convolution kernel of the specific rows and cols
  18. /// </summary>
  19. /// <param name="rows">The number of raws for the convolution kernel</param>
  20. /// <param name="cols">The number of columns for the convolution kernel</param>
  21. public ConvolutionKernelF(int rows, int cols)
  22. :base(rows, cols)
  23. {
  24. Debug.Assert( ! (rows <= 1 || cols <= 1) );
  25. _center = new Point2D<int>(-1, -1);
  26. }
  27. /// <summary>
  28. /// Create a convolution kernel using the specific matrix and center
  29. /// </summary>
  30. /// <param name="kernel">the values for the convolution kernel</param>
  31. /// <param name="center">the center of the kernel</param>
  32. public ConvolutionKernelF(Matrix<float> kernel, Point2D<int> center)
  33. : this(kernel.Data, center)
  34. {
  35. }
  36. /// <summary>
  37. /// Create a convolution kernel using the specific floating point matrix
  38. /// </summary>
  39. /// <param name="kernel">the values for the convolution kernel</param>
  40. public ConvolutionKernelF(float[,] kernel)
  41. : this(kernel, new Point2D<int>(-1, -1))
  42. {
  43. }
  44. /// <summary>
  45. /// Create a convolution kernel using the specific floating point matrix and center
  46. /// </summary>
  47. /// <param name="kernel">the values for the convolution kernel</param>
  48. /// <param name="center">the center for the convolution kernel</param>
  49. public ConvolutionKernelF(float[,] kernel, Point2D<int> center)
  50. : base( Math.Max(kernel.GetLength(0), 2), Math.Max(kernel.GetLength(1), 2))
  51. {
  52. int rows = kernel.GetLength(0);
  53. int cols = kernel.GetLength(1);
  54. Debug.Assert(!(rows == 0 || cols == 0));
  55. /*
  56. if (rows == 1)
  57. {
  58. kernel = new float[2, cols] { kernel[0], new float[cols] };
  59. rows++;
  60. }
  61. if (cols == 1)
  62. {
  63. kernel = System.Array.ConvertAll<float[], float[]>(kernel, delegate(float[] fs) { return new float[2] { fs[0], 0.0f }; });
  64. cols++;
  65. }
  66. Emgu.Utils.CopyMatrix(kernel, CvMat.data);
  67. */
  68. throw new System.Exception("Unimplemented");
  69. _center = center;
  70. }
  71. ///<summary> Return a filpped copy of the convolution kernel</summary>
  72. ///<param name="horizontal">if the kernel to be flipped horizontally</param>
  73. ///<param name="vertical">if the kernel to be flipped vertically</param>
  74. ///<returns> The flipped copy of <i>this</i> image </returns>
  75. public ConvolutionKernelF Flip(bool horizontal, bool vertical)
  76. {
  77. int code = 0;
  78. if (horizontal && !vertical) code = 1;
  79. else if (!horizontal && vertical) code = 0;
  80. else if (horizontal && vertical) code = -1;
  81. else
  82. {
  83. throw new Emgu.Exception(
  84. Emgu.ExceptionHeader.CriticalException,
  85. "Must Flip in at least one of the dimension");
  86. }
  87. ConvolutionKernelF res = new ConvolutionKernelF(Height, Width);
  88. CvInvoke.cvFlip(Ptr, res.Ptr, code);
  89. res.Center.X = ( Center.X == -1 ? -1 : ( horizontal ? Width - Center.X -1 : Center.X) );
  90. res.Center.Y = ( Center.Y == -1 ? -1 : ( vertical ? Height - Center.Y -1 : Center.Y));
  91. return res;
  92. }
  93. /// <summary>
  94. /// The center of the convolution kernel
  95. /// </summary>
  96. public Point2D<int> Center { get { return _center; } }
  97. /// <summary>
  98. /// Obtain the transpose of the convolution kernel
  99. /// </summary>
  100. /// <returns></returns>
  101. public new ConvolutionKernelF Transpose()
  102. {
  103. return new ConvolutionKernelF(
  104. base.Transpose(),
  105. new Point2D<int>(_center.Y, _center.X));
  106. }
  107. }
  108. }