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")
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
void close()
Stop capture and closes the opened camera, if any.
mrpt::math::CQuaternionDouble & quat()
Read/Write access to the quaternion representing the 3D rotation.
bool getObservation(mrpt::obs::CObservationImage &out_observation)
Grab mono image from the camera.
double x() const
Common members of all points & poses classes.
float strobe_delay
(default=0.0) Delay in ms.
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
bool strobe_enabled
(default=false) Enable the generation of a strobe signal in GPIO.
bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
bool gain_abs
(default=true) Numeric mode (absolute or integer values)
float autoexposure_EV
(default=0.0) Exposure Value, if autoexposure_auto=false
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
float read_float(const std::string §ion, const std::string &name, float defaultValue, bool failIfNotFound=false) const
void * m_img_buffer
Opaque pointer to the FlyCapture2::Image, used as a temporary buffer and to avoid mem alloc/reallocs...
A wrapper for Point Gray Research (PGR) FlyCapture2 API for capturing images from Firewire...
T y() const
Return y coordinate of the quaternion.
float m_baseline
Camera baseline (only for stereo cameras)
#define CHECK_TRICLOPS_ERROR(_err)
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
float strobe_duration
(default=1.0) Pulse durationin ms.
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Contains classes for various device interfaces.
std::string grabmode
(Default="BUFFER_FRAMES") A string with a grab mode, from the list available in FlyCapture2::GrabMode...
GLenum GLsizei GLenum GLenum const GLvoid * image
std::string read_string(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
float m_focalLength
Camera focal length.
bool autoexposure_auto
(default=true)
GLsizei GLsizei GLuint * obj
void startCapture()
Start the actual image capture of the camera.
CImageGrabber_FlyCapture2()
Constructor that does not open a camera.
#define CHECK_FC2_ERROR(_err)
This class allows loading and storing values and vectors of different types from a configuration text...
int read_int(const std::string §ion, const std::string &name, int defaultValue, bool failIfNotFound=false) const
mrpt::poses::CPose3DQuat cameraPose
The pose of the LEFT camera, relative to the robot.
int grabTimeout
(Default=5000) Time in milliseconds that RetrieveBuffer() and WaitForBufferEvent() will wait for an i...
TCaptureOptions_FlyCapture2 m_options
Camera options.
T r() const
Return r coordinate of the quaternion.
void * m_triclops
Opaque pointer to the TriclopsContext objetc.
void loadFromMemoryBuffer(unsigned int width, unsigned int height, bool color, unsigned char *rawpixels, bool swapRedBlue=false)
Reads the image from raw pixels buffer in memory.
unsigned int trigger_source
(default=0) Refer to PGR docs.
unsigned int strobe_source
(default=0) Refer to PGR docs.
unsigned int rect_width
(default=640) Width for output rectified images
void * m_camera_info
Opaque pointer to the FlyCapture2::CameraInfo object.
std::string videomode
(Default="", which means default) A string with a video mode, from the list available in FlyCapture2:...
void loadOptionsFrom(const mrpt::utils::CConfigFileBase &configSource, const std::string §ionName, const std::string &prefix=std::string())
Loads all the options from a config file.
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
Options used when creating a camera capture object of type CImageGrabber_FlyCapture2.
unsigned int rect_height
(default=480) Height for output rectified images
bool autoexposure_abs
(default=true) Numeric mode (absolute or integer values)
uint64_t TimeStamp
A real-time timestamp (ms)
void setFromIplImage(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy).
bool stereo_mode
(default=false) Obtain images as stereo pairs with Flycapture2
GLsizei const GLchar ** string
mrpt::utils::TCamera leftCamera
Parameters for the left/right cameras: individual intrinsic and distortion parameters of the cameras...
virtual ~CImageGrabber_FlyCapture2()
Destructor.
mrpt::utils::CImage imageRight
Image from the right camera, only contains a valid image if hasImageRight == true.
bool trigger_enabled
(default=false) Enable non-free-running mode, only capturing when a given input trigger signal is det...
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
static void startSyncCapture(int numCameras, const CImageGrabber_FlyCapture2 **cameras_array)
Starts a synchronous capture of several cameras, which must have been already opened.
float shutter_time_ms
(default=4.0) Shutter time, if shutter_auto=false
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
bool shutter_abs
(default=true) Numeric mode (absolute or integer values)
T x() const
Return x coordinate of the quaternion.
void * m_camera
Opaque pointer to the FlyCapture2::Camera object.
static std::string getFC2version()
Returns the PGR FlyCapture2 library version.
unsigned int trigger_mode
(default=0) Refer to PGR docs.
std::string framerate
(Default="", which means default) A string with a framerate, from the list available in FlyCapture2::...
float gain_dB
(default=0.0) Sensor gain, if gain_auto=false
bool get_rectified
(default=false) Rectify stereo images (needs Triclops installed)
mrpt::poses::CPose3DQuat rightCameraPose
The pose of the right camera, relative to the left one: Note that using the conventional reference co...
float m_centerCol
Camera center coordinates.
bool shutter_auto
(default=true)
mrpt::utils::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
uint64_t read_uint64_t(const std::string §ion, const std::string &name, uint64_t defaultValue, bool failIfNotFound=false) const
unsigned int camera_index
(Default=0) If open_by_guid==false, will open the i'th camera based on this 0-based index...
std::string trim(const std::string &str)
Removes leading and trailing spaces.
unsigned int trigger_polarity
(default=0) Refer to PGR docs.
bool gain_auto
(default=true)
bool autoexposure_onOff
(default=true) Activate this feature
mrpt::utils::TCamera rightCamera
T z() const
Return z coordinate of the quaternion.
mrpt::system::TTimeStamp time_tToTimestamp(const double t)
Transform from standard "time_t" (actually a double number, it can contain fractions of seconds) to T...
bool strCmpI(const std::string &s1, const std::string &s2)
Return true if the two strings are equal (case insensitive)
unsigned int camera_guid[4]
GUID of the camera to open, only when open_by_guid==true.
#define ASSERTMSG_(f, __ERROR_MSG)
void setIntrinsicParamsFromValues(double fx, double fy, double cx, double cy)
Set the matrix of intrinsic params of the camera from the individual values of focal length and princ...
unsigned int strobe_polarity
(default=0) Refer to PGR docs.
void open(const TCaptureOptions_FlyCapture2 &options, const bool startCapture=true)
Tries to open the camera with the given options, and starts capture.
void stopCapture()
Stop capture.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
unsigned int numBuffers
(Default=30) Number of images that can be stored in the buffer, if enabled with grabMode.
bool open_by_guid
(Default=false) Set to true to force opening a camera by its GUID, in camera_guid ...