Click or drag to resize

Camera Class

Interface to one Zivid camera
Inheritance Hierarchy
SystemObject
  Zivid.NETCamera

Namespace:  Zivid.NET
Assembly:  ZividCoreNET (in ZividCoreNET.dll) Version: 0.0.0.0
Syntax
public class Camera : IDisposable

The Camera type exposes the following members.

Constructors
  NameDescription
Public methodCamera
Initializes a new instance of the Camera class
Public methodCamera(Camera*)
Initializes a new instance of the Camera class
Public methodCamera(Camera*)
Initializes a new instance of the Camera class
Top
Properties
  NameDescription
Public propertyComputeDevice
Connect the camera to a given Compute device
Public propertyFirmwareVersion
Firmware version
Public propertyIntrinsics
Intrinsic parameters of the camera
Public propertyModelName
Model name
Public propertyRevision
Camera revision
Public propertySerialNumber
Serial number of the Zivid camera
Public propertySettings
The current camera settings
Public propertyState
Current camera state
Public propertyUserData
Read user data from camera
Public propertyUserDataMaxSizeBytes
Get the maximum number of bytes of user data that can be stored in the camera
Top
Methods
  NameDescription
Public methodAllocateDeviceCloud
Allocate a new point cloud on the Compute device
Public methodCapture
Capture a single frame
Public methodCapture2D
Capture a single frame in 2D
Public methodConnect
Connect to the camera
Public methodDisconnect
Disconnect from the camera and free all resources associated with it
Public methodDispose
Releases all resources used by the Camera
Public methodSetFrameCallback
Set a callback that is executed very time a new frame is ready
Public methodSetSettings
Update the camera settings
Public methodStartLive
Start live (continuous) capturing of frames
Public methodStopLive
Stop live (continuous) capturing of frames
Public methodToString
Get string representation of the camera info
(Overrides ObjectToString.)
Public methodUpdateSettings
Update a batch of camera settings by supplying an update settings delegate
Public methodWriteUserData
Write user data to camera. The total number of writes supported depends on camera model and size of data
Top
Remarks
See Settings for a list of settings that can be configured in the camera. Capture single frames by calling Capture or start continuous frame capture by using StartLive.
Examples
This example shows how to perform a capture with a Zivid camera and save the result to disk.
SampleCapture
using System;
using Duration = Zivid.NET.Duration;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            var resultFile = "result.zdf";

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();

            Console.WriteLine("Adjusting the iris");
            camera.UpdateSettings(s =>
            {
                s.Iris = 22;
                s.ExposureTime = Duration.FromMicroseconds(8333);
                s.Filters.Outlier.Enabled = true;
                s.Filters.Outlier.Threshold = 5;
            });

            Console.WriteLine("Capture a frame");
            var frame = camera.Capture();

            Console.WriteLine("Saving frame to file: " + resultFile);
            frame.Save(resultFile);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to simulate a capture with Zivid by reading data from a file.
SampleCaptureFromFile
// Please make sure that Zivid sample data has been selected during installation of Zivid software.
// Latest version of Zivid software (including samples) can be found at http://zivid.com/software/.

using System;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            var zdfFile = Zivid.NET.Environment.DataPath + "/MiscObjects.zdf";
            var resultFile = "result.zdf";

            Console.WriteLine("Initializing camera emulation using file: " + zdfFile);
            var camera = zivid.CreateFileCamera(zdfFile);

            Console.WriteLine("Capture a frame");
            var frame = camera.Capture();

            Console.WriteLine("Saving frame to file: " + resultFile);
            frame.Save(resultFile);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to simulate a capture and visualize the results.
SampleCaptureFromFileVis3D
// Please make sure that Zivid sample data has been selected during installation of Zivid software.
// Latest version of Zivid software (including samples) can be found at http://zivid.com/software/.

using System;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Setting up visualization");
            var visualizer = new Zivid.NET.CloudVisualizer();
            zivid.DefaultComputeDevice = visualizer.ComputeDevice;

            var zdfFile = Zivid.NET.Environment.DataPath + "/MiscObjects.zdf";

            Console.WriteLine("Initializing camera emulation using file: " + zdfFile);
            var camera = zivid.CreateFileCamera(zdfFile);

            Console.WriteLine("Capture a frame");
            var frame = camera.Capture();

            Console.WriteLine("Display the frame");
            visualizer.Show(frame);
            visualizer.ShowMaximized();
            visualizer.ResetToFit();

            Console.WriteLine("Run the visualizer. Block until window closes");
            visualizer.Run();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to perform a high-dynamic-range (HDR) capture with Zivid.
SampleCaptureHDR
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Setting up visualization");
            var visualizer = new Zivid.NET.CloudVisualizer();
            zivid.DefaultComputeDevice = visualizer.ComputeDevice;

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();

            Console.WriteLine("Recording HDR source images");
            var frames = new List<Zivid.NET.Frame>();
            foreach (var iris in new ulong[] { 10, 25, 35 })
            {
                Console.WriteLine("Measure with iris = " + iris);
                camera.UpdateSettings(s =>
                {
                    s.Iris = iris;
                });
                frames.Add(camera.Capture());
            }

            Console.WriteLine("Creating HDR frame");
            var hdrFrame = Zivid.NET.HDR.CombineFrames(frames);

            Console.WriteLine("Display the frame");
            visualizer.ShowMaximized();
            visualizer.Show(hdrFrame);
            visualizer.ResetToFit();

            Console.WriteLine("Run the visualizer. Block until window closes");
            visualizer.Run();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to perform a 2D capture with Zivid and save the image to disk.
SampleCapture2D
using System;
using System.Drawing;

using Duration = Zivid.NET.Duration;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();

            Console.WriteLine("Setting the capture settings");
            var settings = new Zivid.NET.Settings2D()
            {
                Iris = 22,
                ExposureTime = Duration.FromMicroseconds(10000),
                Gain = 1.0
            };

            Console.WriteLine("Capture a 2D frame");
            var frame2D = camera.Capture2D(settings);

            Console.WriteLine("Get RGBA8 image from Frame2D");
            var image = frame2D.Image<Zivid.NET.RGBA8>();

            Console.WriteLine("Image Width: {0}, Height: {1}", image.Width, image.Height);
            Console.WriteLine("Pixel (0, 0): R={0}, G={1}, B={2}, A={3}",
                              image[0, 0].r,
                              image[0, 0].g,
                              image[0, 0].b,
                              image[0, 0].a);

            byte[] byteArray = image.ToArray();
            Console.WriteLine("First four bytes:  [0]: {0}, [1]: {1}, [2]: {2}, [3]: {3}",
                              byteArray[0],
                              byteArray[1],
                              byteArray[2],
                              byteArray[3]);

            var resultFile = "image.png";
            Console.WriteLine("Saving the image to {0}", resultFile);
            image.Save(resultFile);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to perform repeated captures using a Zivid camera in Live mode and visualize the resulting point cloud.
SampleCaptureLiveVis3D
using System;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Setting up visualization");
            var visualizer = new Zivid.NET.CloudVisualizer();
            zivid.DefaultComputeDevice = visualizer.ComputeDevice;

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();

            visualizer.ShowMaximized();

            Console.WriteLine("Starting live capturing of frames");
            var resetToFit = true;
            camera.SetFrameCallback(frame => {
                visualizer.Show(frame);
                if (resetToFit)
                {
                    visualizer.ResetToFit();
                    resetToFit = false;
                }
                frame.Dispose();
            });

            camera.StartLive();

            Console.WriteLine("Run the visualizer. Block until window closes");
            visualizer.Run();

            Console.WriteLine("Stopping live capturing");
            camera.StopLive();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to perform a capture using a Zivid camera and visualize the resulting point cloud.
SampleCaptureVis3D
using System;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Setting up visualization");
            var visualizer = new Zivid.NET.CloudVisualizer();
            zivid.DefaultComputeDevice = visualizer.ComputeDevice;

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();

            Console.WriteLine("Adjusting the iris");
            camera.UpdateSettings(s =>
            {
                s.Iris = 22;
            });

            Console.WriteLine("Capture a frame");
            var frame = camera.Capture();

            Console.WriteLine("Display the frame");
            visualizer.Show(frame);
            visualizer.ShowMaximized();
            visualizer.ResetToFit();

            Console.WriteLine("Run the visualizer. Block until window closes");
            visualizer.Run();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }
}
Examples
This example shows how to store custom user data on the camera.
SampleCameraUserData
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    enum Mode { read, write, clear };

    static void Main(string[] args)
    {
        try
        {
            Mode mode = ParseMode(args);
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Connecting to camera");
            var camera = zivid.ConnectCamera();
            CheckUserDataSupport(camera);

            switch (mode)
            {
                case Mode.read:
                    Console.WriteLine("Reading user data from camera");
                    Console.WriteLine("Done. User data: '" + Read(camera) + "'");
                    break;
                case Mode.write:
                    var userData = ParseWriteData(args);
                    Console.WriteLine("Writing '" + userData + "' to the camera");
                    Write(camera, userData);
                    Console.WriteLine("Done");
                    break;
                case Mode.clear:
                    Console.WriteLine("Clearing user data from camera");
                    Clear(camera);
                    Console.WriteLine("Done");
                    break;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Environment.ExitCode = 1;
        }
    }

    static ArgumentException UsageException()
    {
        return new ArgumentException("Usage: "
            + System.Reflection.Assembly.GetEntryAssembly().Location
            + " <read|write <string>|clear>");
    }

    static Mode ParseMode(string[] args)
    {
        if (args.Length > 0)
        {
            if (args[0].Equals("read"))
            {
                return Mode.read;
            }
            else if (args[0].Equals("write"))
            {
                return Mode.write;
            }
            else if (args[0].Equals("clear"))
            {
                return Mode.clear;
            }
        }
        throw UsageException();
    }

    static void CheckUserDataSupport(Zivid.NET.Camera camera)
    {
        if (camera.UserDataMaxSizeBytes == 0)
        {
            throw new System.InvalidOperationException("This camera does not support user data");
        }
    }

    static string Read(Zivid.NET.Camera camera)
    {
        return System.Text.Encoding.ASCII.GetString(camera.UserData.ToArray());
    }

    static void Write(Zivid.NET.Camera camera, string dataString)
    {
        camera.WriteUserData(System.Text.Encoding.ASCII.GetBytes(dataString));
    }

    static void Clear(Zivid.NET.Camera camera)
    {
        Write(camera, "");
    }

    static string ParseWriteData(string[] args)
    {
        if (args.Length > 1)
        {
            return args[1];
        }
        throw UsageException();
    }
}
Examples
This example shows how to perform a hand-eye calibration with a real Zivid camera
SampleHandEyeCalibration
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Zivid.NET.HandEye;

class Program
{
    static void Main()
    {
        try
        {
            var zivid = new Zivid.NET.Application();

            Console.WriteLine("Connecting to camera...");
            var camera = zivid.ConnectCamera();
            var inputs = ReadInputs(camera);

            Console.WriteLine("Performing hand-eye calibration ... ");
            var calibrationResult = Calibrator.CalibrateEyeToHand(inputs);

            if (calibrationResult)
            {
                Console.WriteLine("{0}\n{1}\n{2}", "OK", "Result:", calibrationResult);
            }
            else
            {
                Console.WriteLine("Failed");
                Environment.ExitCode = 1;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex.Message);
            Environment.ExitCode = 1;
        }
    }

    static List<CalibrationInput> ReadInputs(Zivid.NET.Camera camera)
    {
        var input = new List<CalibrationInput>();
        var currentPoseId = 0U;
        var beingInput = true;

        Interaction.ExtendInputBuffer(2048);

        do
        {
            switch (Interaction.EnterCommand())
            {
                case CommandType.AddPose:
                    try
                    {
                        var robotPose = Interaction.EnterRobotPose(currentPoseId);
                        var frame = Interaction.AcquireCheckerboardFrame(camera);

                        Console.Write("Detecting checkerboard square centers... ");
                        var result = Detector.DetectFeaturePoints(frame.GetPointCloud());

                        if (result)
                        {
                            Console.WriteLine("OK");
                            input.Add(new CalibrationInput(robotPose, result));
                            ++currentPoseId;
                        }
                        else
                        {
                            Console.WriteLine("FAILED");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: {0}", ex.Message);
                        continue;
                    }
                    break;

                case CommandType.Calibrate:
                    beingInput = false;
                    break;

                case CommandType.Unknown:
                    Console.WriteLine("Error: Unknown command");
                    break;
            }
        } while (beingInput);
        return input;
    }
}

enum CommandType
{
    AddPose,
    Calibrate,
    Unknown
}

class Interaction
{
    // Console.ReadLine only supports reading 256 characters, by default. This limit is modified
    // by calling ExtendInputBuffer with the maximum length of characters to be read.
    public static void ExtendInputBuffer(int size)
    {
        Console.SetIn(new StreamReader(Console.OpenStandardInput(), Console.InputEncoding, false, size));
    }

    public static CommandType EnterCommand()
    {
        Console.Write("Enter command, p (to add robot pose) or c (to perform calibration): ");
        var command = Console.ReadLine().ToLower();

        switch (command)
        {
            case "p": return CommandType.AddPose;
            case "c": return CommandType.Calibrate;
            default: return CommandType.Unknown;
        }
    }

    public static Pose EnterRobotPose(ulong index)
    {
        var elementCount = 16;
        Console.WriteLine("Enter pose with id (a line with {0} space separated values describing 4x4 row-major matrix) : {1}", elementCount, index);
        var input = Console.ReadLine();

        var elements = input.Split()
            .Where(x => !string.IsNullOrEmpty(x.Trim()))
            .Select(x => double.Parse(x))
            .ToArray();

        var robotPose = new Pose(elements);
        Console.WriteLine("The following pose was entered: \n{0}", robotPose);
        return robotPose;
    }

    public static Zivid.NET.Frame AcquireCheckerboardFrame(Zivid.NET.Camera camera)
    {
        Console.Write("Capturing checkerboard image... ");
        var settings = new Zivid.NET.Settings
        {
            Iris = 17,
            Gain = 1.0,
            Brightness = 1.0,
            ExposureTime = Zivid.NET.Duration.FromMicroseconds(20_000),
        };
        settings.Filters.Gaussian.Enabled = true;

        camera.SetSettings(settings);
        var frame = camera.Capture();

        Console.WriteLine("OK");
        return frame;
    }
}
See Also