16 #if MRPT_HAS_FLYCAPTURE2
17 #include <FlyCapture2.h>
18 using namespace FlyCapture2;
22 #include <fc2triclops.h>
23 using namespace Fc2Triclops;
27 #include <opencv2/core/core.hpp>
28 #include <opencv2/highgui/highgui.hpp>
29 #include <opencv2/imgproc/imgproc.hpp>
30 #include <opencv2/imgproc/imgproc_c.h>
33 #define CHECK_FC2_ERROR(_err) \
35 if (_err != PGRERROR_OK) \
37 THROW_EXCEPTION_FMT( \
38 "FlyCapture2 error:\n%s", _err.GetDescription()) \
41 #define CHECK_TRICLOPS_ERROR(_err) \
43 if (_err != TriclopsErrorOk) \
45 THROW_EXCEPTION_FMT( \
46 "Triclops Error:\n'%s'", triclopsErrorToString(_err)) \
49 #define FC2_CAM reinterpret_cast<FlyCapture2::Camera*>(m_camera)
50 #define FC2_CAM_INFO reinterpret_cast<FlyCapture2::CameraInfo*>(m_camera_info)
51 #define FC2_BUF_IMG reinterpret_cast<FlyCapture2::Image*>(m_img_buffer)
52 #define TRI_CONTEXT reinterpret_cast<TriclopsContext*>(m_triclops)
57 #if MRPT_HAS_FLYCAPTURE2
66 const fc2_str_val<VideoMode> fc2_VideoMode_table[] = {
67 {
"VIDEOMODE_160x120YUV444", VIDEOMODE_160x120YUV444},
68 {
"VIDEOMODE_320x240YUV422", VIDEOMODE_320x240YUV422},
69 {
"VIDEOMODE_640x480YUV411", VIDEOMODE_640x480YUV411},
70 {
"VIDEOMODE_640x480YUV422", VIDEOMODE_640x480YUV422},
71 {
"VIDEOMODE_640x480RGB", VIDEOMODE_640x480RGB},
72 {
"VIDEOMODE_640x480Y8", VIDEOMODE_640x480Y8},
73 {
"VIDEOMODE_640x480Y16", VIDEOMODE_640x480Y16},
74 {
"VIDEOMODE_800x600YUV422", VIDEOMODE_800x600YUV422},
75 {
"VIDEOMODE_800x600RGB", VIDEOMODE_800x600RGB},
76 {
"VIDEOMODE_800x600Y8", VIDEOMODE_800x600Y8},
77 {
"VIDEOMODE_800x600Y16", VIDEOMODE_800x600Y16},
78 {
"VIDEOMODE_1024x768YUV422", VIDEOMODE_1024x768YUV422},
79 {
"VIDEOMODE_1024x768RGB", VIDEOMODE_1024x768RGB},
80 {
"VIDEOMODE_1024x768Y8", VIDEOMODE_1024x768Y8},
81 {
"VIDEOMODE_1024x768Y16", VIDEOMODE_1024x768Y16},
82 {
"VIDEOMODE_1280x960YUV422", VIDEOMODE_1280x960YUV422},
83 {
"VIDEOMODE_1280x960RGB", VIDEOMODE_1280x960RGB},
84 {
"VIDEOMODE_1280x960Y8", VIDEOMODE_1280x960Y8},
85 {
"VIDEOMODE_1280x960Y16", VIDEOMODE_1280x960Y16},
86 {
"VIDEOMODE_1600x1200YUV422", VIDEOMODE_1600x1200YUV422},
87 {
"VIDEOMODE_1600x1200RGB", VIDEOMODE_1600x1200RGB},
88 {
"VIDEOMODE_1600x1200Y8", VIDEOMODE_1600x1200Y8},
89 {
"VIDEOMODE_1600x1200Y16", VIDEOMODE_1600x1200Y16},
90 {
"VIDEOMODE_FORMAT7", VIDEOMODE_FORMAT7}};
91 fc2_str_val<FrameRate> fc2_FrameRate_table[] = {
100 {
"FRAMERATE_FORMAT7", FlyCapture2::FRAMERATE_FORMAT7}};
101 fc2_str_val<GrabMode> fc2_GrabMode_table[] = {{
"DROP_FRAMES", DROP_FRAMES},
102 {
"BUFFER_FRAMES", BUFFER_FRAMES}};
104 #define GET_CONV_TABLE(type) \
105 vector<fc2_str_val<type>> fc2_vals_gen(type) \
107 size_t n = sizeof(fc2_##type##_table) / sizeof(fc2_##type##_table[0]); \
108 vector<fc2_str_val<type>> vec( \
109 &fc2_##type##_table[0], &fc2_##type##_table[n]); \
112 GET_CONV_TABLE(VideoMode)
113 GET_CONV_TABLE(FrameRate)
114 GET_CONV_TABLE(GrabMode)
116 template <
typename T>
119 vector<fc2_str_val<T>> fc2_vals = fc2_vals_gen(T());
121 for (
size_t i = 0; i < fc2_vals.size(); i++)
124 return fc2_vals[i].val;
129 template <
typename T>
130 const char* fc2_defnum2str(
const T&
val)
132 vector<fc2_str_val<T>> fc2_vals = fc2_vals_gen(T());
133 size_t i =
static_cast<int>(
val);
134 if (i < fc2_vals.size())
135 return fc2_vals[i].str;
138 "Error: Unknown FlyCapture2 enum: %i",
static_cast<int>(
val))
144 TCaptureOptions_FlyCapture2::TCaptureOptions_FlyCapture2()
149 grabmode(
"BUFFER_FRAMES"),
152 trigger_enabled(false),
156 strobe_enabled(false),
160 strobe_duration(1.0f),
161 autoexposure_auto(true),
162 autoexposure_onOff(true),
163 autoexposure_abs(true),
164 autoexposure_EV(0.0f),
167 shutter_time_ms(4.0f),
172 get_rectified(false),
191 cfg.
read_string(sect, prefix +
string(
"camera_guid"),
"",
true);
192 vector<string> sGUIDparts;
195 sGUIDparts.size() == 4,
196 "GUID format error: must have four blocks like XXX-XXX-XXX-XXX");
198 for (
int i = 0; i < 4; i++)
199 sscanf(sGUIDparts[i].c_str(),
"%X", &
camera_guid[i]);
264 m_camera_info(nullptr),
265 m_img_buffer(nullptr),
268 #if MRPT_HAS_FLYCAPTURE2
277 m_camera_info(nullptr),
278 m_img_buffer(nullptr),
281 #if MRPT_HAS_FLYCAPTURE2
290 #if MRPT_HAS_FLYCAPTURE2
302 #if MRPT_HAS_FLYCAPTURE2
303 FlyCapture2::Error fe;
305 cout <<
"[CImageGrabber_FlyCapture2::open] FlyCapture2 version: "
323 unsigned int numCameras;
324 fe = busMgr.GetNumOfCameras(&numCameras);
330 "Error: camera_index to open is '%u', but only '%u' "
331 "cameras were detected in the system.",
339 m_camera =
new FlyCapture2::Camera();
343 "[CImageGrabber_FlyCapture2::open] Opening camera with GUID= "
344 "%08X-%08X-%08X-%08X...\n",
345 guid.value[0], guid.value[1], guid.value[2], guid.value[3]);
354 "[CImageGrabber_FlyCapture2::open] Camera connected ok:\n"
355 " Serial number - %u\n"
356 " Camera model - %s\n"
357 " Camera vendor - %s\n"
360 " Firmware version - %s\n"
361 " Firmware build time - %s\n\n",
362 ci->serialNumber, ci->modelName, ci->vendorName, ci->sensorInfo,
363 ci->sensorResolution, ci->firmwareVersion, ci->firmwareBuildTime);
368 bool isSupported =
false;
372 FlyCapture2::VideoMode vidMode =
374 FlyCapture2::FrameRate vidRate =
377 fe =
FC2_CAM->GetVideoModeAndFrameRateInfo(
378 vidMode, vidRate, &isSupported);
383 FlyCapture2::VideoMode curVidMode;
384 FlyCapture2::FrameRate curVidRate;
386 FC2_CAM->GetVideoModeAndFrameRate(&curVidMode, &curVidRate);
390 "Camera mode '%s' + '%s' is not supported by this "
391 "camera. Current mode is %d, current rate is %d.",
394 static_cast<int>(curVidMode),
395 static_cast<int>(curVidRate)))
398 fe =
FC2_CAM->SetVideoModeAndFrameRate(vidMode, vidRate);
403 #if MRPT_HAS_TRICLOPS
404 Fc2Triclops::ErrorType fte;
406 StereoCameraMode
mode = TWO_CAMERA;
408 if (fte) handleFc2TriclopsError(fte,
"setStereoMode");
414 if (fte != ERRORTYPE_OK)
415 handleFc2TriclopsError(fte,
"getContextFromCamera");
432 te = triclopsSetResolution(
440 te = triclopsGetImageCenter(
450 FlyCapture2::VideoMode curVidMode;
451 FlyCapture2::FrameRate curVidRate;
452 fe =
FC2_CAM->GetVideoModeAndFrameRate(&curVidMode, &curVidRate);
453 if (fe == PGRERROR_OK)
455 "[CImageGrabber_FlyCapture2::open] Current camera mode is %s, "
456 "current rate is %s.\n",
457 fc2_defnum2str<FlyCapture2::VideoMode>(curVidMode),
458 fc2_defnum2str<FlyCapture2::FrameRate>(curVidRate));
462 FlyCapture2::TriggerModeInfo trigInfo;
463 FC2_CAM->GetTriggerModeInfo(&trigInfo);
465 FlyCapture2::TriggerMode trig;
473 fe =
FC2_CAM->SetTriggerMode(&trig);
479 FlyCapture2::StrobeControl strobe;
487 fe =
FC2_CAM->SetStrobe(&strobe);
492 FlyCapture2::FC2Config fc2conf;
493 FC2_CAM->GetConfiguration(&fc2conf);
502 fe =
FC2_CAM->SetConfiguration(&fc2conf);
507 FlyCapture2::Property
p;
508 p.type = FlyCapture2::AUTO_EXPOSURE;
522 FlyCapture2::Property
p;
523 p.type = FlyCapture2::BRIGHTNESS;
526 p.autoManualMode =
true;
535 FlyCapture2::Property
p;
536 p.type = FlyCapture2::SHUTTER;
549 FlyCapture2::Property
p;
550 p.type = FlyCapture2::GAIN;
562 EmbeddedImageInfo eii;
563 fe =
FC2_CAM->GetEmbeddedImageInfo(&eii);
564 if (fe == PGRERROR_OK)
566 if (eii.frameCounter.available) eii.frameCounter.onOff =
true;
567 if (eii.timestamp.available) eii.timestamp.onOff =
true;
568 if (eii.exposure.available) eii.exposure.onOff =
true;
569 if (eii.brightness.available) eii.brightness.onOff =
true;
572 FC2_CAM->SetEmbeddedImageInfo(&eii);
586 #if MRPT_HAS_FLYCAPTURE2
605 #if MRPT_HAS_FLYCAPTURE2
607 std::vector<const FlyCapture2::Camera*> cam_ptrs(numCameras);
609 for (
int i = 0; i < numCameras; i++)
615 "Camera #%i in list is not opened. Call open() first.", i)
618 FlyCapture2::Camera* cam =
619 reinterpret_cast<FlyCapture2::Camera*
>(
obj->m_camera);
623 if (!cam_ptrs.empty())
625 FlyCapture2::Error
error = FlyCapture2::Camera::StartSyncCapture(
626 cam_ptrs.size(), &cam_ptrs[0]);
637 #if MRPT_HAS_FLYCAPTURE2
655 #if MRPT_HAS_FLYCAPTURE2
688 #if MRPT_HAS_TRICLOPS
709 #if MRPT_HAS_FLYCAPTURE2
710 FlyCapture2::FC2Version fc2Version;
711 FlyCapture2::Utilities::GetLibraryVersion(&fc2Version);
713 "%d.%d.%d.%d", fc2Version.major, fc2Version.minor, fc2Version.type,
729 #if MRPT_HAS_FLYCAPTURE2
732 std::cerr <<
"[CImageGrabber_FlyCapture2::getObservation] Camera is "
733 "not opened. Call open() first.\n";
738 FlyCapture2::Error
error;
739 FlyCapture2::Image
image;
746 FlyCapture2::PixelFormat pf =
image.GetPixelFormat();
747 const bool is_color =
748 pf == PIXEL_FORMAT_RGB8 || pf == PIXEL_FORMAT_RGB16 ||
749 pf == PIXEL_FORMAT_S_RGB16 || pf == PIXEL_FORMAT_RAW8 ||
750 pf == PIXEL_FORMAT_RAW16 || pf == PIXEL_FORMAT_RAW12 ||
751 pf == PIXEL_FORMAT_BGR || pf == PIXEL_FORMAT_BGRU ||
752 pf == PIXEL_FORMAT_RGBU || pf == PIXEL_FORMAT_BGR16 ||
753 pf == PIXEL_FORMAT_BGRU16 || pf == PIXEL_FORMAT_422YUV8_JPEG;
756 is_color ? PIXEL_FORMAT_BGR : PIXEL_FORMAT_MONO8,
FC2_BUF_IMG);
759 unsigned int img_rows, img_cols, img_stride;
760 FC2_BUF_IMG->GetDimensions(&img_rows, &img_cols, &img_stride);
762 img_cols, img_rows, is_color,
FC2_BUF_IMG->GetData());
765 if (timestamp.seconds != 0)
767 timestamp.seconds + 1e-6 * timestamp.microSeconds);
772 catch (std::exception& e)
774 std::cerr <<
"[CImageGrabber_FlyCapture2::getObservation] Error:\n"
775 << e.what() << std::endl;
792 #if MRPT_HAS_FLYCAPTURE2 && MRPT_HAS_TRICLOPS && MRPT_HAS_OPENCV
795 std::cerr <<
"[CImageGrabber_FlyCapture2::getObservation] Camera is "
796 "not opened. Call open() first.\n";
802 FlyCapture2::Error ferr;
803 Fc2Triclops::ErrorType fterr;
805 FlyCapture2::Image
image;
817 IplImage* imageIpl[2];
818 FlyCapture2::Image rawImage[2];
821 fterr = Fc2Triclops::unpackUnprocessedRawOrMono16Image(
825 if (fterr != Fc2Triclops::ERRORTYPE_OK)
827 Fc2Triclops::handleFc2TriclopsError(
828 fterr,
"unprocessedRawOrMono16Image()");
833 unsigned int img_rows, img_cols, img_stride;
834 for (
int i = 0; i < 2; ++i)
836 FlyCapture2::Image rgbuImage;
837 ferr = rawImage[i].SetColorProcessing(FlyCapture2::HQ_LINEAR);
839 ferr = rawImage[i].Convert(PIXEL_FORMAT_BGRU, &rgbuImage);
842 unsigned char* data::Ptr;
849 TriclopsInput triclopsColorInput;
850 te = triclopsBuildPackedTriclopsInput(
851 rgbuImage.GetCols(), rgbuImage.GetRows(),
852 rgbuImage.GetStride(),
853 (
unsigned long)
image.GetTimeStamp().seconds,
854 (
unsigned long)
image.GetTimeStamp().microSeconds,
855 rgbuImage.GetData(), &triclopsColorInput);
858 TriclopsPackedColorImage rectPackColImg;
859 te = triclopsRectifyPackedColorImage(
860 *(
TRI_CONTEXT), i == 0 ? TriCam_RIGHT : TriCam_LEFT,
861 const_cast<TriclopsInput*
>(&triclopsColorInput),
866 img_rows = rectPackColImg.nrows;
867 img_cols = rectPackColImg.ncols;
868 img_stride = rectPackColImg.rowinc;
869 data::Ptr = (
unsigned char*)rectPackColImg.data;
873 rgbuImage.GetDimensions(&img_rows, &img_cols, &img_stride);
874 data::Ptr = rgbuImage.GetData();
878 cvCreateImage(cvSize(img_cols, img_rows), IPL_DEPTH_8U, 4);
881 memcpy(tmpImage->imageData, data::Ptr, img_rows * img_stride);
882 tmpImage->widthStep = img_stride;
885 cvCreateImage(cvSize(img_cols, img_rows), IPL_DEPTH_8U, 3);
886 cvCvtColor(tmpImage, imageIpl[i], CV_BGRA2BGR);
887 imageIpl[i]->origin = tmpImage->origin;
889 cvReleaseImage(&tmpImage);
900 if (timestamp.seconds != 0)
902 timestamp.seconds + 1e-6 * timestamp.microSeconds);
904 out_observation.
timestamp = ts_retrieved;
930 catch (std::exception& e)
932 std::cerr <<
"[CImageGrabber_FlyCapture2::getObservation] Error:\n"
933 << e.what() << std::endl;
938 "MRPT compiled without support for FlyCapture2, Triclops or OpenCV")