コード例 #1
0
    /// <summary>
    /// Gets a Texture2D version of the 2D mask that displays which pixels within the bounding box a detected object occupies.
    /// Texture is the size of the 2D bounding box and meant to be overlayed on top of it.
    /// </summary>
    /// <param name="masktex">Texture2D output - set this to the Texture2D object you want to be the mask.</param>
    /// <param name="fliponYaxis">True to flip the image on the Y axis, since it comes from the ZED upside-down relative to Unity coords.\r\n
    /// Note: It is faster to invert the Y UV value in the shader that displays it, since the bytes must be flipped in a for loop otherwise. </param>
    /// <returns>True if texture was successfully retrieved; false otherwise.</returns>
    public bool GetMaskTexture(out Texture2D masktex, bool fliponYaxis)
    {
        if (!fliponYaxis)
        {
            if (maskTexture == null)
            {
                IntPtr maskpointer = maskMat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);
                if (maskpointer != IntPtr.Zero)
                {
                    maskTexture = ZEDMatToTexture_CPU(maskMat, false);
                }
            }
        }
        else
        {
            if (maskTextureFlipped == null)
            {
                IntPtr maskpointer = maskMat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);
                if (maskpointer != IntPtr.Zero)
                {
                    maskTextureFlipped = ZEDMatToTexture_CPU(maskMat, true);
                }
            }
        }

        masktex = fliponYaxis ? maskTextureFlipped : maskTexture;
        return(masktex != null);
    }
コード例 #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);
        }
    }