Unity Crash When Calling zedCamera.Open(ref initParams) in Custom Script

I’m developing a Unity application that integrates the ZED camera using a custom script without the ZED rig prefab. My objective is to capture images from the ZED camera, process them with OpenCV for Unity (applying image warping), and display the result in a RawImage UI component. However, when I include the following code to open the ZED camera, Unity crashes:ERROR_CODE err = zedCamera.Open(ref initParams); if (err != ERROR_CODE.SUCCESS) { Debug.LogError($"Failed to open ZED camera: {err}"); // Handle the error appropriately, such as retrying or exiting the application return; } Debug.Log("ZED camera opened successfully.");

Can you share the code you wrote please?

Hi,
Thanks for your soon reply.
In this script I am trying to apply a simple image processing technique such as image resize. Then, I would like to do perspective projection using image warping.

using UnityEngine;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using sl;
using UnityEngine.UI;

public class ZEDImageProcessing : MonoBehaviour
{
    private ZEDCamera zedCamera;
    private Texture2D zedTexture;
    private Mat zedMat;
    private Mat resizedMat;
    public int targetWidth = 640;
    public int targetHeight = 480;
    public RawImage rawImageComponent;

void Start()
{
    zedCamera = new ZEDCamera();

  InitParameters initParams = new InitParameters
{
    resolution = RESOLUTION.HD720,  // Lower resolution for testing
    cameraFPS = 30,                // Lower FPS for stability
    depthMode = DEPTH_MODE.NONE,   // Disable depth processing
};

ERROR_CODE err = zedCamera.Open(ref initParams);
if (err != ERROR_CODE.SUCCESS)
{
    Debug.LogError($"Failed to open ZED camera: {err}");
    // Handle the error appropriately, such as retrying or exiting the application
    return;
}
Debug.Log("ZED camera opened successfully.");

    // 

    int imageWidth = zedCamera.ImageWidth;
    int imageHeight = zedCamera.ImageHeight;

    if (imageWidth > 0 && imageHeight > 0)
    {
        Debug.Log($"Image dimensions: Width = {imageWidth}, Height = {imageHeight}");
        zedTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGBA32, false);
    }
    else
    {
        Debug.LogError("Invalid image dimensions retrieved from ZED camera.");
    }
}


    void Update()
    {
        if (zedCamera != null && zedCamera.IsOpened())
        {
             // Create a ZEDMat to hold the image
            ZEDMat leftImage = new ZEDMat();
              // Retrieve the left image into ZEDMat
        zedCamera.RetrieveImage(leftImage, VIEW.LEFT);
         

            // Convert Texture2D to OpenCV Mat
            Utils.texture2DToMat(zedTexture, zedMat);

            // Resize the image using OpenCV
            Imgproc.resize(zedMat, resizedMat, new Size(targetWidth, targetHeight), 0, 0, Imgproc.INTER_LINEAR);

            // Convert the resized Mat back to Texture2D
            Texture2D resizedTexture = new Texture2D(targetWidth, targetHeight, TextureFormat.RGBA32, false);
            Utils.matToTexture2D(resizedMat, resizedTexture);

            // Apply the texture to the RawImage component
            if (rawImageComponent != null && resizedTexture != null)
            {
                rawImageComponent.texture = resizedTexture;
                Debug.Log("Texture assigned to RawImage.");
            }
            else
            {
                Debug.LogError("Either RawImage or resizedTexture is null.");
            }
        }
    }

    void OnDestroy()
    {
        // Properly close the ZED camera and release resources
        if (zedCamera != null)
        {
            zedCamera.Close();
        }
        if (zedMat != null)
        {
            zedMat.Dispose();
        }
        if (resizedMat != null)
        {
            resizedMat.Dispose();
        }
    }
}

You need to initialize the zedCamera object by calling :

zedCamera.CreateCamera(0, false);

before the open.

Stereolabs Support