Common Vision Blox 14.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Programming with CVB - First Steps

With Common Vision Blox 13.3 new image and data acquisition interfaces have been introduced. The GenTL interface now also supports acquisition of multiple streams from one device, acquisition of structured data streams that contain multiple logical parts (enabling the acquisition of 3D point clouds directly from devices that support this) and acquisition into user-specified destination buffers. A detailed description how to acquire images and point clouds can be found in the document 3rd Generation Acquisition Interfaces. If you are still using the CVB vin driver and like to migrate you code to the up-to-date GenTL stack, you will find a step-by-step guide for all supported programming languages in the document Migration Guide for the Acquisition Stacks.

The Common Vision Blox SDK is provided for three programming languages. Further information can be found under the following links:

Basic Image Acquisition

After sucessfully installing CVB and configuring you camera, you can start programming with CVB.

In this section you will find code examples for each supported programming language (C++, .Net and python), covering the following topics:

(1) Setting GenICam Driver Options
(2) Getting and Setting Nodemap Features
(3) Getting an Image
(4) Accessing Pixel Values

(1) Setting GenICam Driver Options

The following code snippet shows how to set specific GenICam driver options. A list of all driver options can be found in this table.

When programming in C++ use function SetParameter().
// Discover all devices with the search criteria defined by the DiscoverFlags
auto devices = Cvb::DeviceFactory::Discover(Cvb::DiscoverFlags::IgnoreVins);
// Set number of buffers allocated at startup
devices.at(0).SetParameter(CVB_LIT("NumBuffer"), CVB_LIT("10"));
static std::vector< DiscoveryInformation > Discover()


(2) Getting and Setting Nodemap Features

To access a specific nodemap use the according get function:

C++ C# Python
Cvb::Device::NodeMap() Cvb.Device.NodeMaps cvb.Device.node_maps

In the following table all available nodemaps and their ID to be used in the get function is listed:

Nodemap / Name C++ C# Python
DevicePort / Device Cvb::NodeMapID::Device NodeMapNames.Device cvb.NodeMapID.Device
DeviceTLPort / TLDevice Cvb::NodeMapID::TLDevice NodeMapNames.TLDevice cvb.NodeMapID.TLDevice
TLPort / TLSystem Cvb::NodeMapID::System NodeMapNames.System cvb.NodeMapID.System
DataStreamPort / TLDatastream Cvb::NodeMapID::DataStream NodeMapNames.DataStream cvb.NodeMapID.DataStream
FactoryPort / TLFactory Cvb::NodeMapID::Factory NodeMapNames.Factory cvb.NodeMapID.Factory
InterfacePort / TLInterface Cvb::NodeMapID::Interface NodeMapNames.Interface cvb.NodeMapID.Interface

The following code snippet shows examplarily how to get and set the device nodemap feature "Std::ExposureTime".

#include <cvb/device_factory.hpp>
#include <cvb/genapi/node_map_enumerator.hpp>
// discover devices
auto devices = Cvb::DeviceFactory::Discover(Cvb::DiscoverFlags::IgnoreVins);
// open device
auto device = Cvb::DeviceFactory::Open<Cvb::GenICamDevice>(devices.at(0).AccessToken(), Cvb::AcquisitionStack::GenTL);
// list nodemaps
auto nodemaps = device->NodeMaps();
for (auto it = nodemaps.begin(); it != nodemaps.end(); ++it)
std::cout << it->first << std::endl;
// change node for exposure time in device node map
auto nodemap = device->NodeMap(Cvb::NodeMapID::Device);
auto node = nodemap->Node<Cvb::FloatNode>("Std::ExposureTime");
node->SetValue(node->Max() / 2.0);
std::cout << "Exposure time set to: " << node->Value() << " " << node->Unit() << "\n";
The exposure time is a Cvb::FloatNode. Be careful, that you cast your node to the correct node class.
A detailed description of the nodemaps in general can be found in the section Nodemap.

(3) Acquiring an Image

This code example shows how to acquire and save an image.
#include <cvb/global.hpp>
#include <cvb/device_factory.hpp>
#include <cvb/driver/image_stream.hpp>
// discover devices
auto devices = Cvb::DeviceFactory::Discover(Cvb::DiscoverFlags::IgnoreVins);
// open first device
auto device = Cvb::DeviceFactory::Open<Cvb::GenICamDevice>(devices.at(0).AccessToken(), Cvb::AcquisitionStack::GenTL);
// start streaming
auto dataStream = device->Stream<Cvb::ImageStream>();
dataStream->Start();
// acquire data
Cvb::WaitStatus waitStatus;
Cvb::NodeMapEnumerator enumerator;
std::tie(image, waitStatus, enumerator) = dataStream->WaitFor(std::chrono::milliseconds(3000));
image->Save("test.bmp");
// stop streaming
if(!dataStream->TryAbort())
std::cout << "Error: Acquisition has not been succesfully stopped.\n";
WaitStatus
std::shared_ptr< Image > ImagePtr


(4) Accessing Pixel Values

In general the pixel access depends on the underlying image layout. Therefore CVB provides the fast linear access for images with a linear memory representation and a slower VPAT access for all other memory layouts. Detailed information about the memory layouts can be found in the manual for the ImageManager. In C++ and python a convenient image access is provided by CVB which is independent from the memory layout.

In C++ the most convenient way to access pixel values is using Cvb::Vist(). Cvb::Vist() accepts as input an image plane and a lambda function or a function object where the operation to be executed on the image pixel is impelemented. The only pre-requisite is, that the lambda function can deal with a Cvb::Block object as shown in the example below.
A detailed description of Cvb::Vist() and more code examples can be found here.
#include <cvb/block.hpp>
auto image = ...;
// pixel access using a lamda function and Cvb::Visit()
Cvb::Visit([](auto block)
{
// loop over whole image
for (int y = 0; y < block.Height(); ++y) {
for (int x = 0; x < block.Width(); ++x) {
block(x, y) /= 2; // divide pixel value by 2
}
}
}
, image->Plane(0));

Further Examples

INotify Interface

INotify Interface - Callback Functions For Events

This interface allows to register callback functions for events like disconnect/reconnect or events generated by the device. In case such an event carries additional data, it is passed to the callback function.

auto path = Cvb::InstallPath();
path += CVB_LIT("Drivers/GenICam.vin");
path = Cvb::ExpandPath(path);
auto device = Cvb::DeviceFactory::Open(path);
std::cout << "ok" << std::endl;
device->RegisterConnectionStateChangedEvent([device]() {
// The device can only be captured when using lambda
if (device->ConnectionState() == Cvb::ConnectionState::Connected)
std::cout << "Connected" << std::endl;
else
std::cout << "Disconnected" << std::endl;
});
while (true)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)

Sample Programs

Example programs in C++, .Net and python can be found under:

  • %CVB%%\Tutorial (Windows)
  • /opt/cvb/tutorial (Linux)

or in the CVB online documentation: