Zivid C++ API  1.2.1+6ca8e224-12
Defining the Future of 3D Machine Vision
Zivid sample code

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 <chrono>
#include <iostream>
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" << std::endl;
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 : { 20U, 25U, 30U })
{
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 << "Saving the frames" << std::endl;
frames[0].save("20.zdf");
frames[1].save("25.zdf");
frames[2].save("30.zdf");
hdrFrame.save("HDR.zdf");
std::cout << "Display the frame" << std::endl;
vis.show(hdrFrame);
vis.resetToFit();
std::cout << "Run the visualizer. Block until window closes" << std::endl;
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://zivid.com/software/.
#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://zivid.com/software/.
#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" << std::endl;
vis.run();
}
catch(const std::exception &e)
{
std::cerr << "Error: " << Zivid::toString(e) << std::endl;
return EXIT_FAILURE;
}
}