示例#1
0
        private byte[] Encode([NotNull] string ext, [NotNull] MatOfByte buffer)
        {
            if (ext == null)
            {
                throw new ArgumentNullException(nameof(ext));
            }
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            switch (Format)
            {
            case ColorFormat.RGB:
            {
                Mat bgr = new Mat(Height, Width, CvType.CV_8UC3);
                Imgproc.cvtColor(Mat, bgr, Imgproc.COLOR_RGB2BGR);     // OpenCV uses BGR, Mat is RGB
                Imgcodecs.imencode(ext, bgr, buffer);
                break;
            }

            case ColorFormat.Grayscale:
                Imgcodecs.imencode(ext, Mat, buffer);
                break;

            default:
                throw new NotSupportedException($"Image encoding for {Format} not supported");
            }
            return(buffer.toArray());
        }
    /// <summary>
    /// Converts an image Mat to a Texture2D.
    /// </summary>
    /// <param name="imgDest">Mat that is an image</param>
    /// <returns></returns>
    private static Texture2D MatToTexture2D(Mat imgDest)
    {
        Texture2D texture   = new Texture2D((int)imgDest.size().width, (int)imgDest.size().height);
        var       matOfByte = new MatOfByte();

        Imgcodecs.imencode(".png", imgDest, matOfByte);
        texture.LoadImage(matOfByte.toArray());
        return(texture);
    }