/// <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);
    }
Beispiel #2
0
    /// <summary>
    /// Converts the given zedMat to an Alpha8 (8-bit single channel) texture.
    /// Assumes you are giving it the ZEDMat from an Object Detection mask, and is therefore 8-bit single channel.
    /// </summary>
    private static Texture2D ZEDMatToTexture_CPU(sl.ZEDMat zedmat, bool flipYcoords = false)
    {
        int width  = zedmat.GetWidth(); //Shorthand.
        int height = zedmat.GetHeight();


        IntPtr maskpointer = zedmat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);

        if (maskpointer != IntPtr.Zero && zedmat.IsInit())
        {
            byte[] texbytes = new byte[zedmat.GetStepBytes() * height];

            System.Runtime.InteropServices.Marshal.Copy(zedmat.GetPtr(sl.ZEDMat.MEM.MEM_CPU), texbytes, 0, texbytes.Length);

            if (flipYcoords)
            {
                byte[] flippedbytes = new byte[texbytes.Length];
                int    steplength   = zedmat.GetWidthBytes();
                for (int i = 0; i < texbytes.Length; i += steplength)
                {
                    Array.Copy(texbytes, i, flippedbytes, flippedbytes.Length - i - steplength, steplength);
                }

                texbytes = flippedbytes;
            }

            Texture2D zedtex = new Texture2D(width, height, TextureFormat.Alpha8, false, false);
            zedtex.anisoLevel = 0;
            zedtex.LoadRawTextureData(texbytes);
            zedtex.Apply(); //Slight bottleneck here - it forces the CPU and GPU to sync.

            return(zedtex);
        }
        else
        {
            Debug.LogError("Pointer to texture was null - returning null.");
            return(null);
        }
    }