This example touches on the preparation and cleanup of a camera just before and just after the acquisition of images. Image retrieval and conversion, grabbing image data, and saving images are all covered as well.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <filesystem>
#include "SpinStereoHelper.h"
#include "StereoParameters.h"
#include "Getopt.h"
using namespace std;
{
string executionPath = string(argv[0]);
size_t found = executionPath.find_last_of("/\\");
string programName = executionPath.substr(found + 1);
int iOpt;
const char* currentParamPosition;
bool bBadArgs = false;
if (argc == 1)
{
return true;
}
const char* paramMatchPattern = "n:ABCDEFGh?";
while ((iOpt =
GetOption(argc, argv, paramMatchPattern, ¤tParamPosition)) != 0)
{
switch (iOpt)
{
case 'n':
#ifdef _MSC_VER
if (sscanf_s(currentParamPosition,
"%d", ¶ms.
numImageSets) != 1)
#else
if (sscanf(currentParamPosition,
"%d", ¶ms.
numImageSets) != 1)
#endif
{
bBadArgs = true;
}
else
{
{
cout << "The numImageSets argument must be a number greater than 0." << endl;
bBadArgs = true;
}
}
break;
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'E':
break;
case 'F':
break;
case 'G':
break;
case '?':
case 'h':
default:
cerr << "Invalid option provided: " << currentParamPosition << endl;
return false;
}
}
if (bBadArgs)
{
cout << "Invalid arguments" << endl;
return false;
}
{
{
cout << "Need to have disparity Image (-E) for point cloud generation" << endl << endl;
return false;
}
{
cout << "Need to have Rectified Sensor1 Image (-C) for point cloud generation" << endl << endl;
return false;
}
}
{
cout << "Need to enable at least one image (-A/-B/-C/-D/-E)" << endl << endl;
return false;
}
return true;
}
{
cout << "Usage: ";
cout << pszProgramName << " [OPTIONS]" << endl << endl;
cout << "OPTIONS" << endl
<< endl
<< " -n NUM_FRAMES Number frames" << endl
<< " -A DO_ENABLE_RAW_SENSOR1_TRANSMIT doEnableRawSensor1Transmit" << endl
<< " -B DO_ENABLE_RAW_SENSOR2_TRANSMIT doEnableRawSensor2Transmit" << endl
<< " -C DO_ENABLE_RECT_SENSOR1_TRANSMIT doEnableRectSensor1Transmit" << endl
<< " -D DO_ENABLE_RECT_SENSOR2_TRANSMIT doEnableRectSensor2Transmit" << endl
<< " -E DO_ENABLE_DISPARITY_TRANSMIT doEnableDisparityTransmit" << endl
<< " -F DO_ENABLE_POINTCLOUD_OUTPUT doEnablePointCloudOutput" << endl
<< " -G DO_ENABLE_POST_PROCESSING doEnableDisparityPostProcessing" << endl
<< "EXAMPLE" << endl
<< endl
<<
" " << pszProgramName <<
" -n " << params.
numImageSets <<
" -A "
<< " -B "
<< " -C "
<< " -D "
<< " -E "
<< " -F " << endl
<< endl;
}
{
bool result = true;
cout << endl << endl << "*** CONFIGURING CHUNK DATA ***" << endl << endl;
try
{
CBooleanPtr ptrChunkModeActive = nodeMap.GetNode(
"ChunkModeActive");
{
cout << "Unable to activate chunk mode. Aborting..." << endl << endl;
return false;
}
ptrChunkModeActive->SetValue(true);
cout << "Chunk mode activated..." << endl;
{
cout << "Unable to retrieve chunk selector. Aborting..." << endl << endl;
return false;
}
ptrChunkSelector->GetEntries(entries);
cout << "Enabling entries..." << endl;
for (size_t i = 0; i < entries.size(); i++)
{
{
continue;
}
ptrChunkSelector->SetIntValue(ptrChunkSelectorEntry->GetValue());
cout << "\t" << ptrChunkSelectorEntry->GetSymbolic() << ": ";
CBooleanPtr ptrChunkEnable = nodeMap.GetNode(
"ChunkEnable");
{
cout << "not available" << endl;
result = false;
}
else if (ptrChunkEnable->GetValue())
{
cout << "enabled" << endl;
}
{
ptrChunkEnable->SetValue(true);
cout << "enabled" << endl;
}
else
{
cout << "not writable" << endl;
result = false;
}
}
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
try
{
{
cout << "Unable to retrieve chunk selector. Aborting..." << endl << endl;
return false;
}
ptrChunkSelector->GetEntries(entries);
cout << "Disabling entries..." << endl;
for (size_t i = 0; i < entries.size(); i++)
{
{
continue;
}
ptrChunkSelector->SetIntValue(ptrChunkSelectorEntry->GetValue());
cout << "\t" << ptrChunkSelectorEntry->GetSymbolic() << ": ";
CBooleanPtr ptrChunkEnable = nodeMap.GetNode(
"ChunkEnable");
{
cout << "not available" << endl;
result = false;
}
else if (!ptrChunkEnable->GetValue())
{
cout << "disabled" << endl;
}
{
ptrChunkEnable->SetValue(false);
cout << "disabled" << endl;
}
else
{
cout << "not writable" << endl;
}
}
cout << endl;
CBooleanPtr ptrChunkModeActive = nodeMap.GetNode(
"ChunkModeActive");
{
cout << "Unable to deactivate chunk mode. Aborting..." << endl << endl;
return false;
}
ptrChunkModeActive->SetValue(false);
cout << "Chunk mode deactivated..." << endl;
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
cout << "Printing chunk data from image..." << endl;
try
{
ChunkData chunkData = pImage->GetChunkData();
std::cout << "\tExposure time: " << exposureTime << endl;
cout << "\tFrame ID: " << frameID << endl;
cout << "\tStereoHeight: " << stereoHeight << endl;
cout << "\tStereoWidth: " << stereoWidth << endl;
cout << "\tScan3dCoordinateOffset: " << scan3dCoordinateOffset << endl;
cout << "\tScan3dBaseline: " << scan3dBaseline << endl;
cout << "\tScan3dFocalLength: " << scan3dFocalLength << endl;
cout << "\tMinZ: " << minZ << endl;
cout << "\tMaxZ: " << maxZ << endl;
cout << "\tScan3dPrincipalPointU: " << scan3dPrincipalPointU << endl;
cout << "\tScan3dPrincipalPointV: " << scan3dPrincipalPointV << endl;
cout << "\tSmallPenalty: " << smallPenalty << endl;
cout << "\tLargePenalty: " << largePenalty << endl;
cout << "\tUniquenessRatio: " << uniquenessRatio << endl;
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
int counter,
string prefix = "")
{
pointCloudParameters,
stereoCameraParameters);
stringstream strstr("");
strstr << prefix;
strstr << "PointCloud_" << counter << ".ply";
cout << "Save point cloud to file: " << strstr.str() << endl;
return true;
}
int counter,
const string prefix = "")
{
cout << "Save images to files." << endl;
stringstream strstr;
{
strstr.str("");
strstr << prefix;
strstr << "RawSensor1_" << counter << ".png";
string rawSensor1Filename = strstr.str();
cout << "Save raw Sensor1 image to file: " << rawSensor1Filename << endl;
}
{
strstr.str("");
strstr << prefix;
strstr << "RawSensor2_" << counter << ".png";
string rawSensor2Filename = strstr.str();
cout << "Save raw Sensor2 image to file: " << rawSensor2Filename << endl;
}
{
strstr.str("");
strstr << prefix;
strstr << "RectSensor1_" << counter << ".png";
string rectSensor1Filename = strstr.str();
cout << "Save rectified sensor1 image to file: " << rectSensor1Filename << endl;
}
{
strstr.str("");
strstr << prefix;
strstr << "RectSensor2_" << counter << ".png";
string rectSensor2Filename = strstr.str();
cout << "Save rectified sensor2 image to file: " << rectSensor2Filename << endl;
}
{
strstr.str("");
strstr << prefix;
strstr << "Disparity_" << counter << ".pgm";
string disparityFilename = strstr.str();
cout << "Save disparity image to file: " << disparityFilename << endl;
}
return true;
}
{
CIntegerPtr ptrPacketSize = nodeMap.GetNode(
"GevSCPSPacketSize");
{
cout << "Unable to read or write packet size. Aborting..." << endl;
return false;
}
unsigned int maxGevSCPSPacketSize = static_cast<unsigned int>(ptrPacketSize->GetMax());
const unsigned int maxPacketSize =
pCam->DiscoverMaxPacketSize() > maxGevSCPSPacketSize ? maxGevSCPSPacketSize : pCam->DiscoverMaxPacketSize();
ptrPacketSize->SetValue(maxPacketSize);
cout << "PacketSize set to: " << ptrPacketSize->GetValue() << endl;
CIntegerPtr ptrCurrentThroughput = nodeMap.GetNode(
"DeviceLinkCurrentThroughput");
CIntegerPtr ptrThroughputLimit = nodeMap.GetNode(
"DeviceLinkThroughputLimit");
{
cout << "Unable to read node DeviceLinkCurrentThroughput. Aborting..." << endl << endl;
return false;
}
{
cout << "Unable to read or write to node DeviceLinkThroughputLimit. Aborting..." << endl;
return false;
}
cout << "Current camera throughput: " << ptrCurrentThroughput->GetValue() << endl;
if (ptrThroughputLimit->GetMin() > ptrCurrentThroughput->GetValue())
{
cout << "DeviceLinkCurrentThroughput node minimum of: " << ptrThroughputLimit->GetMin()
<< " is higher than current throughput we desire to set (" << ptrCurrentThroughput->GetValue() << ")"
<< endl;
ptrThroughputLimit->SetValue(ptrThroughputLimit->GetMin());
}
else
{
ptrThroughputLimit->SetValue(ptrCurrentThroughput->GetValue());
}
cout << "DeviceLinkThroughputLimit set to: " << ptrThroughputLimit->GetValue() << endl << endl;
return true;
}
{
bool result = true;
cout << endl << endl << "*** IMAGE ACQUISITION ***" << endl << endl;
try
{
pCam->BeginAcquisition();
cout << endl << "Acquiring " << numImageSets << " image sets." << endl;
gcstring serialNumber = pCam->TLDevice.DeviceSerialNumber.GetValue();
uint64_t timeoutInMilliSecs = 2000;
for (unsigned int counter = 0; counter < numImageSets; counter++)
{
try
{
cout << endl << "Acquiring stereo image set: " << counter << endl;
imageList = pCam->GetNextImageSync(timeoutInMilliSecs);
{
cout << "Failed to get next image set." << endl;
continue;
}
{
{
{
cout << "Post-process disparity image..." << endl;
pDisparity,
pRectifiedImage,
stereoCameraParameters);
}
}
else
{
cout << "Skipping disparity post processing as disparity components are disabled" << endl;
}
}
stringstream ss("");
ss << "StereoAcquisition_" << serialNumber << "_";
{
cerr << "Failed to save images." << endl;
result = false;
break;
}
{
{
{
cerr << "Failed to compute the 3D point cloud." << endl;
result = false;
break;
}
}
else
{
cout << "Skipping compute 3D point cloud as rectified sensor1 or disparity sensor1 components "
"are disabled"
<< endl;
}
}
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
}
pCam->EndAcquisition();
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
{
bool result = true;
try
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
pCam->Init();
cout << endl << "Checking camera stereo support..." << endl;
{
cout << "Device serial number " << pCam->TLDevice.DeviceSerialNumber.GetValue()
<< " is not a valid BX camera. Skipping..." << endl;
return true;
}
#ifdef _DEBUG
#else
#endif
cout << endl << "Configuring camera..." << endl;
cout << endl << "Configuring chunk data..." << endl;
cout << endl << "Configuring device link throughput..." << endl;
cout << endl << "Configuring stereo processing..." << endl;
cout << endl <<
"*** STEREO PARAMETERS *** " << endl << stereoParameters.
ToString() << endl;
#if _DEBUG
cout << endl << "*** CAMERA CALIBRATION PARAMETERS ***" << endl;
{
cerr << "Failed to get camera calibration parameters." << endl;
return false;
}
#endif
cout << endl << "Acquiring images..." << endl;
#ifdef _DEBUG
#endif
pCam->DeInit();
}
{
cout <<
"Error: " << e.
what() << endl;
result = false;
}
return result;
}
int main(
int argc,
char** argv)
{
FILE* tempFile = fopen("test.txt", "w+");
if (tempFile == nullptr)
{
cout << "Failed to create file in current folder. Please check "
"permissions."
<< endl;
cout << "Press Enter to exit..." << endl;
getchar();
return -1;
}
fclose(tempFile);
remove("test.txt");
{
return -1;
}
cout << "Application build date: " << __DATE__ << " " << __TIME__ << endl << endl;
const LibraryVersion spinnakerLibraryVersion = system->GetLibraryVersion();
cout <<
"Spinnaker library version: " << spinnakerLibraryVersion.
major <<
"." << spinnakerLibraryVersion.
minor
<<
"." << spinnakerLibraryVersion.
type <<
"." << spinnakerLibraryVersion.
build << endl
<< endl;
const unsigned int numCameras = camList.
GetSize();
cout << "Number of cameras detected: " << numCameras << endl << endl;
if (numCameras == 0)
{
system->ReleaseInstance();
cout << "Not enough cameras!" << endl;
cout << "Done! Press Enter to exit..." << endl;
getchar();
return -1;
}
bool result = true;
for (unsigned int i = 0; i < numCameras; i++)
{
cout << endl << "Running example for camera " << i << "..." << endl;
cout << "Camera " << i << " example complete..." << endl << endl;
}
pCam = nullptr;
system->ReleaseInstance();
cout << endl << "Done! Press Enter to exit..." << endl;
getchar();
return (result == true) ? 0 : -1;
}