7 #include "PlusConfigure.h" 15 #include <Spinnaker.h> 16 #include <SpinGenApi/SpinnakerGenApi.h> 28 const unsigned int DEFAULT_CAMERA_NUM(0);
29 const psvs::PIXEL_ENCODING DEFAULT_PIXEL_ENCODING(psvs::RGB24);
30 const FrameSizeType DEFAULT_FRAME_SIZE = { 640, 480, 1 };
31 const unsigned int DEFAULT_FRAME_RATE(30);
32 const psvs::EXPOSURE_MODE DEFAULT_EXPOSURE_MODE(psvs::EXPOSURE_AUTO_CONTINUOUS);
33 const float FLAG_EXPOSURE_MICROSEC(-1);
34 const psvs::GAIN_MODE DEFAULT_GAIN_MODE(psvs::GAIN_AUTO_CONTINUOUS);
35 const float FLAG_GAIN_DB(-1);
36 const psvs::WHITE_BALANCE_MODE DEFAULT_WHITE_BALANCE_MODE(psvs::WB_AUTO_CONTINUOUS);
37 const float FLAG_WHITE_BALANCE(-1);
40 std::string EXPOSURE_TIMED_STRING =
"TIMED";
41 std::string EXPOSURE_AUTO_ONCE_STRING =
"AUTO_ONCE";
42 std::string EXPOSURE_AUTO_CONTINUOUS_STRING =
"AUTO_CONTINUOUS";
43 std::string GAIN_MANUAL_STRING =
"MANUAL";
44 std::string GAIN_AUTO_ONCE_STRING =
"AUTO_ONCE";
45 std::string GAIN_AUTO_CONTINUOUS_STRING =
"AUTO_CONTINUOUS";
46 std::string WB_MANUAL_STRING =
"MANUAL";
47 std::string WB_AUTO_ONCE_STRING =
"AUTO_ONCE";
48 std::string WB_AUTO_CONTINUOUS_STRING =
"AUTO_CONTINUOUS";
52 class vtkPlusSpinnakerVideoSource::vtkInternal
62 virtual ~vtkInternal()
67 Spinnaker::SystemPtr SystemPtr;
70 Spinnaker::CameraList CameraList;
73 Spinnaker::CameraPtr CameraPtr;
78 : Internal(new vtkInternal(this)),
80 CameraNumber(DEFAULT_CAMERA_NUM),
81 PixelEncoding(DEFAULT_PIXEL_ENCODING),
82 FrameRate(DEFAULT_FRAME_RATE),
83 ExposureMode(DEFAULT_EXPOSURE_MODE),
84 ExposureMicroSec(FLAG_EXPOSURE_MICROSEC),
85 GainMode(DEFAULT_GAIN_MODE),
87 WhiteBalanceMode(DEFAULT_WHITE_BALANCE_MODE),
88 WhiteBalanceRed(FLAG_WHITE_BALANCE),
89 WhiteBalanceBlue(FLAG_WHITE_BALANCE)
91 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::vtkPlusSpinnakerVideoSource()");
96 this->
FrameSize[0] = DEFAULT_FRAME_SIZE[0];
97 this->
FrameSize[1] = DEFAULT_FRAME_SIZE[1];
104 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::~vtkPlusSpinnakerVideoSource()");
114 std::map<EXPOSURE_MODE, std::string> ExposureModeToString;
119 std::map<GAIN_MODE, std::string> GainModeToString;
120 GainModeToString[
GAIN_MANUAL] = GAIN_MANUAL_STRING;
124 std::map<WHITE_BALANCE_MODE, std::string> WhiteBalanceModeToString;
125 WhiteBalanceModeToString[
WB_MANUAL] = WB_MANUAL_STRING;
126 WhiteBalanceModeToString[
WB_AUTO_ONCE] = WB_AUTO_ONCE_STRING;
130 os << indent <<
"CameraNumber: " << this->
CameraNumber << std::endl;
131 os << indent <<
"VideoFormat:" << this->
PixelEncoding << std::endl;
132 os << indent <<
"FrameSize:" <<
"[" << this->
FrameSize[0] <<
", " << this->
FrameSize[1] <<
"]" << std::endl;
133 os << indent <<
"FrameRate:" << this->
FrameRate << std::endl;
134 os << indent <<
"ExposureMode:" << ExposureModeToString.find(this->
ExposureMode)->second << std::endl;
139 os << indent <<
"GainMode:" << GainModeToString.find(this->
GainMode)->second << std::endl;
142 os << indent <<
"GainDB:" << this->
GainDB << std::endl;
144 os << indent <<
"WhiteBalanceMode:" << WhiteBalanceModeToString.find(this->
WhiteBalanceMode)->second << std::endl;
147 os << indent <<
"WhiteBalance(red):" << this->
WhiteBalanceRed << std::endl;
148 os << indent <<
"WhiteBalance(blue):" << this->
WhiteBalanceBlue << std::endl;
155 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::PrintSelf(ostream& os, vtkIndent indent)");
166 LOG_WARNING(
"Requested frame rate of " << frameRate <<
" (fps) is too low. Using default of " << DEFAULT_FRAME_RATE <<
".");
186 if (exposureMicroSec < 11 || exposureMicroSec > 30000000)
188 LOG_WARNING(
"Requested invalid exposure time of " << exposureMicroSec <<
" (us). Using continuous auto-exposure.");
206 if (gainDb < 0 || gainDb > 18)
208 LOG_WARNING(
"Requested invalid gain of " << gainDb <<
" (dB). Using continuous auto-gain.");
232 LOG_ERROR(
"Failed to configure SpinnakerVideoSource: ExposureMode is set to \"TIMED\", but no ExposureMicroSec attribute is provided.");
239 LOG_ERROR(
"Failed to configure SpinnakerVideoSource: GainMode is set to \"MANUAL\", but no GainDB attribute is provided.");
246 LOG_ERROR(
"Failed to configure SpinnakerVideoSource: WhiteBalanceMode is set to \"MANUAL\", but no WhiteBalanceRed attribute is provided.");
251 LOG_ERROR(
"Failed to configure SpinnakerVideoSource: WhiteBalanceMode is set to \"MANUAL\", but no WhiteBalanceBlue attribute is provided.");
261 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::ReadConfiguration(vtkXMLDataElement* rootConfigElement)");
266 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
int,
CameraNumber, deviceConfig);
268 XML_READ_ENUM2_ATTRIBUTE_OPTIONAL(
PixelEncoding, deviceConfig,
273 int requestedFrameSize[2] = { static_cast<int>(DEFAULT_FRAME_SIZE[0]), static_cast<int>(DEFAULT_FRAME_SIZE[1]) };
274 if (deviceConfig->GetVectorAttribute(
"FrameSize", 2, requestedFrameSize))
276 if (requestedFrameSize[0] < 0 || requestedFrameSize[1] < 0)
278 LOG_ERROR(
"Negative frame size defined in config file. Please define a positive frame size.");
281 this->
FrameSize[0] = static_cast<unsigned int>(requestedFrameSize[0]);
282 this->
FrameSize[1] = static_cast<unsigned int>(requestedFrameSize[1]);
287 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
int,
FrameRate, deviceConfig);
290 XML_READ_ENUM3_ATTRIBUTE_OPTIONAL(
ExposureMode, deviceConfig,
297 XML_READ_ENUM3_ATTRIBUTE_OPTIONAL(
GainMode, deviceConfig,
301 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
float,
GainDB, deviceConfig);
308 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
float,
WhiteBalanceRed, deviceConfig);
311 XML_FIND_NESTED_ELEMENT_REQUIRED(dataSourcesElement, deviceConfig,
"DataSources");
312 for (
int nestedElementIndex = 0; nestedElementIndex < dataSourcesElement->GetNumberOfNestedElements(); nestedElementIndex++)
314 vtkXMLDataElement* dataElement = dataSourcesElement->GetNestedElement(nestedElementIndex);
315 if (STRCASECMP(dataElement->GetName(),
"DataSource") != 0)
321 if (dataElement->GetAttribute(
"Type") != NULL && STRCASECMP(dataElement->GetAttribute(
"Type"),
"Video") == 0)
325 const char* toolId = dataElement->GetAttribute(
"Id");
329 LOG_ERROR(
"Failed to initialize Spinnaker API VideoSource: Id is missing");
336 std::ostringstream str;
338 LOG_INFO(std::endl <<
"SpinnakerVideoSource configuration:" << std::endl << str.str());
346 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::WriteConfiguration(vtkXMLDataElement* rootConfigElement)");
356 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::InternalConnect()");
359 this->Internal->SystemPtr = Spinnaker::System::GetInstance();
362 this->Internal->CameraList = this->Internal->SystemPtr->GetCameras();
365 const int numConnectedCams = this->Internal->CameraList.GetSize();
366 if (numConnectedCams == 0)
368 this->Internal->CameraList.Clear();
369 LOG_ERROR(
"No Spinnaker compatible camera detected.");
372 else if (this->CameraNumber < 0 || this->
CameraNumber > numConnectedCams)
374 LOG_ERROR(
"Invalid camera number (" << this->
CameraNumber <<
375 "in config. Please verify your camera is connected and that your config contains the correct CameraNumber value.");
380 this->Internal->CameraPtr = this->Internal->CameraList.GetByIndex(this->
CameraNumber);
385 this->Internal->CameraPtr->Init();
387 catch (Spinnaker::Exception e)
389 LOG_ERROR(
"SpinnakerVideoSource: Error initializing camera. Exception text : " << e.what());
397 Spinnaker::GenApi::INodeMap& nodeMap = this->Internal->CameraPtr->GetNodeMap();
400 std::map<PIXEL_ENCODING, std::string> PixelEncodingToString;
401 PixelEncodingToString[
RGB24] =
"BayerRG8";
402 PixelEncodingToString[
MONO8] =
"Mono8";
405 Spinnaker::GenApi::CEnumerationPtr pfPtr = nodeMap.GetNode(
"PixelFormat");
406 if (Spinnaker::GenApi::IsAvailable(pfPtr) && Spinnaker::GenApi::IsWritable(pfPtr))
409 std::string spinnakerPixelFormat = PixelEncodingToString.find(this->
PixelEncoding)->second;
410 Spinnaker::GenApi::CEnumEntryPtr pfEnumPtr = pfPtr->GetEntryByName(spinnakerPixelFormat.c_str());
411 if (Spinnaker::GenApi::IsAvailable(pfEnumPtr) && Spinnaker::GenApi::IsReadable(pfEnumPtr))
414 int64_t pixelFormat = pfEnumPtr->GetValue();
416 pfPtr->SetIntValue(pixelFormat);
420 LOG_ERROR(
"Requested VideoFormat of \"" << this->
PixelEncoding <<
"\" is not available...");
426 LOG_ERROR(
"Requested VideoFormat of \"" << this->
PixelEncoding <<
"\" is not available...");
431 Spinnaker::GenApi::CIntegerPtr WidthPtr = nodeMap.GetNode(
"Width");
432 if (Spinnaker::GenApi::IsAvailable(WidthPtr) && Spinnaker::GenApi::IsWritable(WidthPtr))
435 if (this->
FrameSize[0] != WidthPtr->GetValue())
437 LOG_WARNING(
"Failed to set image width to requested value of " << this->
FrameSize[0] <<
438 "(px). Setting width to default value of " << DEFAULT_FRAME_SIZE[0] <<
"(px).");
439 WidthPtr->SetValue(DEFAULT_FRAME_SIZE[0]);
440 this->
FrameSize[0] = DEFAULT_FRAME_SIZE[0];
445 LOG_ERROR(
"Unable to set image width.");
450 Spinnaker::GenApi::CIntegerPtr HeightPtr = nodeMap.GetNode(
"Height");
451 if (Spinnaker::GenApi::IsAvailable(HeightPtr) && Spinnaker::GenApi::IsWritable(HeightPtr))
454 if (this->
FrameSize[1] != HeightPtr->GetValue())
456 LOG_WARNING(
"Failed to set image height to requested value of " << this->
FrameSize[1] <<
457 "(px). Setting width to default value of " << DEFAULT_FRAME_SIZE[1] <<
"(px).");
458 WidthPtr->SetValue(DEFAULT_FRAME_SIZE[1]);
459 this->
FrameSize[1] = DEFAULT_FRAME_SIZE[1];
464 LOG_ERROR(
"Unable to set image height.");
469 this->Internal->CameraPtr->AcquisitionMode.SetValue(Spinnaker::AcquisitionMode_Continuous);
472 Spinnaker::GenApi::CBooleanPtr FrameRateEnablePtr = nodeMap.GetNode(
"AcquisitionFrameRateEnable");
473 if (Spinnaker::GenApi::IsAvailable(FrameRateEnablePtr) && Spinnaker::GenApi::IsWritable(FrameRateEnablePtr))
475 FrameRateEnablePtr->SetValue(
true);
479 LOG_ERROR(
"Unable to enable manual frame rate control.");
484 Spinnaker::GenApi::CFloatPtr FrameRatePtr = nodeMap.GetNode(
"AcquisitionFrameRate");
485 if (Spinnaker::GenApi::IsAvailable(FrameRatePtr) && Spinnaker::GenApi::IsWritable(FrameRatePtr))
488 if (this->
FrameRate != FrameRatePtr->GetValue())
490 LOG_WARNING(
"Failed to set frame rate to requested value of " << this->
FrameRate <<
491 "(frames / sec). FrameRate set to default value of " << FrameRatePtr->GetValue() <<
" (frames / sec).");
496 LOG_ERROR(
"Unable to set frame rate.");
503 this->Internal->CameraPtr->ExposureAuto.SetValue(Spinnaker::ExposureAuto_Off);
504 this->Internal->CameraPtr->ExposureMode.SetValue(Spinnaker::ExposureMode_Timed);
509 this->Internal->CameraPtr->ExposureAuto.SetValue(Spinnaker::ExposureAuto_Once);
513 this->Internal->CameraPtr->ExposureAuto.SetValue(Spinnaker::ExposureAuto_Continuous);
519 this->Internal->CameraPtr->GainAuto.SetValue(Spinnaker::GainAuto_Off);
520 this->Internal->CameraPtr->Gain.SetValue(this->
GainDB);
524 this->Internal->CameraPtr->GainAuto.SetValue(Spinnaker::GainAuto_Once);
528 this->Internal->CameraPtr->GainAuto.SetValue(Spinnaker::GainAuto_Continuous);
534 this->Internal->CameraPtr->BalanceWhiteAuto.SetValue(Spinnaker::BalanceWhiteAutoEnums::BalanceWhiteAuto_Off);
535 this->Internal->CameraPtr->BalanceRatioSelector.SetValue(Spinnaker::BalanceRatioSelectorEnums::BalanceRatioSelector_Blue);
536 Spinnaker::GenApi::CFloatPtr balanceRatioPtr = nodeMap.GetNode(
"BalanceRatio");
538 this->Internal->CameraPtr->BalanceRatioSelector.SetValue(Spinnaker::BalanceRatioSelectorEnums::BalanceRatioSelector_Red);
543 this->Internal->CameraPtr->BalanceWhiteAuto.SetValue(Spinnaker::BalanceWhiteAutoEnums::BalanceWhiteAuto_Once);
547 this->Internal->CameraPtr->BalanceWhiteAuto.SetValue(Spinnaker::BalanceWhiteAutoEnums::BalanceWhiteAuto_Continuous);
550 catch (Spinnaker::Exception &e)
552 LOG_ERROR(
"SpinnakerVideoSource: Error configuring camera. Exception text: " << e.what());
562 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::InternalDisconnect()");
566 this->Internal->CameraPtr->DeInit();
567 this->Internal->CameraPtr = NULL;
569 catch (Spinnaker::Exception &e)
571 LOG_ERROR(
"SpinnakerVideoSource: Failed to de-initialize camera. Exception text: " << e.what());
577 this->Internal->CameraList.Clear();
579 catch (Spinnaker::Exception & e)
581 LOG_ERROR(
"SpinnakerVideoSource: Failed to clear camera list. Exception text: " << e.what());
587 this->Internal->SystemPtr->ReleaseInstance();
589 catch (Spinnaker::Exception & e)
591 LOG_ERROR(
"SpinnakerVideoSource: Failed to release system pointer. Exception text: " << e.what());
601 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::InternalStartRecording()");
606 Spinnaker::GenApi::INodeMap& nodeMapTLDevice = this->Internal->CameraPtr->GetTLDeviceNodeMap();
607 Spinnaker::GenApi::INodeMap& nodeMap = this->Internal->CameraPtr->GetNodeMap();
611 Spinnaker::GenApi::CEnumerationPtr ptrAcquisitionMode = nodeMap.GetNode(
"AcquisitionMode");
612 if (!IsAvailable(ptrAcquisitionMode) || !IsWritable(ptrAcquisitionMode))
614 LOG_ERROR(
"Unable to set acquisition mode to continuous (node retrieval). Aborting...");
617 Spinnaker::GenApi::CEnumEntryPtr ptrAcquisitionModeContinuous = ptrAcquisitionMode->GetEntryByName(
"Continuous");
618 if (!IsAvailable(ptrAcquisitionModeContinuous) || !IsReadable(ptrAcquisitionModeContinuous))
620 LOG_ERROR(
"Unable to set acquisition mode to continuous (entry 'continuous' retrieval). Aborting...");
623 int64_t acquisitionModeContinuous = ptrAcquisitionModeContinuous->GetValue();
624 ptrAcquisitionMode->SetIntValue(acquisitionModeContinuous);
625 LOG_INFO(
"Acquisition mode set to continuous...");
628 this->Internal->CameraPtr->BeginAcquisition();
631 Spinnaker::GenApi::CStringPtr ptrStringSerial = nodeMapTLDevice.GetNode(
"DeviceSerialNumber");
632 if (IsAvailable(ptrStringSerial) && IsReadable(ptrStringSerial))
638 catch (Spinnaker::Exception &e)
640 LOG_ERROR(
"SpinnakerVideoSource: Failed to start image acquisition. Exception text: " << e.what());
650 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::InternalStopRecording()");
655 this->Internal->CameraPtr->EndAcquisition();
657 catch (Spinnaker::Exception &e)
659 LOG_ERROR(
"SpinnakerVideoSource: Failed to end recording. Exception text: " << e.what());
670 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::NotifyConfigured()");
674 LOG_ERROR(
"Device must have at least one video source.");
679 LOG_ERROR(
"Device must have at least one output channel.");
689 LOG_TRACE(
"vtkPlusSpinnakerVideoSource::InternalUpdate()");
696 Spinnaker::ImagePtr pResultImage = this->Internal->CameraPtr->GetNextImage();
697 if (pResultImage->IsIncomplete())
699 cout <<
"Image incomplete with image status " << pResultImage->GetImageStatus() <<
"..." << endl << endl;
703 Spinnaker::ImagePtr convertedImage;
706 convertedImage = pResultImage->Convert(Spinnaker::PixelFormat_RGB8, Spinnaker::HQ_LINEAR);
710 convertedImage = pResultImage->Convert(Spinnaker::PixelFormat_Mono8, Spinnaker::HQ_LINEAR);
714 LOG_ERROR(
"Invalid image type selected.");
724 LOG_WARNING(
"Failed to get video source in SpinnakerVideoSource");
751 convertedImage->GetData(),
763 convertedImage->GetData(),
774 pResultImage->Release();
776 catch (Spinnaker::Exception &e)
778 LOG_ERROR(
"SpinnakerVideoSource: Failed in InternalUpdate(). Exception text: " << e.what());
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual PlusStatus InternalConnect()
virtual int GetNumberOfVideoSources() const
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
WHITE_BALANCE_MODE WhiteBalanceMode
PlusStatus InternalStartRecording()
virtual PlusStatus NotifyConfigured()
PlusStatus SetInputFrameSize(unsigned int x, unsigned int y, unsigned int z)
virtual PlusStatus AddItem(vtkImageData *frame, US_IMAGE_ORIENTATION usImageOrientation, US_IMAGE_TYPE imageType, long frameNumber, double unfilteredTimestamp=UNDEFINED_TIMESTAMP, double filteredTimestamp=UNDEFINED_TIMESTAMP, const igsioFieldMapType *customFields=NULL)
PlusStatus SetGainMode(GAIN_MODE gainMode)
PlusStatus WriteConfiguration(vtkXMLDataElement *config)
PlusStatus SetFrameRate(int FrameRate)
bool RequireImageOrientationInConfiguration
PlusStatus SetImageType(US_IMAGE_TYPE imageType)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
PlusStatus SetPixelType(igsioCommon::VTKScalarPixelType pixelType)
void PrintConfiguration(ostream &os, vtkIndent indent)
virtual void PrintSelf(ostream &os, vtkIndent indent)
PlusStatus GetFirstVideoSource(vtkPlusDataSource *&anImage)
PlusStatus SetExposureMicroSec(int exposureMicroSec)
PlusStatus SetExposureMode(EXPOSURE_MODE expMode)
virtual PlusStatus InternalUpdate()
double InternalUpdateRate
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
PlusStatus SetWhiteBalanceMode(WHITE_BALANCE_MODE wbMode)
vtkStandardNewMacro(vtkPlusSpinnakerVideoSource)
PlusStatus CheckCameraParameterValidity()
bool StartThreadForInternalUpdates
~vtkPlusSpinnakerVideoSource()
virtual PlusStatus InternalDisconnect()
int32_t * deviceSerialNumber
EXPOSURE_MODE ExposureMode
PlusStatus SetNumberOfScalarComponents(unsigned int numberOfScalarComponents)
PlusStatus SetGainDB(int gainDb)
vtkPlusSpinnakerVideoSource()
Interface class to Spinnaker API compatible Point Grey Cameras.
virtual int OutputChannelCount() const
PlusStatus InternalStopRecording()
PIXEL_ENCODING PixelEncoding
virtual int GetNumberOfItems()
Interface to a 3D positioning tool, video source, or generalized data stream.