/// <summary>
    /// Copies the given ZEDMat to a given OpenCV mat, creating either or both mats if necessary, then calls an ImageUpdatedEvent with them.
    /// Used in OnZEDGrabbed to call different events, and to make it easy to add more kinds of images/events by just adding more calls to this method.
    /// </summary>
    /// <param name="cam">Unity Camera object that represents the ZED camera. Usually from ZEDManager.GetLeftCamera() or ZEDManager.GetRightCamera().</param>
    /// <param name="zedmat">ZEDMat used to get the ZED image. Passing an empty one is okay - it'll get filled appropriately.</param>
    /// <param name="view">Type of image requested, like LEFT or LEFT_GRAY.</param>
    /// <param name="mattype">Data type and channel of required ZEDMat. See summaries over each enum entry to know which is correct for your image type.</param>
    /// <param name="cvMat">OpenCV mat to copy to. Passing an empty one is okay - it'll get filled appropriately.</param>
    /// <param name="updateevent">Event to call if the method retrieves the image successfully.</param>
    private void DeployGrabbedEvent(Camera cam, ref ZEDMat zedmat, VIEW view, ZEDMat.MAT_TYPE mattype, ref Mat cvMat, ImageUpdatedEvent updateevent,
                                    OpenCVConversion conversionatend = OpenCVConversion.NONE)
    {
        if (zedmat == null)
        {
            zedmat = new ZEDMat((uint)zedCam.ImageWidth, (uint)zedCam.ImageHeight, mattype);
        }

        /*if (cvMat == null)
         * {
         *  cvMat = SLMat2CVMat(zedmat, mattype);
         * }*/

        ERROR_CODE err = zedManager.zedCamera.RetrieveImage(zedmat, view, ZEDMat.MEM.MEM_CPU, zedmat.GetResolution());

        if (err == ERROR_CODE.SUCCESS)
        {
            Mat buffermat = GetOpenCVBufferMat(zedCam.ImageHeight, zedCam.ImageWidth, SLMatType2CVMatType(mattype));

            //copyToMat(zedmat.GetPtr(), cvMat);
            Utils.copyToMat(zedmat.GetPtr(), buffermat);

            ConvertColorSpace(buffermat, ref cvMat, conversionatend);
            //Mat convertedmat = ConvertColorSpace(buffermat, conversionatend);

            //updateevent.Invoke(cam, camMat, cvMat);
            updateevent.Invoke(cam, camMat, cvMat);
        }
    }
    /// <summary>
    /// If we want an output format that differs from the default for ZED's image, here's where we change it.
    /// For example, the SDK provides the left view image in BGRA format. But we may want BGR (no alpha) or RGB.
    /// This gets called in DeployGrabbedEvent.
    private void ConvertColorSpace(Mat source, ref Mat dest, OpenCVConversion converttype)
    {
        switch (converttype)
        {
        case OpenCVConversion.NONE:
        default:
            dest = source;
            break;

        case OpenCVConversion.BGRA2BGR:
            //Mat bgrimage = new Mat(source.rows(), source.cols(), CvType.CV_8UC3, new Scalar(0, 0, 0));
            if (dest == null)
            {
                dest = new Mat(source.rows(), source.cols(), CvType.CV_8UC3, new Scalar(0, 0, 0));
            }
            Imgproc.cvtColor(source, dest, Imgproc.COLOR_BGRA2BGR);
            break;

        case OpenCVConversion.BGRA2RGB:
            if (dest == null)
            {
                dest = new Mat(source.rows(), source.cols(), CvType.CV_8UC3, new Scalar(0, 0, 0));
            }
            Imgproc.cvtColor(source, dest, Imgproc.COLOR_BGRA2RGB);
            break;

        case OpenCVConversion.BGRA2RGBA:
            //Mat rgbaimage = new Mat(source.rows(), source.cols(), CvType.CV_8UC4, new Scalar(0, 0, 0));
            if (dest == null)
            {
                dest = new Mat(source.rows(), source.cols(), CvType.CV_8UC3, new Scalar(0, 0, 0));
            }
            Imgproc.cvtColor(source, dest, Imgproc.COLOR_BGRA2RGBA);
            break;
        }
    }