Zed_unity raw image issue

“Below I have pasted my code, the problem is unity is throwing an error that zedmat constructor could not take 4 arguments please help to find the way around.”

ZEDMat zedmat = new ZEDMat(1280, 720, ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU); //Alternative Matrix Type: MAT_8U_C4
while (err != sl.ERROR_CODE.SUCCESS)
{
err = zedManager.zedCamera.RetrieveImage( VIEW.LEFT_UNRECTIFIED, ZEDMat.MEM.MEM_CPU,);
Debug.Log(err);
}
IntPtr matptr = zedmat.GetPtr(ZEDMat.MEM.MEM_CPU);

int matwidth = zedmat.GetWidth();
int matheight = zedmat.GetHeight();
int len = 1280 * 720 * 4;
byte[] frame_bytes = new byte[len];
Marshal.Copy(matptr, frame_bytes, 0, len); 

if (frame_bytes != null)
{                     
    Texture2D image = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
    image.LoadRawTextureData(frame_bytes);
    byte[] bytes = image.EncodeToJPG();
    Destroy(image);
    File.WriteAllBytes(Application.dataPath + "/../zed.png", bytes);
    Application.Quit();
}

Hi,

To initialize a ZEDMat, you need to use the Create method (see here : https://github.com/stereolabs/zed-unity/blob/master/ZEDCamera/Assets/ZED/SDK/NativeInterface/ZEDMat.cs#L352)

It should look like that:

ZEDMat zedMat = new ZEDMat();zedMat.Create(new sl.Resolution(1280, 720), ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU); //Alternative Matrix Type: MAT_8U_C4

Stereolabs Support

Thank you for the reply, but still it is not working for me.

" ZEDMat zedmat = new ZEDMat();
zedmat.Create(new sl.Resolution(1280,720), ZEDMat.MAT_TYPE.MAT_8U_C4,
ZEDMat.MEM.MEM_CPU);

Could you be more specific about what you are trying to achieve and what error are you facing?

Below is the code full code I am working on, my objective is to get the raw image in unity.
But I am confused between to scripts i.e ZEDCamera.cs, ZEDMat.cs.

It would be really helpful If you can get me through this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.IO;

namespace sl {

public class CaptureImage : ZEDMat
{
public GameObject ZED_Rig_Stereo;
private ZEDManager zedmanager;
sl.ERROR_CODE err = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
sl.ZEDMat zedmat = new sl.ZEDMat();
// Start is called before the first frame update
void Start()
{
zedmanager = ZED_Rig_Stereo.GetComponent();
}

// Update is called once per frame
void Update()
{
    // sl.ZEDMat zedmat = new sl.ZEDMat(1280, 720, ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU); //Alternative Matrix Type: MAT_8U_C4           
while (err != sl.ERROR_CODE.SUCCESS)
{
    err = zedmanager.zedCamera.RetrieveImage(zedmat, VIEW.LEFT_UNRECTIFIED, ZEDMat.MEM.MEM_CPU);
    Debug.Log(err);
}
IntPtr matptr = zedmat.GetPtr(ZEDMat.MEM.MEM_CPU);

int matwidth = zedmat.GetWidth();
int matheight = zedmat.GetHeight();
int len = 1280 * 720 * 4;
byte[] frame_bytes = new byte[len];
Marshal.Copy(matptr, frame_bytes, 0, len); 

if (frame_bytes != null)
{                     
    Texture2D image = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
    image.LoadRawTextureData(frame_bytes);
    byte[] bytes = image.EncodeToPNG();
    // Destroy(image);
    File.WriteAllBytes(Application.dataPath + "/../zed.png", bytes);
    Application.Quit();
}

}
}
}

Hi BemjaminV
This is the updated one,

error is “The name ‘zedmat’ does not exist in the current context”

sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.IO;
using sl;

namespace sl{ //ZEDMat is a wrapper for sl.Mat{

public class CaptureImage : ZEDCamera
{
public GameObject ZED_Rig_Stereo;
private ZEDManager zedmanager;
sl.ERROR_CODE err = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
// sl.ZEDMat zedmat = new sl.ZEDMat();
// Start is called before the first frame update
void Start()
{
zedmanager = ZED_Rig_Stereo.GetComponent();
}

// Update is called once per frame
void Update()
{
    ZEDMat zedMat = new ZEDMat();
    zedMat.Create(new sl.Resolution(1280, 720), ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU); //Alternative Matrix Type: MAT_8U_C4
    // sl.ZEDMat zedmat = new sl.ZEDMat(1280, 720, ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU); //Alternative Matrix Type: MAT_8U_C4           
while (err != sl.ERROR_CODE.SUCCESS)
{
    err = zedmanager.zedCamera.RetrieveImage(zedmat, VIEW.LEFT_UNRECTIFIED, ZEDMat.MEM.MEM_CPU);
    Debug.Log(err);
}
IntPtr matptr = zedmat.GetPtr(ZEDMat.MEM.MEM_CPU);

int matwidth = zedmat.GetWidth();
int matheight = zedmat.GetHeight();
int len = 1280 * 720 * 4;
byte[] frame_bytes = new byte[len];
Marshal.Copy(matptr, frame_bytes, 0, len); 

if (frame_bytes != null)
{                     
    Texture2D image = new Texture2D(1280, 720, TextureFormat.RGBA32, false);
    image.LoadRawTextureData(frame_bytes);
    byte[] bytes = image.EncodeToPNG();
    // Destroy(image);
    File.WriteAllBytes(Application.dataPath + "/../zed.png", bytes);
    Application.Quit();
}

}
}
}

Here is a fixed version of your script. It saves the image when the V key of your keyboard is pressed.
This script now inherits from MonoBehavior, therefore you need to add it in the scene, on an empty GameObject for example.

CaptureImage.cs (2.5 KB)

Thank you very much BenjaminV, now it is working nicely.

1 Like

For anyone wondering how to save a image retrieved from the ZED SDK in Unity, here is an example:

   public class CaptureImage : MonoBehaviour
    {
        public GameObject ZED_Rig_Stereo;
        private ZEDManager zedmanager;
        sl.ERROR_CODE err = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
        sl.ZEDMat zedmat;


        public void Awake()
        {
            zedmat = new sl.ZEDMat();
            zedmat.Create(new sl.Resolution(1280, 720), ZEDMat.MAT_TYPE.MAT_8U_C4, ZEDMat.MEM.MEM_CPU);
            if (!zedmanager)
            {
                zedmanager = FindObjectOfType<ZEDManager>();
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.V))
            {
                err = zedmanager.zedCamera.RetrieveImage(zedmat, VIEW.LEFT_UNRECTIFIED, ZEDMat.MEM.MEM_CPU);
                SaveImagePng(zedmat);
            }
        }

        void SaveImagePng(sl.ZEDMat zedmat)
        {
            int matwidth = zedmat.GetWidth();
            int matheight = zedmat.GetHeight();
            IntPtr matptr = zedmat.GetPtr(ZEDMat.MEM.MEM_CPU);
            byte[] matbytes = new byte[matwidth * matheight * 4];
            Marshal.Copy(matptr, matbytes, 0, matbytes.Length);

            //Flip the image vertically
            //Note that this could be done straight from the pointer (without matbytes) but doing it this way for clarity. 
            byte[] flippedbytes = new byte[matwidth * matheight * 4];
            int steplength = matwidth * 4; //Also retrievable from ZEDMat.GetStepBytes();
            for (int i = 0; i < matbytes.Length; i += steplength)
            {
                //Array.Copy(matbytes, i, flippedbytes, flippedbytes.Length - i - steplength, steplength);
            }

            //Create the copy texture and encode it
            Texture2D bgrCopy = new Texture2D(matwidth, matheight, TextureFormat.BGRA32, false); //BRGA32 to account for the ZED's different color space
            bgrCopy.LoadRawTextureData(matbytes);
            bgrCopy.Apply();

            byte[] pngbytes = bgrCopy.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "/../ZEDRGB.png", pngbytes);
        }
    }
1 Like

Hi BenjaminV, can suggest me some code snippets for date and time overlay over the image.
In the same code above mentioned.

Hi,

There is plenty of tutorials and documentation showing how to display text/images on the screen in Unity online.

For example : https://docs.unity3d.com/ScriptReference/Canvas.html

Stereolabs Support

1 Like