4 #include <visp3/core/vpConfig.h>
18 #if defined(VISP_HAVE_APRILTAG) && \
19 (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
20 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || \
21 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
22 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
26 #ifdef VISP_HAVE_MODULE_SENSOR
27 #include <visp3/sensor/vp1394CMUGrabber.h>
28 #include <visp3/sensor/vp1394TwoGrabber.h>
29 #include <visp3/sensor/vpFlyCaptureGrabber.h>
30 #include <visp3/sensor/vpRealSense2.h>
31 #include <visp3/sensor/vpV4l2Grabber.h>
34 #include <visp3/detection/vpDetectorAprilTag.h>
36 #include <visp3/core/vpXmlParserCamera.h>
37 #include <visp3/gui/vpDisplayFactory.h>
39 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
40 #include <opencv2/highgui/highgui.hpp>
41 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
42 #include <opencv2/videoio/videoio.hpp>
45 int main(
int argc,
const char **argv)
47 #ifdef ENABLE_VISP_NAMESPACE
54 double tagSize = 0.053;
55 float quad_decimate = 1.0;
57 std::string intrinsic_file =
"";
58 std::string camera_name =
"";
59 bool display_tag =
false;
61 unsigned int thickness = 2;
62 bool align_frame =
false;
64 #if !(defined(VISP_HAVE_DISPLAY))
65 bool display_off =
true;
66 std::cout <<
"Warning: There is no 3rd party (X11, GDI or openCV) to dislay images..." << std::endl;
68 bool display_off =
false;
73 for (
int i = 1; i < argc; i++) {
74 if (std::string(argv[i]) ==
"--pose-method" && i + 1 < argc) {
77 else if (std::string(argv[i]) ==
"--tag-size" && i + 1 < argc) {
78 tagSize = atof(argv[++i]);
80 else if (std::string(argv[i]) ==
"--camera-device" && i + 1 < argc) {
81 opt_device = atoi(argv[++i]);
83 else if (std::string(argv[i]) ==
"--quad-decimate" && i + 1 < argc) {
84 quad_decimate = (float)atof(argv[++i]);
86 else if (std::string(argv[i]) ==
"--nthreads" && i + 1 < argc) {
87 nThreads = atoi(argv[++i]);
89 else if (std::string(argv[i]) ==
"--intrinsic" && i + 1 < argc) {
90 intrinsic_file = std::string(argv[++i]);
92 else if (std::string(argv[i]) ==
"--camera-name" && i + 1 < argc) {
93 camera_name = std::string(argv[++i]);
95 else if (std::string(argv[i]) ==
"--display-tag") {
98 else if (std::string(argv[i]) ==
"--display-off") {
101 else if (std::string(argv[i]) ==
"--color" && i + 1 < argc) {
102 color_id = atoi(argv[++i]);
104 else if (std::string(argv[i]) ==
"--thickness" && i + 1 < argc) {
105 thickness = (
unsigned int)atoi(argv[++i]);
107 else if (std::string(argv[i]) ==
"--tag-family" && i + 1 < argc) {
110 else if (std::string(argv[i]) ==
"--z-aligned") {
113 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
114 std::cout <<
"Usage: " << argv[0] <<
" [--camera-device <camera device> (default: 0)]"
115 <<
" [--tag-size <tag_size in m> (default: 0.053)]"
116 <<
" [--quad-decimate <quad_decimate> (default: 1)]"
117 <<
" [--nthreads <nb> (default: 1)]"
118 <<
" [--intrinsic <intrinsic file> (default: empty)]"
119 <<
" [--camera-name <camera name> (default: empty)]"
120 <<
" [--pose-method <method> (0: HOMOGRAPHY, 1: HOMOGRAPHY_VIRTUAL_VS, "
121 <<
" 2: DEMENTHON_VIRTUAL_VS, 3: LAGRANGE_VIRTUAL_VS, "
122 <<
" 4: BEST_RESIDUAL_VIRTUAL_VS, 5: HOMOGRAPHY_ORTHOGONAL_ITERATION) (default: 0)]"
123 <<
" [--tag-family <family> (0: TAG_36h11, 1: TAG_36h10 (DEPRECATED), 2: TAG_36ARTOOLKIT (DEPRECATED),"
124 <<
" 3: TAG_25h9, 4: TAG_25h7 (DEPRECATED), 5: TAG_16h5, 6: TAG_CIRCLE21h7, 7: TAG_CIRCLE49h12,"
125 <<
" 8: TAG_CUSTOM48h12, 9: TAG_STANDARD41h12, 10: TAG_STANDARD52h13) (default: 0)]"
126 <<
" [--display-tag] [--z-aligned]";
127 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
128 std::cout <<
" [--display-off] [--color <color id>] [--thickness <line thickness>]";
130 std::cout <<
" [--help,-h]" << std::endl;
135 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
136 std::shared_ptr<vpDisplay> display;
144 #if defined(VISP_HAVE_PUGIXML)
146 if (!intrinsic_file.empty() && !camera_name.empty())
151 #if defined(VISP_HAVE_V4L2)
153 std::ostringstream device;
154 device <<
"/dev/video" << opt_device;
155 std::cout <<
"Use Video 4 Linux grabber on device " << device.str() << std::endl;
159 #elif defined(VISP_HAVE_DC1394)
161 std::cout <<
"Use DC1394 grabber" << std::endl;
164 #elif defined(VISP_HAVE_CMU1394)
166 std::cout <<
"Use CMU1394 grabber" << std::endl;
169 #elif defined(VISP_HAVE_FLYCAPTURE)
171 std::cout <<
"Use FlyCapture grabber" << std::endl;
174 #elif defined(VISP_HAVE_REALSENSE2)
176 std::cout <<
"Use Realsense 2 grabber" << std::endl;
179 config.disable_stream(RS2_STREAM_DEPTH);
180 config.disable_stream(RS2_STREAM_INFRARED);
181 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
185 std::cout <<
"Read camera parameters from Realsense device" << std::endl;
187 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
188 std::cout <<
"Use OpenCV grabber on device " << opt_device << std::endl;
189 cv::VideoCapture g(opt_device);
191 std::cout <<
"Failed to open the camera" << std::endl;
200 std::cout << cam << std::endl;
201 std::cout <<
"poseEstimationMethod: " << poseEstimationMethod << std::endl;
202 std::cout <<
"tagFamily: " << tagFamily << std::endl;
203 std::cout <<
"nThreads : " << nThreads << std::endl;
204 std::cout <<
"Z aligned: " << align_frame << std::endl;
207 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
219 detector.setAprilTagQuadDecimate(quad_decimate);
220 detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
221 detector.setAprilTagNbThreads(nThreads);
223 detector.setZAlignedWithCameraAxis(align_frame);
226 std::vector<double> time_vec;
229 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
230 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
232 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
242 std::vector<vpHomogeneousMatrix> cMo_vec;
243 detector.detect(I, tagSize, cam, cMo_vec);
246 time_vec.push_back(t);
248 std::stringstream ss;
249 ss <<
"Detection time: " << t <<
" ms for " << detector.getNbObjects() <<
" tags";
253 for (
size_t i = 0; i < cMo_vec.size(); i++) {
264 std::cout <<
"Benchmark computation time" << std::endl;
265 std::cout <<
"Mean / Median / Std: " <<
vpMath::getMean(time_vec) <<
" ms"
270 std::cerr <<
"Catch an exception: " << e.
getMessage() << std::endl;
273 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
274 if (display !=
nullptr) {
286 #ifndef VISP_HAVE_APRILTAG
287 std::cout <<
"Enable Apriltag support, configure and build ViSP to run this tutorial" << std::endl;
289 std::cout <<
"Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
290 <<
"Realsense2), configure and build ViSP again to use this example"
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void open(vpImage< unsigned char > &I)
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void open(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static vpColor getColor(const unsigned int &i)
static const vpColor none
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
Class that defines generic functionalities for display.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
const char * getMessage() const
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static double getMean(const std::vector< double > &v)
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
XML parser to load and save intrinsic camera parameters.
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0, bool verbose=true)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()