/// <summary>
    /// Creates an OpenCV version of a ZEDMat.
    /// In this sample, we only ever use 8U_C1 (for grayscale image) but you can call it yourself
    /// for any ZEDMat type and get a properly-formatted OpenCV Mat.
    /// </summary>
    /// <param name="zedmat">Source ZEDMat.</param>
    /// <param name="zedmattype">Type of ZEDMat - data type and channel number. Depends on the type of image
    /// it represents. See summaries of each enum value to choose the type, as you can't currently
    /// retrieve the material type from an instantiated ZEDMat.</param>
    /// <returns>OpenCV Mat formatted correctly so that you can copy data from the source ZEDMat into it.</returns>
    private static Mat SLMat2CVMat(sl.ZEDMat zedmat, ZEDMat.MAT_TYPE zedmattype)
    {
        int cvmattype = SLMatType2CVMatType(zedmattype);
        Mat cvmat     = new Mat(zedmat.GetHeight(), zedmat.GetWidth(), cvmattype);

        return(cvmat);
    }
    /// <summary>
    /// Returns the OpenCV type that corresponds to a given ZED Mat type.
    /// </summary>
    private static int SLMatType2CVMatType(ZEDMat.MAT_TYPE zedmattype)
    {
        switch (zedmattype)
        {
        case ZEDMat.MAT_TYPE.MAT_32F_C1:
            return(CvType.CV_32FC1);

        case ZEDMat.MAT_TYPE.MAT_32F_C2:
            return(CvType.CV_32FC2);

        case ZEDMat.MAT_TYPE.MAT_32F_C3:
            return(CvType.CV_32FC3);

        case ZEDMat.MAT_TYPE.MAT_32F_C4:
            return(CvType.CV_32FC4);

        case ZEDMat.MAT_TYPE.MAT_8U_C1:
            return(CvType.CV_8UC1);

        case ZEDMat.MAT_TYPE.MAT_8U_C2:
            return(CvType.CV_8UC2);

        case ZEDMat.MAT_TYPE.MAT_8U_C3:
            return(CvType.CV_8UC3);

        case ZEDMat.MAT_TYPE.MAT_8U_C4:
            return(CvType.CV_8UC4);

        default:
            return(-1);
        }
    }
    /// <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);
        }
    }