Zivid  1.0.1+3607.8a7510c4
Zivid API
Zivid sample code

Table of Contents

Examples of how to use the Zivid camera from C++

The examples on this page show how to get started using the Zivid Camera. All the code are also available as C++ source in the installation.

Capture a 3D image

The following examples assume you have a Zivid camera connected.

Zivid capture frame - simple

This example captures a 3D image using a Zivid camera, then writes the data to disk.

#include <Zivid/Zivid.h>
#include <iostream>
#include <chrono>
int main()
{
try
{
auto resultFile = "result.zdf";
std::cout << "Connecting to camera" << std::endl;
auto camera = zivid.connectCamera();
std::cout << "Adjusting the camera settings" << std::endl;
camera << Zivid::Settings::Iris{ 20 }
<< Zivid::Settings::ExposureTime{ std::chrono::microseconds{ 8333 } }
std::cout << "Capture a frame" << std::endl;
auto frame = camera.capture();
std::cout << "Saving frame to file: " << resultFile << std::endl;
frame.save(resultFile);
}
catch (const std::exception& e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}

Capture 3D image and visualize it

This example captures a 3D image using a Zivid camera, then displays it using the CloudVisualizer class included in the API.

#include <Zivid/Zivid.h>
#include <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
std::cout << "Connecting to camera" << std::endl;
auto camera = zivid.connectCamera();
std::cout << "Adjusting the iris" << std::endl;
camera << Zivid::Settings::Iris{ 20 };
std::cout << "Capture a frame" << std::endl;
auto frame = camera.capture();
std::cout << "Display the frame" << std::endl;
vis.show(frame);
vis.resetToFit();
std::cout << "Run the visualizer. Block until window closes";
vis.run();
}
catch (const std::exception& e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}

Zivid capture HDR 3D image

This example captures a HDR 3D image by combining frames with several different iris values.

#include <Zivid/Zivid.h>
#include <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
std::cout << "Connecting to camera" << std::endl;
auto camera = zivid.connectCamera();
std::cout << "Recording HDR source images" << std::endl;
std::vector<Zivid::Frame> frames;
for (const size_t iris : { 10U, 25U, 35U })
{
std::cout << "Capture frame with iris = " << iris << std::endl;
camera << Zivid::Settings::Iris{ iris };
frames.emplace_back(camera.capture());
}
std::cout << "Creating HDR frame" << std::endl;
auto hdrFrame = Zivid::HDR::combineFrames(begin(frames), end(frames));
std::cout << "Display the frame" << std::endl;
vis.show(hdrFrame);
vis.resetToFit();
std::cout << "Run the visualizer. Block until window closes";
vis.run();
}
catch (const std::exception& e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}

Zivid capture from file

The following examples assume you do not use a Zivid camera, but have data files emulating its functionality.

Zivid capture from file - simple

This example captures a 3D image from a virtual camera using data from file, then writes the data to disk.

// 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://wiki.zividlabs.com/.
#include <Zivid/Zivid.h>
#include <iostream>
int main()
{
try
{
auto zdfFile = Zivid::Environment::dataPath() + "/MiscObjects.zdf";
auto resultFile = "result.zdf";
std::cout << "Initializing camera emulation using file: " << zdfFile << std::endl;
auto camera = zivid.createFileCamera(zdfFile);
std::cout << "Capture a frame" << std::endl;
auto frame = camera.capture();
std::cout << "Saving frame to file: " << resultFile << std::endl;
frame.save(resultFile);
}
catch (const std::exception &e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}

Zivid capture from file with visualization

This example captures a 3D image from a virtual camera using data from file, then visualizes the data.

// 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://wiki.zividlabs.com/.
#include <Zivid/Zivid.h>
#include <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
auto zdfFile = Zivid::Environment::dataPath() + "/MiscObjects.zdf";
std::cout << "Initializing camera emulation using file: " << zdfFile << std::endl;
auto camera = zivid.createFileCamera(zdfFile);
std::cout << "Capture a frame" << std::endl;
auto frame = camera.capture();
std::cout << "Display the frame" << std::endl;
vis.show(frame);
vis.resetToFit();
std::cout << "Run the visualizer. Block until window closes";
vis.run();
}
catch (const std::exception& e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}