# Zed\_unity raw image issue

**URL:** <https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791>\
**Category:** C#\
**Tags:** unity\
**Created:** [June 12, 2023, 5:26am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791 "2023-06-12T05:26:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 12, 2023, 5:26am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/1 "2023-06-12T05:26:46Z")

</div>

“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();
}

```

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [June 12, 2023, 6:49am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/2 "2023-06-12T06:49:08Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 12, 2023, 7:17am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/3 "2023-06-12T07:17:20Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [June 12, 2023, 9:49am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/4 "2023-06-12T09:49:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 12, 2023, 10:48am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/5 "2023-06-12T10:48:10Z")

</div>

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](http://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();
}

```

}  
}  
}

---

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 12, 2023, 10:54am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/6 "2023-06-12T10:54:38Z")

</div>

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](http://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();
}

```

}  
}  
}

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [June 12, 2023, 11:52am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/7 "2023-06-12T11:52:12Z")

</div>

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](https://community.stereolabs.com/uploads/short-url/4AWPdLDbsNDSKUptMT4v7PMxOSi.cs) (2.5 KB)

---

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 12, 2023, 12:13pm UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/8 "2023-06-12T12:13:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [June 12, 2023, 12:29pm UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/9 "2023-06-12T12:29:36Z")

</div>

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

```auto
   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);
        }
    }

```

---

<div class="post-metadata">

**Author:** ![gauravkhanduri](https://community.stereolabs.com/user_avatar/community.stereolabs.com/gauravkhanduri/32/1370_2.png) [@gauravkhanduri](https://community.stereolabs.com/u/gauravkhanduri)\
**Post date:** [June 13, 2023, 9:50am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/10 "2023-06-13T09:50:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [June 13, 2023, 9:58am UTC](https://community.stereolabs.com/t/zed-unity-raw-image-issue/2791/11 "2023-06-13T09:58:54Z")

</div>

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](https://docs.unity3d.com/ScriptReference/Canvas.html)

Stereolabs Support
