CVB++ 15.0
Spectral/CubeAcquisition
1// ---------------------------------------------------------------------------
4// ---------------------------------------------------------------------------
5
6#include <iostream>
7
8#include <cvb/driver/stream.hpp>
9#include <cvb/device_factory.hpp>
10
11#include <cvb/spectral/spectral.hpp>
12#include <cvb/spectral/cube.hpp>
13#include <cvb/spectral/wrapped_cube.hpp>
14
15// Get Images
16std::vector<Cvb::ImagePtr> GrabImageVec(Cvb::String driverPath, int numImg)
17{
19 imgVec.reserve(numImg);
20 // open a device
21 driverPath = Cvb::ExpandPath(driverPath);
22 auto device = Cvb::DeviceFactory::Open(driverPath, Cvb::AcquisitionStack::Vin);
23
24 auto stream = device->Stream();
25 stream->RingBuffer()->ChangeCount(numImg, Cvb::DeviceUpdateMode::NewDeviceImage);
26 // start the stream - start the acquisition
27 stream->Start();
28
29 for (int i = 0; i < numImg; ++i)
30 {
32 Cvb::ImagePtr img = stream->WaitFor(std::chrono::seconds(10), res);
33 if (res == Cvb::WaitStatus::Timeout)
34 throw std::runtime_error("acquisition timeout");
35 if( i % 10 == 0)
36 std::cout << ".";
37 imgVec.push_back(img);
38 }
39 // synchronously stop the stream
40 stream->Stop();
41 return imgVec;
42}
43
44
45int main()
46{
47 try
48 {
49 // Create cube from stack of images
50 int numberOfImages = 100;
51 Cvb::String vinDriverPath(Cvb::ExpandPath(Cvb::InstallPath() + CVB_LIT("drivers/GenICam.vin")));
52 std::cout << "Start acquiring " << numberOfImages << " images from:\n\t" << vinDriverPath << std::endl;
53 std::vector<Cvb::ImagePtr> imgVec = GrabImageVec(vinDriverPath, numberOfImages);
54
56
57 // .. MetaData
58 auto metaData = stackedCube->MetaData();
59 int bandsOut = metaData->ReadField<int>(Cvb::Spectral::FieldID::Bands);
60 // Some imaginary wavelengths
61 std::vector<double> wavelengths(bandsOut);
62 for (int i = 0; i < bandsOut; ++i)
63 wavelengths.at(i) = 400 + i;
64 metaData->WriteField<std::vector<double>>(Cvb::Spectral::FieldID::Wavelength, wavelengths);
65
66 Cvb::String enviHeaderFile(Cvb::ExpandPath(CVB_LIT("StackedCube.hdr")));
67 Cvb::String enviBinaryFile(Cvb::ExpandPath(CVB_LIT("StackedCube.bin")));
68 stackedCube->Save(enviHeaderFile, enviBinaryFile);
69 std::cout << "\nSaved Cube to file:\n\t"
70 << enviHeaderFile << "\n\t"
71 << enviBinaryFile << std::endl;
72 }
73 catch (const std::exception& error)
74 {
75 std::cout << error.what() << std::endl;
76 }
77}
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition: decl_device_factory.hpp:55
static std::unique_ptr< Cube > FromImages(const std::vector< ImagePtr > &images, CubeEncoding bufferLayout)
Initializes a stacked cube using an array of images.
Definition: cube.hpp:111
@ Bands
The number of bands.
@ Wavelength
Array of wavelengths with the unit given in FieldID::WavelengthUnit.
WaitStatus
Status after waiting for an image to be returned.
Definition: global.hpp:376
@ Timeout
A timeout occurred, no image buffer has been returned.