Cv2_imgcodecs.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using OpenCvSharp.Internal;
  2. using OpenCvSharp.Internal.Vectors;
  3. namespace OpenCvSharp;
  4. static partial class Cv2
  5. {
  6. /// <summary>
  7. /// Loads an image from a file.
  8. /// </summary>
  9. /// <param name="fileName">Name of file to be loaded.</param>
  10. /// <param name="flags">Specifies color type of the loaded image</param>
  11. /// <returns></returns>
  12. public static Mat ImRead(string fileName, ImreadModes flags = ImreadModes.Color)
  13. {
  14. if (string.IsNullOrEmpty(fileName))
  15. throw new ArgumentNullException(nameof(fileName));
  16. NativeMethods.HandleException(
  17. NativeMethods.imgcodecs_imread(fileName, (int) flags, out var ret));
  18. if (ret == IntPtr.Zero)
  19. throw new OpenCvSharpException("imread failed.");
  20. return Mat.FromNativePointer(ret);
  21. }
  22. /// <summary>
  23. /// Loads a multi-page image from a file.
  24. /// </summary>
  25. /// <param name="filename">Name of file to be loaded.</param>
  26. /// <param name="mats">A vector of Mat objects holding each page, if more than one.</param>
  27. /// <param name="flags">Flag that can take values of @ref cv::ImreadModes, default with IMREAD_ANYCOLOR.</param>
  28. /// <returns></returns>
  29. public static bool ImReadMulti(string filename, out Mat[] mats, ImreadModes flags = ImreadModes.AnyColor)
  30. {
  31. if (filename is null)
  32. throw new ArgumentNullException(nameof(filename));
  33. using var matsVec = new VectorOfMat();
  34. NativeMethods.HandleException(
  35. NativeMethods.imgcodecs_imreadmulti(filename, matsVec.CvPtr, (int) flags, out var ret));
  36. mats = matsVec.ToArray();
  37. return ret != 0;
  38. }
  39. /// <summary>
  40. /// Saves an image to a specified file.
  41. /// </summary>
  42. /// <param name="fileName">Name of the file.</param>
  43. /// <param name="img">Image to be saved.</param>
  44. /// <param name="prms">Format-specific save parameters encoded as pairs</param>
  45. /// <returns></returns>
  46. public static bool ImWrite(string fileName, Mat img, int[]? prms = null)
  47. {
  48. if (string.IsNullOrEmpty(fileName))
  49. throw new ArgumentNullException(nameof(fileName));
  50. if (img is null)
  51. throw new ArgumentNullException(nameof(img));
  52. if (prms is null)
  53. prms = Array.Empty<int>();
  54. NativeMethods.HandleException(
  55. NativeMethods.imgcodecs_imwrite(fileName, img.CvPtr, prms, prms.Length, out var ret));
  56. GC.KeepAlive(img);
  57. return ret != 0;
  58. }
  59. /// <summary>
  60. /// Saves an image to a specified file.
  61. /// </summary>
  62. /// <param name="fileName">Name of the file.</param>
  63. /// <param name="img">Image to be saved.</param>
  64. /// <param name="prms">Format-specific save parameters encoded as pairs</param>
  65. /// <returns></returns>
  66. public static bool ImWrite(string fileName, Mat img, params ImageEncodingParam[] prms)
  67. {
  68. if (prms is null)
  69. throw new ArgumentNullException(nameof(prms));
  70. if (prms.Length <= 0)
  71. return ImWrite(fileName, img);
  72. var p = new List<int>();
  73. foreach (var item in prms)
  74. {
  75. p.Add((int) item.EncodingId);
  76. p.Add(item.Value);
  77. }
  78. return ImWrite(fileName, img, p.ToArray());
  79. }
  80. /// <summary>
  81. /// Saves an image to a specified file.
  82. /// </summary>
  83. /// <param name="fileName">Name of the file.</param>
  84. /// <param name="img">Image to be saved.</param>
  85. /// <param name="prms">Format-specific save parameters encoded as pairs</param>
  86. /// <returns></returns>
  87. public static bool ImWrite(string fileName, IEnumerable<Mat> img, int[]? prms = null)
  88. {
  89. if (string.IsNullOrEmpty(fileName))
  90. throw new ArgumentNullException(nameof(fileName));
  91. if (img is null)
  92. throw new ArgumentNullException(nameof(img));
  93. prms ??= Array.Empty<int>();
  94. using var imgVec = new VectorOfMat(img);
  95. NativeMethods.HandleException(
  96. NativeMethods.imgcodecs_imwrite_multi(fileName, imgVec.CvPtr, prms, prms.Length, out var ret));
  97. GC.KeepAlive(img);
  98. return ret != 0;
  99. }
  100. /// <summary>
  101. /// Saves an image to a specified file.
  102. /// </summary>
  103. /// <param name="fileName">Name of the file.</param>
  104. /// <param name="img">Image to be saved.</param>
  105. /// <param name="prms">Format-specific save parameters encoded as pairs</param>
  106. /// <returns></returns>
  107. public static bool ImWrite(string fileName, IEnumerable<Mat> img, params ImageEncodingParam[] prms)
  108. {
  109. if (prms is null)
  110. throw new ArgumentNullException(nameof(prms));
  111. if (prms.Length <= 0)
  112. return ImWrite(fileName, img);
  113. var p = new List<int>();
  114. foreach (var item in prms)
  115. {
  116. p.Add((int)item.EncodingId);
  117. p.Add(item.Value);
  118. }
  119. return ImWrite(fileName, img, p.ToArray());
  120. }
  121. /// <summary>
  122. /// Reads image from the specified buffer in memory.
  123. /// </summary>
  124. /// <param name="buf">The input array of vector of bytes.</param>
  125. /// <param name="flags">The same flags as in imread</param>
  126. /// <returns></returns>
  127. public static Mat ImDecode(Mat buf, ImreadModes flags)
  128. {
  129. if (buf is null)
  130. throw new ArgumentNullException(nameof(buf));
  131. buf.ThrowIfDisposed();
  132. NativeMethods.HandleException(
  133. NativeMethods.imgcodecs_imdecode_Mat(buf.CvPtr, (int) flags, out var ret));
  134. GC.KeepAlive(buf);
  135. return new Mat(ret);
  136. }
  137. /// <summary>
  138. /// Reads image from the specified buffer in memory.
  139. /// </summary>
  140. /// <param name="buf">The input array of vector of bytes.</param>
  141. /// <param name="flags">The same flags as in imread</param>
  142. /// <returns></returns>
  143. public static Mat ImDecode(InputArray buf, ImreadModes flags)
  144. {
  145. if (buf is null)
  146. throw new ArgumentNullException(nameof(buf));
  147. buf.ThrowIfDisposed();
  148. NativeMethods.HandleException(
  149. NativeMethods.imgcodecs_imdecode_InputArray(buf.CvPtr, (int) flags, out var ret));
  150. GC.KeepAlive(buf);
  151. return new Mat(ret);
  152. }
  153. /// <summary>
  154. /// Reads image from the specified buffer in memory.
  155. /// </summary>
  156. /// <param name="buf">The input array of vector of bytes.</param>
  157. /// <param name="flags">The same flags as in imread</param>
  158. /// <returns></returns>
  159. public static Mat ImDecode(byte[] buf, ImreadModes flags)
  160. {
  161. if (buf is null)
  162. throw new ArgumentNullException(nameof(buf));
  163. unsafe
  164. {
  165. fixed (byte* pBuf = &buf[0])
  166. {
  167. NativeMethods.HandleException(
  168. NativeMethods.imgcodecs_imdecode_vector(pBuf, buf.Length, (int)flags, out var ret));
  169. return new Mat(ret);
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Reads image from the specified buffer in memory.
  175. /// </summary>
  176. /// <param name="span">The input slice of bytes.</param>
  177. /// <param name="flags">The same flags as in imread</param>
  178. /// <returns></returns>
  179. public static Mat ImDecode(ReadOnlySpan<byte> span, ImreadModes flags)
  180. {
  181. if (span.IsEmpty)
  182. throw new ArgumentException("Empty span", nameof(span));
  183. unsafe
  184. {
  185. fixed (byte* pBuf = span)
  186. {
  187. NativeMethods.HandleException(
  188. NativeMethods.imgcodecs_imdecode_vector(pBuf, span.Length, (int) flags, out var ret));
  189. return new Mat(ret);
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Compresses the image and stores it in the memory buffer
  195. /// </summary>
  196. /// <param name="ext">The file extension that defines the output format</param>
  197. /// <param name="img">The image to be written</param>
  198. /// <param name="buf">Output buffer resized to fit the compressed image.</param>
  199. /// <param name="prms">Format-specific parameters.</param>
  200. public static bool ImEncode(string ext, InputArray img, out byte[] buf, int[]? prms = null)
  201. {
  202. if (string.IsNullOrEmpty(ext))
  203. throw new ArgumentNullException(nameof(ext));
  204. if (img is null)
  205. throw new ArgumentNullException(nameof(img));
  206. if (prms is null)
  207. prms = Array.Empty<int>();
  208. img.ThrowIfDisposed();
  209. using var bufVec = new VectorOfByte();
  210. NativeMethods.HandleException(
  211. NativeMethods.imgcodecs_imencode_vector(ext, img.CvPtr, bufVec.CvPtr, prms, prms.Length, out var ret));
  212. GC.KeepAlive(img);
  213. buf = bufVec.ToArray();
  214. return ret != 0;
  215. }
  216. /// <summary>
  217. /// Compresses the image and stores it in the memory buffer
  218. /// </summary>
  219. /// <param name="ext">The file extension that defines the output format</param>
  220. /// <param name="img">The image to be written</param>
  221. /// <param name="buf">Output buffer resized to fit the compressed image.</param>
  222. /// <param name="prms">Format-specific parameters.</param>
  223. public static void ImEncode(string ext, InputArray img, out byte[] buf, params ImageEncodingParam[] prms)
  224. {
  225. if (prms is null)
  226. throw new ArgumentNullException(nameof(prms));
  227. var p = new List<int>();
  228. foreach (var item in prms)
  229. {
  230. p.Add((int)item.EncodingId);
  231. p.Add(item.Value);
  232. }
  233. ImEncode(ext, img, out buf, p.ToArray());
  234. }
  235. /// <summary>
  236. ///
  237. /// </summary>
  238. /// <param name="fileName"></param>
  239. /// <returns></returns>
  240. public static bool HaveImageReader(string fileName)
  241. {
  242. if (fileName is null)
  243. throw new ArgumentNullException(nameof(fileName));
  244. NativeMethods.HandleException(
  245. NativeMethods.imgcodecs_haveImageReader(fileName, out var ret));
  246. return ret != 0;
  247. }
  248. /// <summary>
  249. ///
  250. /// </summary>
  251. /// <param name="fileName"></param>
  252. /// <returns></returns>
  253. public static bool HaveImageWriter(string fileName)
  254. {
  255. if (fileName is null)
  256. throw new ArgumentNullException(nameof(fileName));
  257. NativeMethods.HandleException(
  258. NativeMethods.imgcodecs_haveImageWriter(fileName, out var ret));
  259. return ret != 0;
  260. }
  261. }