123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using OpenCvSharp.Internal;
- using OpenCvSharp.Internal.Vectors;
- namespace OpenCvSharp;
- static partial class Cv2
- {
- /// <summary>
- /// Loads an image from a file.
- /// </summary>
- /// <param name="fileName">Name of file to be loaded.</param>
- /// <param name="flags">Specifies color type of the loaded image</param>
- /// <returns></returns>
- public static Mat ImRead(string fileName, ImreadModes flags = ImreadModes.Color)
- {
- if (string.IsNullOrEmpty(fileName))
- throw new ArgumentNullException(nameof(fileName));
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imread(fileName, (int) flags, out var ret));
- if (ret == IntPtr.Zero)
- throw new OpenCvSharpException("imread failed.");
- return Mat.FromNativePointer(ret);
- }
- /// <summary>
- /// Loads a multi-page image from a file.
- /// </summary>
- /// <param name="filename">Name of file to be loaded.</param>
- /// <param name="mats">A vector of Mat objects holding each page, if more than one.</param>
- /// <param name="flags">Flag that can take values of @ref cv::ImreadModes, default with IMREAD_ANYCOLOR.</param>
- /// <returns></returns>
- public static bool ImReadMulti(string filename, out Mat[] mats, ImreadModes flags = ImreadModes.AnyColor)
- {
- if (filename is null)
- throw new ArgumentNullException(nameof(filename));
- using var matsVec = new VectorOfMat();
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imreadmulti(filename, matsVec.CvPtr, (int) flags, out var ret));
- mats = matsVec.ToArray();
- return ret != 0;
- }
- /// <summary>
- /// Saves an image to a specified file.
- /// </summary>
- /// <param name="fileName">Name of the file.</param>
- /// <param name="img">Image to be saved.</param>
- /// <param name="prms">Format-specific save parameters encoded as pairs</param>
- /// <returns></returns>
- public static bool ImWrite(string fileName, Mat img, int[]? prms = null)
- {
- if (string.IsNullOrEmpty(fileName))
- throw new ArgumentNullException(nameof(fileName));
- if (img is null)
- throw new ArgumentNullException(nameof(img));
- if (prms is null)
- prms = Array.Empty<int>();
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imwrite(fileName, img.CvPtr, prms, prms.Length, out var ret));
- GC.KeepAlive(img);
- return ret != 0;
- }
- /// <summary>
- /// Saves an image to a specified file.
- /// </summary>
- /// <param name="fileName">Name of the file.</param>
- /// <param name="img">Image to be saved.</param>
- /// <param name="prms">Format-specific save parameters encoded as pairs</param>
- /// <returns></returns>
- public static bool ImWrite(string fileName, Mat img, params ImageEncodingParam[] prms)
- {
- if (prms is null)
- throw new ArgumentNullException(nameof(prms));
- if (prms.Length <= 0)
- return ImWrite(fileName, img);
- var p = new List<int>();
- foreach (var item in prms)
- {
- p.Add((int) item.EncodingId);
- p.Add(item.Value);
- }
- return ImWrite(fileName, img, p.ToArray());
- }
- /// <summary>
- /// Saves an image to a specified file.
- /// </summary>
- /// <param name="fileName">Name of the file.</param>
- /// <param name="img">Image to be saved.</param>
- /// <param name="prms">Format-specific save parameters encoded as pairs</param>
- /// <returns></returns>
- public static bool ImWrite(string fileName, IEnumerable<Mat> img, int[]? prms = null)
- {
- if (string.IsNullOrEmpty(fileName))
- throw new ArgumentNullException(nameof(fileName));
- if (img is null)
- throw new ArgumentNullException(nameof(img));
- prms ??= Array.Empty<int>();
- using var imgVec = new VectorOfMat(img);
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imwrite_multi(fileName, imgVec.CvPtr, prms, prms.Length, out var ret));
- GC.KeepAlive(img);
- return ret != 0;
- }
- /// <summary>
- /// Saves an image to a specified file.
- /// </summary>
- /// <param name="fileName">Name of the file.</param>
- /// <param name="img">Image to be saved.</param>
- /// <param name="prms">Format-specific save parameters encoded as pairs</param>
- /// <returns></returns>
- public static bool ImWrite(string fileName, IEnumerable<Mat> img, params ImageEncodingParam[] prms)
- {
- if (prms is null)
- throw new ArgumentNullException(nameof(prms));
- if (prms.Length <= 0)
- return ImWrite(fileName, img);
- var p = new List<int>();
- foreach (var item in prms)
- {
- p.Add((int)item.EncodingId);
- p.Add(item.Value);
- }
- return ImWrite(fileName, img, p.ToArray());
- }
- /// <summary>
- /// Reads image from the specified buffer in memory.
- /// </summary>
- /// <param name="buf">The input array of vector of bytes.</param>
- /// <param name="flags">The same flags as in imread</param>
- /// <returns></returns>
- public static Mat ImDecode(Mat buf, ImreadModes flags)
- {
- if (buf is null)
- throw new ArgumentNullException(nameof(buf));
- buf.ThrowIfDisposed();
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imdecode_Mat(buf.CvPtr, (int) flags, out var ret));
- GC.KeepAlive(buf);
- return new Mat(ret);
- }
- /// <summary>
- /// Reads image from the specified buffer in memory.
- /// </summary>
- /// <param name="buf">The input array of vector of bytes.</param>
- /// <param name="flags">The same flags as in imread</param>
- /// <returns></returns>
- public static Mat ImDecode(InputArray buf, ImreadModes flags)
- {
- if (buf is null)
- throw new ArgumentNullException(nameof(buf));
- buf.ThrowIfDisposed();
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imdecode_InputArray(buf.CvPtr, (int) flags, out var ret));
- GC.KeepAlive(buf);
- return new Mat(ret);
- }
- /// <summary>
- /// Reads image from the specified buffer in memory.
- /// </summary>
- /// <param name="buf">The input array of vector of bytes.</param>
- /// <param name="flags">The same flags as in imread</param>
- /// <returns></returns>
- public static Mat ImDecode(byte[] buf, ImreadModes flags)
- {
- if (buf is null)
- throw new ArgumentNullException(nameof(buf));
- unsafe
- {
- fixed (byte* pBuf = &buf[0])
- {
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imdecode_vector(pBuf, buf.Length, (int)flags, out var ret));
- return new Mat(ret);
- }
- }
- }
- /// <summary>
- /// Reads image from the specified buffer in memory.
- /// </summary>
- /// <param name="span">The input slice of bytes.</param>
- /// <param name="flags">The same flags as in imread</param>
- /// <returns></returns>
- public static Mat ImDecode(ReadOnlySpan<byte> span, ImreadModes flags)
- {
- if (span.IsEmpty)
- throw new ArgumentException("Empty span", nameof(span));
- unsafe
- {
- fixed (byte* pBuf = span)
- {
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imdecode_vector(pBuf, span.Length, (int) flags, out var ret));
- return new Mat(ret);
- }
- }
- }
- /// <summary>
- /// Compresses the image and stores it in the memory buffer
- /// </summary>
- /// <param name="ext">The file extension that defines the output format</param>
- /// <param name="img">The image to be written</param>
- /// <param name="buf">Output buffer resized to fit the compressed image.</param>
- /// <param name="prms">Format-specific parameters.</param>
- public static bool ImEncode(string ext, InputArray img, out byte[] buf, int[]? prms = null)
- {
- if (string.IsNullOrEmpty(ext))
- throw new ArgumentNullException(nameof(ext));
- if (img is null)
- throw new ArgumentNullException(nameof(img));
- if (prms is null)
- prms = Array.Empty<int>();
- img.ThrowIfDisposed();
- using var bufVec = new VectorOfByte();
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_imencode_vector(ext, img.CvPtr, bufVec.CvPtr, prms, prms.Length, out var ret));
- GC.KeepAlive(img);
- buf = bufVec.ToArray();
- return ret != 0;
- }
- /// <summary>
- /// Compresses the image and stores it in the memory buffer
- /// </summary>
- /// <param name="ext">The file extension that defines the output format</param>
- /// <param name="img">The image to be written</param>
- /// <param name="buf">Output buffer resized to fit the compressed image.</param>
- /// <param name="prms">Format-specific parameters.</param>
- public static void ImEncode(string ext, InputArray img, out byte[] buf, params ImageEncodingParam[] prms)
- {
- if (prms is null)
- throw new ArgumentNullException(nameof(prms));
- var p = new List<int>();
- foreach (var item in prms)
- {
- p.Add((int)item.EncodingId);
- p.Add(item.Value);
- }
- ImEncode(ext, img, out buf, p.ToArray());
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static bool HaveImageReader(string fileName)
- {
- if (fileName is null)
- throw new ArgumentNullException(nameof(fileName));
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_haveImageReader(fileName, out var ret));
- return ret != 0;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static bool HaveImageWriter(string fileName)
- {
- if (fileName is null)
- throw new ArgumentNullException(nameof(fileName));
- NativeMethods.HandleException(
- NativeMethods.imgcodecs_haveImageWriter(fileName, out var ret));
- return ret != 0;
- }
- }
|