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 <iostream>
#include <chrono>
int main()
{
try
{
auto resultFile = "result.zdf";
std::cout << "Connecting to camera" << std::endl;
std::cout << "Adjusting the camera settings" << std::endl;
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)
{
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 <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
std::cout << "Connecting to camera" << std::endl;
std::cout << "Adjusting the iris" << std::endl;
std::cout << "Capture a frame" << std::endl;
auto frame = camera.capture();
std::cout << "Display the frame" << std::endl;
std::cout << "Run the visualizer. Block until window closes";
}
catch (const std::exception& e)
{
return EXIT_FAILURE;
}
}
Zivid capture HDR 3D image
This example captures a HDR 3D image by combining frames with several different iris values.
#include <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
std::cout << "Connecting to camera" << std::endl;
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;
frames.emplace_back(camera.capture());
}
std::cout << "Creating HDR frame" << std::endl;
std::cout << "Display the frame" << std::endl;
std::cout << "Run the visualizer. Block until window closes";
}
catch (const std::exception& e)
{
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.
#include <iostream>
int main()
{
try
{
auto resultFile = "result.zdf";
std::cout << "Initializing camera emulation using file: " << zdfFile << std::endl;
std::cout << "Capture a frame" << std::endl;
std::cout << "Saving frame to file: " << resultFile << std::endl;
}
catch (const std::exception &e)
{
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.
#include <iostream>
int main()
{
try
{
std::cout << "Setting up visualization" << std::endl;
std::cout << "Initializing camera emulation using file: " << zdfFile << std::endl;
std::cout << "Capture a frame" << std::endl;
std::cout << "Display the frame" << std::endl;
std::cout << "Run the visualizer. Block until window closes";
}
catch (const std::exception& e)
{
return EXIT_FAILURE;
}
}