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 using Zivid
The following examples assume you have a Zivid camera connected.
Capture 3D point cloud
This example captures a 3D point cloud using a Zivid camera, then writes the data to disk.
#include <chrono>
#include <iostream>
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 point cloud and visualize it
This example captures a 3D point cloud 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" << std::endl;
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
}
Capture HDR 3D point cloud
This example captures a HDR 3D point cloud 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 : { 20U, 25U, 30U })
{
std::cout << "Capture frame with iris = " << iris << std::endl;
frames.emplace_back(camera.capture());
}
std::cout << "Creating HDR frame" << std::endl;
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;
std::cout << "Run the visualizer. Block until window closes" << std::endl;
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
}
Capture 2D image
This example captures a 2D image using the Zivid camera and saves the image to disk.
#include <chrono>
#include <iostream>
int main()
{
try
{
std::cout << "Connecting to camera" << std::endl;
std::cout << "Setting the capture settings" << std::endl;
std::cout << "Capture a 2D frame" << std::endl;
auto frame = camera.capture2D(settings);
std::cout << "Get RGBA8 image from Frame2D" << std::endl;
std::cout << "Get pixel color at row=100, column=50" << std::endl;
auto pixel = image(100, 50);
std::cout << "Pixel color: R=" << static_cast<int>(pixel.r) << ", G=" << static_cast<int>(pixel.g)
<< ", B=" << static_cast<int>(pixel.b) << ", A=" << static_cast<int>(pixel.a) << std::endl;
auto resultFile = "result.png";
std::cout << "Saving the image to " << resultFile << std::endl;
image.save(resultFile);
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
}
Capture using Capture Assistant
This example shows how to use the Capture Assistant to find recommended capture settings for your scene, and then capture using those settings.
#include <chrono>
#include <iostream>
int main()
{
try
{
std::cout << "Connecting to camera" << std::endl;
const auto resultFile{ "result.zdf" };
std::cout << "Running Capture Assistant with parameters: " << suggestSettingsParameters << std::endl;
std::cout << "Suggested settings are:" << std::endl;
for(const auto &setting : settingsVector)
{
std::cout << setting << std::endl;
}
std::cout << "Capture (and merge) frames using automatically suggested settings" << std::endl;
std::cout << "Saving frame to file: " << resultFile << std::endl;
hdrFrame.
save(resultFile);
}
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 point cloud 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 point cloud 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" << std::endl;
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
}
Hand-eye calibration
This example shows how you can perform hand-eye-calibration using the Zivid API.
#include <iostream>
namespace
{
enum class CommandType
{
cmdAddPose,
cmdCalibrate,
cmdUnknown
};
std::string getInput()
{
std::string command;
std::getline(std::cin, command);
return command;
}
CommandType enterCommand()
{
std::cout << "Enter command, p (to add robot pose) or c (to perform calibration): ";
const auto command = getInput();
if(command == "P" || command == "p")
{
return CommandType::cmdAddPose;
}
if(command == "C" || command == "c")
{
return CommandType::cmdCalibrate;
}
return CommandType::cmdUnknown;
}
{
std::cout << "Enter pose with id (a line with 16 space separated values describing 4x4 row-major matrix) : "
<< index << std::endl;
std::stringstream input(getInput());
double element{ 0 };
std::vector<double> transformElements;
for(size_t i = 0; i < 16 && input >> element; ++i)
{
transformElements.emplace_back(element);
}
std::cout << "The following pose was entered: \n" << robotPose << std::endl;
return robotPose;
}
{
std::cout << "Capturing checkerboard image... " << std::flush;
const auto frame = camera.
capture();
std::cout << "OK" << std::endl;
return frame;
}
}
int main()
{
try
{
std::cout << "Connecting to camera..." << std::endl;
size_t currPoseId{ 0 };
bool calibrate{ false };
std::vector<Zivid::HandEye::CalibrationInput> input;
do
{
switch(enterCommand())
{
case CommandType::cmdAddPose:
{
try
{
const auto robotPose = enterRobotPose(currPoseId);
const auto frame = acquireCheckerboardFrame(camera);
std::cout << "Detecting checkerboard square centers... " << std::flush;
if(result)
{
std::cout << "OK" << std::endl;
currPoseId++;
}
else
{
std::cout << "FAILED" << std::endl;
}
}
catch(const std::exception &e)
{
continue;
}
break;
}
case CommandType::cmdCalibrate:
{
calibrate = true;
break;
}
case CommandType::cmdUnknown:
{
std::cout << "Error: Unknown command" << std::endl;
break;
}
}
} while(!calibrate);
std::cout << "Performing hand-eye calibration ... " << std::flush;
if(calibrationResult)
{
std::cout << "OK\n"
<< "Result:\n"
<< calibrationResult << std::endl;
}
else
{
std::cerr << "\nFAILED" << std::endl;
return EXIT_FAILURE;
}
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
User data
This example shows how to store custom user data on the camera.
#include <iostream>
namespace
{
enum class Mode
{
read,
write,
clear
};
std::invalid_argument usageException(const char *const *argv)
{
return std::invalid_argument{ std::string{ "Usage: " } + argv[0] + " <read|write <string>|clear>" };
}
Mode getMode(int argc, const char *const *argv)
{
if(argc >= 2)
{
if(std::string{ "read" } == argv[1])
{
return Mode::read;
}
if(std::string{ "write" } == argv[1])
{
return Mode::write;
}
if(std::string{ "clear" } == argv[1])
{
return Mode::clear;
}
}
throw usageException(argv);
}
std::string getWriteData(int argc, const char *const *argv)
{
if(argc >= 3)
{
return argv[2];
}
else
{
throw usageException(argv);
}
}
{
camera.
writeUserData(std::vector<uint8_t>{ begin(
string), end(
string) });
}
{
write(camera, "");
}
{
return std::string{ begin(data), end(data) };
}
}
int main(int argc, char **argv)
{
try
{
std::cout << "Connecting to camera" << std::endl;
if(maxDataSize == 0)
{
throw std::runtime_error{ "This camera does not support user data" };
}
switch(getMode(argc, argv))
{
case Mode::read:
std::cout << "Reading user data from camera" << std::endl;
std::cout << "Done. User data: '" << read(camera) << "'" << std::endl;
break;
case Mode::write:
{
auto userData = getWriteData(argc, argv);
std::cout << "Writing '" << userData << "' to the camera" << std::endl;
write(camera, userData);
std::cout << "Done" << std::endl;
break;
}
case Mode::clear:
std::cout << "Clearing user data from camera" << std::endl;
clear(camera);
std::cout << "Done" << std::endl;
break;
}
}
catch(const std::exception &e)
{
return EXIT_FAILURE;
}
}