1+ #include " opencv2/videoio.hpp"
2+ #include " opencv2/highgui.hpp"
3+ #include " opencv2/imgproc.hpp"
4+
5+ #include < iostream>
6+
7+ using namespace cv ;
8+
9+ static void colorizeDisparity (const Mat& gray, Mat& rgb, double maxDisp=-1 .f)
10+ {
11+ CV_Assert (!gray.empty ());
12+ CV_Assert (gray.type () == CV_8UC1);
13+
14+ if (maxDisp <= 0 )
15+ {
16+ maxDisp = 0 ;
17+ minMaxLoc (gray, nullptr , &maxDisp);
18+ }
19+
20+ rgb = Mat::zeros (gray.size (), CV_8UC3);
21+ if (maxDisp < 1 )
22+ return ;
23+
24+ Mat tmp;
25+ convertScaleAbs (gray, tmp, 255 .f / maxDisp);
26+ applyColorMap (tmp, rgb, COLORMAP_JET);
27+ }
28+
29+ static float getMaxDisparity (VideoCapture& capture, int minDistance)
30+ {
31+ float b = (float )capture.get (CAP_OPENNI_DEPTH_GENERATOR_BASELINE); // mm
32+ float F = (float )capture.get (CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH); // pixels
33+ return b * F / minDistance;
34+ }
35+
36+ static void colorizeDepth (const Mat& depth, Mat& rgb)
37+ {
38+ CV_Assert (!depth.empty ());
39+
40+ normalize (depth, rgb, 0 , 255 , NORM_MINMAX, CV_8UC1);
41+ applyColorMap (rgb, rgb, COLORMAP_JET);
42+ }
43+
44+ const char * keys = " {type t | | Camera Type: OpenNI, RealSense, Orbbec}"
45+ " {dist d | 400 | The minimum measurable distance in milimeter between the camera and the object}"
46+ " {help h | | Help}" ;
47+
48+ const char * about =
49+ " \n This example demostrates how to get data from 3D cameras via OpenCV.\n "
50+ " Currently OpenCV supports 3 types of 3D cameras:\n "
51+ " 1. Depth sensors compatible with OpenNI (Kinect, XtionPRO). "
52+ " Users must install OpenNI library and PrimeSensorModule for OpenNI and configure OpenCV with WITH_OPENNI flag ON in CMake.\n "
53+ " 2. Depth sensors compatible with Intel® RealSense SDK (RealSense). "
54+ " Users must install Intel RealSense SDK 2.0 and configure OpenCV with WITH_LIBREALSENSE flag ON in CMake.\n "
55+ " 3. Orbbec UVC depth sensors. "
56+ " For the use of OpenNI based Orbbec cameras, please refer to the example openni_orbbec_astra.cpp\n " ;
57+
58+ int main (int argc, char *argv[])
59+ {
60+ CommandLineParser parser (argc, argv, keys);
61+ parser.about (about);
62+
63+ if (parser.has (" help" ))
64+ {
65+ parser.printMessage ();
66+ return 0 ;
67+ }
68+
69+ if (parser.has (" type" ))
70+ {
71+ int backend;
72+ int flagDepth, flagBGR, flagIR;
73+ bool hasDisparity = false ;
74+
75+ String camType = parser.get <String>(" type" );
76+ if (camType == " OpenNI" )
77+ {
78+ backend = CAP_OPENNI2;
79+ flagDepth = CAP_OPENNI_DEPTH_MAP;
80+ flagBGR = CAP_OPENNI_BGR_IMAGE;
81+ flagIR = CAP_OPENNI_IR_IMAGE;
82+ hasDisparity = true ;
83+ }
84+ else if (camType == " RealSense" )
85+ {
86+ backend = CAP_INTELPERC;
87+ flagDepth = CAP_INTELPERC_DEPTH_MAP;
88+ flagBGR = CAP_INTELPERC_IMAGE;
89+ flagIR = CAP_INTELPERC_IR_MAP;
90+ }
91+ else if (camType == " Orbbec" )
92+ {
93+ backend = CAP_OBSENSOR;
94+ flagDepth = CAP_OBSENSOR_DEPTH_MAP;
95+ flagBGR = CAP_OBSENSOR_BGR_IMAGE;
96+ flagIR = CAP_OBSENSOR_IR_IMAGE; // Note output IR stream is not implemented for now.
97+ }
98+ else
99+ {
100+ std::cerr << " Invalid input of camera type." << std::endl;
101+ return -1 ;
102+ }
103+
104+ VideoCapture capture (backend);
105+ if (!capture.isOpened ())
106+ {
107+ std::cerr << " Fail to open depth camera." << std::endl;
108+ return -1 ;
109+ }
110+
111+ if (camType == " OpenNI" )
112+ {
113+ capture.set (CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_VGA_30HZ);
114+ capture.set (CAP_OPENNI_DEPTH_GENERATOR_PRESENT, true );
115+ capture.set (CAP_OPENNI_IMAGE_GENERATOR_PRESENT, true );
116+ capture.set (CAP_OPENNI_IR_GENERATOR_PRESENT, true );
117+ }
118+
119+ Mat depthMap;
120+ Mat bgrImage;
121+ Mat irImage;
122+ Mat disparityMap;
123+
124+ for (;;)
125+ {
126+ if (capture.grab ())
127+ {
128+ if (capture.retrieve (depthMap, flagDepth))
129+ {
130+ Mat colorDepthMap;
131+ colorizeDepth (depthMap, colorDepthMap);
132+ imshow (" Colorized Depth Map" , colorDepthMap);
133+ }
134+
135+ if (capture.retrieve (bgrImage, flagBGR))
136+ imshow (" RGB Image" , bgrImage);
137+
138+ if (capture.retrieve (irImage, flagIR))
139+ {
140+ if (camType == " OpenNI" )
141+ {
142+ Mat ir8;
143+ irImage.convertTo (ir8, CV_8U, 256.0 / 3500 , 0.0 );
144+ imshow (" Infrared Image" , ir8);
145+ }
146+ else
147+ imshow (" Infrared Image" , irImage);
148+ }
149+
150+ if (hasDisparity)
151+ {
152+ int minDist = parser.get <int >(" dist" ); // mm
153+ if (capture.retrieve (disparityMap, CAP_OPENNI_DISPARITY_MAP))
154+ {
155+ Mat colorDisparityMap;
156+ colorizeDisparity (disparityMap, colorDisparityMap, getMaxDisparity (capture, minDist));
157+ colorDisparityMap.setTo (Scalar (0 , 0 , 0 ), disparityMap == 0 );
158+ imshow (" Colorized Disparity Map" , colorDisparityMap);
159+ }
160+ }
161+ }
162+
163+ if (waitKey (30 ) >= 0 )
164+ break ;
165+ }
166+ }
167+ else
168+ {
169+ parser.printMessage ();
170+ return 0 ;
171+ }
172+
173+ return 0 ;
174+ }
0 commit comments