Cv2_cuda.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #if ENABLED_CUDA
  2. using System;
  3. using System.Collections.Generic;
  4. using OpenCvSharp.Cuda;
  5. namespace OpenCvSharp
  6. {
  7. static partial class Cv2
  8. {
  9. #region Hardware
  10. /// <summary>
  11. /// Returns the number of installed CUDA-enabled devices.
  12. /// Use this function before any other GPU functions calls.
  13. /// If OpenCV is compiled without GPU support, this function returns 0.
  14. /// </summary>
  15. /// <returns></returns>
  16. public static int GetCudaEnabledDeviceCount()
  17. {
  18. return NativeMethods.cuda_getCudaEnabledDeviceCount();
  19. }
  20. /// <summary>
  21. /// Returns the current device index set by SetDevice() or initialized by default.
  22. /// </summary>
  23. /// <returns></returns>
  24. public static int GetDevice()
  25. {
  26. ThrowIfGpuNotAvailable();
  27. return NativeMethods.cuda_getDevice();
  28. }
  29. /// <summary>
  30. /// Sets a device and initializes it for the current thread.
  31. /// </summary>
  32. /// <param name="device">System index of a GPU device starting with 0.</param>
  33. /// <returns></returns>
  34. public static int SetDevice(int device)
  35. {
  36. ThrowIfGpuNotAvailable();
  37. return NativeMethods.cuda_getDevice();
  38. }
  39. /// <summary>
  40. /// Explicitly destroys and cleans up all resources associated with the current device in the current process.
  41. /// Any subsequent API call to this device will reinitialize the device.
  42. /// </summary>
  43. public static void ResetDevice()
  44. {
  45. ThrowIfGpuNotAvailable();
  46. NativeMethods.cuda_resetDevice();
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="device"></param>
  52. public static void PrintCudaDeviceInfo(int device)
  53. {
  54. ThrowIfGpuNotAvailable();
  55. NativeMethods.cuda_printCudaDeviceInfo(device);
  56. }
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="device"></param>
  61. public static void PrintShortCudaDeviceInfo(int device)
  62. {
  63. ThrowIfGpuNotAvailable();
  64. NativeMethods.cuda_printShortCudaDeviceInfo(device);
  65. }
  66. #endregion
  67. #region CudaMem
  68. /// <summary>
  69. /// Page-locks the matrix m memory and maps it for the device(s)
  70. /// </summary>
  71. /// <param name="m"></param>
  72. public static void RegisterPageLocked(Mat m)
  73. {
  74. ThrowIfGpuNotAvailable();
  75. if (m is null)
  76. throw new ArgumentNullException(nameof(m));
  77. NativeMethods.cuda_registerPageLocked(m.CvPtr);
  78. GC.KeepAlive(m);
  79. }
  80. /// <summary>
  81. /// Unmaps the memory of matrix m, and makes it pageable again.
  82. /// </summary>
  83. /// <param name="m"></param>
  84. public static void UnregisterPageLocked(Mat m)
  85. {
  86. ThrowIfGpuNotAvailable();
  87. if (m is null)
  88. throw new ArgumentNullException(nameof(m));
  89. NativeMethods.cuda_unregisterPageLocked(m.CvPtr);
  90. GC.KeepAlive(m);
  91. }
  92. #endregion
  93. #region GpuMat
  94. /// <summary>
  95. /// Creates continuous GPU matrix
  96. /// </summary>
  97. /// <param name="rows">Number of rows in a 2D array.</param>
  98. /// <param name="cols">Number of columns in a 2D array.</param>
  99. /// <param name="type">Array type.</param>
  100. /// <param name="m"></param>
  101. public static void CreateContinuous(int rows, int cols, MatType type, GpuMat m)
  102. {
  103. ThrowIfGpuNotAvailable();
  104. if (m is null)
  105. throw new ArgumentNullException(nameof(m));
  106. NativeMethods.cuda_createContinuous1(rows, cols, type, m.CvPtr);
  107. GC.KeepAlive(m);
  108. }
  109. /// <summary>
  110. /// Creates continuous GPU matrix
  111. /// </summary>
  112. /// <param name="rows">Number of rows in a 2D array.</param>
  113. /// <param name="cols">Number of columns in a 2D array.</param>
  114. /// <param name="type">Array type.</param>
  115. /// <returns></returns>
  116. public static GpuMat CreateContinuous(int rows, int cols, MatType type)
  117. {
  118. ThrowIfGpuNotAvailable();
  119. IntPtr ret = NativeMethods.cuda_createContinuous2(rows, cols, type);
  120. return new GpuMat(ret);
  121. }
  122. /// <summary>
  123. /// Creates continuous GPU matrix
  124. /// </summary>
  125. /// <param name="size">Number of rows and columns in a 2D array.</param>
  126. /// <param name="type">Array type.</param>
  127. /// <param name="m"></param>
  128. public static void CreateContinuous(Size size, MatType type, GpuMat m)
  129. {
  130. ThrowIfGpuNotAvailable();
  131. CreateContinuous(size.Height, size.Width, type, m);
  132. }
  133. /// <summary>
  134. /// Creates continuous GPU matrix
  135. /// </summary>
  136. /// <param name="size">Number of rows and columns in a 2D array.</param>
  137. /// <param name="type">Array type.</param>
  138. /// <returns></returns>
  139. public static GpuMat CreateContinuous(Size size, MatType type)
  140. {
  141. ThrowIfGpuNotAvailable();
  142. return CreateContinuous(size.Height, size.Width, type);
  143. }
  144. /// <summary>
  145. /// Ensures that size of the given matrix is not less than (rows, cols) size
  146. /// and matrix type is match specified one too
  147. /// </summary>
  148. /// <param name="rows">Number of rows in a 2D array.</param>
  149. /// <param name="cols">Number of columns in a 2D array.</param>
  150. /// <param name="type">Array type.</param>
  151. /// <param name="m"></param>
  152. public static void EnsureSizeIsEnough(int rows, int cols, MatType type, GpuMat m)
  153. {
  154. ThrowIfGpuNotAvailable();
  155. if (m is null)
  156. throw new ArgumentNullException(nameof(m));
  157. NativeMethods.cuda_ensureSizeIsEnough(rows, cols, type, m.CvPtr);
  158. GC.KeepAlive(m);
  159. }
  160. /// <summary>
  161. /// Ensures that size of the given matrix is not less than (rows, cols) size
  162. /// and matrix type is match specified one too
  163. /// </summary>
  164. /// <param name="size">Number of rows and columns in a 2D array.</param>
  165. /// <param name="type">Array type.</param>
  166. /// <param name="m"></param>
  167. public static void EnsureSizeIsEnough(Size size, MatType type, GpuMat m)
  168. {
  169. ThrowIfGpuNotAvailable();
  170. EnsureSizeIsEnough(size.Height, size.Width, type, m);
  171. }
  172. #endregion
  173. /// <summary>
  174. ///
  175. /// </summary>
  176. public static void ThrowIfGpuNotAvailable()
  177. {
  178. if (GetCudaEnabledDeviceCount() < 1)
  179. throw new OpenCvSharpException("GPU module cannot be used.");
  180. }
  181. }
  182. }
  183. #endif