1
#include "opencv2/videoio/videoio.hpp"
2
#include "opencv2/highgui/highgui.hpp"
3
#include "opencv2/imgproc/imgproc.hpp"
12
cout << "\nThis program demonstrates usage of depth sensors (Kinect, XtionPRO,...).\n"
13
"The user gets some of the supported output images.\n"
14
"\nAll supported output map types:\n"
15
"1.) Data given from depth generator\n"
16
" CAP_OPENNI_DEPTH_MAP - depth values in mm (CV_16UC1)\n"
17
" CAP_OPENNI_POINT_CLOUD_MAP - XYZ in meters (CV_32FC3)\n"
18
" CAP_OPENNI_DISPARITY_MAP - disparity in pixels (CV_8UC1)\n"
19
" CAP_OPENNI_DISPARITY_MAP_32F - disparity in pixels (CV_32FC1)\n"
20
" CAP_OPENNI_VALID_DEPTH_MASK - mask of valid pixels (not ocluded, not shaded etc.) (CV_8UC1)\n"
21
"2.) Data given from RGB image generator\n"
22
" CAP_OPENNI_BGR_IMAGE - color image (CV_8UC3)\n"
23
" CAP_OPENNI_GRAY_IMAGE - gray image (CV_8UC1)\n"
27
static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
29
CV_Assert( !gray.empty() );
30
CV_Assert( gray.type() == CV_8UC1 );
35
minMaxLoc( gray, 0, &maxDisp );
38
rgb.create( gray.size(), CV_8UC3 );
43
for( int y = 0; y < gray.rows; y++ )
45
for( int x = 0; x < gray.cols; x++ )
47
uchar d = gray.at<uchar>(y,x);
48
unsigned int H = ((uchar)maxDisp - d) * 240 / (uchar)maxDisp;
50
unsigned int hi = (H/60) % 6;
51
float f = H/60.f - H/60;
52
float p = V * (1 - S);
53
float q = V * (1 - f * S);
54
float t = V * (1 - (1 - f) * S);
58
if( hi == 0 ) //R = V, G = t, B = p
59
res = Point3f( p, t, V );
60
if( hi == 1 ) // R = q, G = V, B = p
61
res = Point3f( p, V, q );
62
if( hi == 2 ) // R = p, G = V, B = t
63
res = Point3f( t, V, p );
64
if( hi == 3 ) // R = p, G = q, B = V
65
res = Point3f( V, q, p );
66
if( hi == 4 ) // R = t, G = p, B = V
67
res = Point3f( V, p, t );
68
if( hi == 5 ) // R = V, G = p, B = q
69
res = Point3f( q, p, V );
71
uchar b = (uchar)(std::max(0.f, std::min (res.x, 1.f)) * 255.f);
72
uchar g = (uchar)(std::max(0.f, std::min (res.y, 1.f)) * 255.f);
73
uchar r = (uchar)(std::max(0.f, std::min (res.z, 1.f)) * 255.f);
75
rgb.at<Point3_<uchar> >(y,x) = Point3_<uchar>(b, g, r);
80
static float getMaxDisparity( VideoCapture& capture )
82
const int minDistance = 400; // mm
83
float b = (float)capture.get( CAP_OPENNI_DEPTH_GENERATOR_BASELINE ); // mm
84
float F = (float)capture.get( CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH ); // pixels
85
return b * F / minDistance;
88
static void printCommandLineParams()
90
cout << "-cd= Colorized disparity? (0 or 1; 1 by default) Ignored if disparity map is not selected to show." << endl;
91
cout << "-fmd= Fixed max disparity? (0 or 1; 0 by default) Ignored if disparity map is not colorized (-cd 0)." << endl;
92
cout << "-mode= image mode: resolution and fps, supported three values: 0 - CAP_OPENNI_VGA_30HZ, 1 - CAP_OPENNI_SXGA_15HZ," << endl;
93
cout << " 2 - CAP_OPENNI_SXGA_30HZ (0 by default). Ignored if rgb image or gray image are not selected to show." << endl;
94
cout << "-m= Mask to set which output images are need. It is a string of size 5. Each element of this is '0' or '1' and" << endl;
95
cout << " determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently)?" << endl ;
96
cout << " By default -m=01010 i.e. disparity map and rgb image will be shown." << endl ;
97
cout << "-r= Filename of .oni video file. The data will grabbed from it." << endl ;
100
static void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[],
101
string& filename, bool& isFileReading )
104
cv::CommandLineParser parser(argc, argv, "{h help||}{cd|1|}{fmd|0|}{mode|0|}{m|01010|}{r||}");
108
printCommandLineParams();
111
isColorizeDisp = (parser.get<int>("cd") != 0);
112
isFixedMaxDisp = (parser.get<int>("fmd") != 0);
113
imageMode = parser.get<int>("mode");
114
int flags = parser.get<int>("m");
115
isFileReading = parser.has("r");
117
filename = parser.get<string>("r");
120
parser.printErrors();
124
if (flags % 100000 == 0)
126
cout << "No one output image is selected." << endl;
129
for (int i = 0; i < 5; i++)
131
retrievedImageFlags[4 - i] = (flags % 10 != 0);
137
* To work with Kinect or XtionPRO the user must install OpenNI library and PrimeSensorModule for OpenNI and
138
* configure OpenCV with WITH_OPENNI flag is ON (using CMake).
140
int main( int argc, char* argv[] )
142
bool isColorizeDisp, isFixedMaxDisp;
144
bool retrievedImageFlags[5];
147
parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags, filename, isVideoReading );
149
cout << "Device opening ..." << endl;
150
VideoCapture capture;
152
capture.open( filename );
155
capture.open( CAP_OPENNI2 );
156
if( !capture.isOpened() )
157
capture.open( CAP_OPENNI );
160
cout << "done." << endl;
162
if( !capture.isOpened() )
164
cout << "Can not open a capture object." << endl;
168
if( !isVideoReading )
174
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_VGA_30HZ );
177
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_SXGA_15HZ );
180
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_SXGA_30HZ );
182
//The following modes are only supported by the Xtion Pro Live
184
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_QVGA_30HZ );
187
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_QVGA_60HZ );
190
CV_Error( Error::StsBadArg, "Unsupported image mode property.\n");
193
cout << "\nThis image mode is not supported by the device, the default value (CV_CAP_OPENNI_SXGA_15HZ) will be used.\n" << endl;
196
// Print some avalible device settings.
197
cout << "\nDepth generator output mode:" << endl <<
198
"FRAME_WIDTH " << capture.get( CAP_PROP_FRAME_WIDTH ) << endl <<
199
"FRAME_HEIGHT " << capture.get( CAP_PROP_FRAME_HEIGHT ) << endl <<
200
"FRAME_MAX_DEPTH " << capture.get( CAP_PROP_OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
201
"FPS " << capture.get( CAP_PROP_FPS ) << endl <<
202
"REGISTRATION " << capture.get( CAP_PROP_OPENNI_REGISTRATION ) << endl;
203
if( capture.get( CAP_OPENNI_IMAGE_GENERATOR_PRESENT ) )
206
"\nImage generator output mode:" << endl <<
207
"FRAME_WIDTH " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FRAME_WIDTH ) << endl <<
208
"FRAME_HEIGHT " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FRAME_HEIGHT ) << endl <<
209
"FPS " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FPS ) << endl;
213
cout << "\nDevice doesn't contain image generator." << endl;
214
if (!retrievedImageFlags[0] && !retrievedImageFlags[1] && !retrievedImageFlags[2])
226
if( !capture.grab() )
228
cout << "Can not grab images." << endl;
233
if( retrievedImageFlags[0] && capture.retrieve( depthMap, CAP_OPENNI_DEPTH_MAP ) )
235
const float scaleFactor = 0.05f;
236
Mat show; depthMap.convertTo( show, CV_8UC1, scaleFactor );
237
imshow( "depth map", show );
240
if( retrievedImageFlags[1] && capture.retrieve( disparityMap, CAP_OPENNI_DISPARITY_MAP ) )
244
Mat colorDisparityMap;
245
colorizeDisparity( disparityMap, colorDisparityMap, isFixedMaxDisp ? getMaxDisparity(capture) : -1 );
246
Mat validColorDisparityMap;
247
colorDisparityMap.copyTo( validColorDisparityMap, disparityMap != 0 );
248
imshow( "colorized disparity map", validColorDisparityMap );
252
imshow( "original disparity map", disparityMap );
256
if( retrievedImageFlags[2] && capture.retrieve( validDepthMap, CAP_OPENNI_VALID_DEPTH_MASK ) )
257
imshow( "valid depth mask", validDepthMap );
259
if( retrievedImageFlags[3] && capture.retrieve( bgrImage, CAP_OPENNI_BGR_IMAGE ) )
260
imshow( "rgb image", bgrImage );
262
if( retrievedImageFlags[4] && capture.retrieve( grayImage, CAP_OPENNI_GRAY_IMAGE ) )
263
imshow( "gray image", grayImage );
266
if( waitKey( 30 ) >= 0 )