2014-07-10 22:27:32 +08:00
|
|
|
#include "opencv2/videoio/videoio.hpp"
|
2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2011-01-22 01:00:08 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2011-02-04 21:41:10 +08:00
|
|
|
{
|
2012-03-27 15:41:48 +08:00
|
|
|
cout << "\nThis program demonstrates usage of depth sensors (Kinect, XtionPRO,...).\n"
|
2011-02-04 21:41:10 +08:00
|
|
|
"The user gets some of the supported output images.\n"
|
|
|
|
"\nAll supported output map types:\n"
|
|
|
|
"1.) Data given from depth generator\n"
|
2015-04-25 00:19:03 +08:00
|
|
|
" CAP_OPENNI_DEPTH_MAP - depth values in mm (CV_16UC1)\n"
|
|
|
|
" CAP_OPENNI_POINT_CLOUD_MAP - XYZ in meters (CV_32FC3)\n"
|
|
|
|
" CAP_OPENNI_DISPARITY_MAP - disparity in pixels (CV_8UC1)\n"
|
|
|
|
" CAP_OPENNI_DISPARITY_MAP_32F - disparity in pixels (CV_32FC1)\n"
|
|
|
|
" CAP_OPENNI_VALID_DEPTH_MASK - mask of valid pixels (not ocluded, not shaded etc.) (CV_8UC1)\n"
|
2011-02-04 21:41:10 +08:00
|
|
|
"2.) Data given from RGB image generator\n"
|
2015-04-25 00:19:03 +08:00
|
|
|
" CAP_OPENNI_BGR_IMAGE - color image (CV_8UC3)\n"
|
|
|
|
" CAP_OPENNI_GRAY_IMAGE - gray image (CV_8UC1)\n"
|
2016-06-28 01:04:59 +08:00
|
|
|
"2.) Data given from IR image generator\n"
|
|
|
|
" CAP_OPENNI_IR_IMAGE - gray image (CV_16UC1)\n"
|
2011-02-04 21:41:10 +08:00
|
|
|
<< endl;
|
|
|
|
}
|
2011-01-26 18:38:31 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
|
2011-01-22 01:00:08 +08:00
|
|
|
{
|
|
|
|
CV_Assert( !gray.empty() );
|
|
|
|
CV_Assert( gray.type() == CV_8UC1 );
|
|
|
|
|
2011-01-26 18:38:31 +08:00
|
|
|
if( maxDisp <= 0 )
|
|
|
|
{
|
|
|
|
maxDisp = 0;
|
|
|
|
minMaxLoc( gray, 0, &maxDisp );
|
|
|
|
}
|
2011-01-22 01:00:08 +08:00
|
|
|
|
|
|
|
rgb.create( gray.size(), CV_8UC3 );
|
2011-02-17 17:29:29 +08:00
|
|
|
rgb = Scalar::all(0);
|
|
|
|
if( maxDisp < 1 )
|
|
|
|
return;
|
|
|
|
|
2011-01-22 01:00:08 +08:00
|
|
|
for( int y = 0; y < gray.rows; y++ )
|
|
|
|
{
|
|
|
|
for( int x = 0; x < gray.cols; x++ )
|
|
|
|
{
|
|
|
|
uchar d = gray.at<uchar>(y,x);
|
|
|
|
unsigned int H = ((uchar)maxDisp - d) * 240 / (uchar)maxDisp;
|
|
|
|
|
|
|
|
unsigned int hi = (H/60) % 6;
|
|
|
|
float f = H/60.f - H/60;
|
|
|
|
float p = V * (1 - S);
|
|
|
|
float q = V * (1 - f * S);
|
|
|
|
float t = V * (1 - (1 - f) * S);
|
|
|
|
|
|
|
|
Point3f res;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
|
|
|
if( hi == 0 ) //R = V, G = t, B = p
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( p, t, V );
|
2012-06-08 01:21:29 +08:00
|
|
|
if( hi == 1 ) // R = q, G = V, B = p
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( p, V, q );
|
2012-06-08 01:21:29 +08:00
|
|
|
if( hi == 2 ) // R = p, G = V, B = t
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( t, V, p );
|
2012-06-08 01:21:29 +08:00
|
|
|
if( hi == 3 ) // R = p, G = q, B = V
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( V, q, p );
|
2012-06-08 01:21:29 +08:00
|
|
|
if( hi == 4 ) // R = t, G = p, B = V
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( V, p, t );
|
2012-06-08 01:21:29 +08:00
|
|
|
if( hi == 5 ) // R = V, G = p, B = q
|
2011-01-22 01:00:08 +08:00
|
|
|
res = Point3f( q, p, V );
|
|
|
|
|
|
|
|
uchar b = (uchar)(std::max(0.f, std::min (res.x, 1.f)) * 255.f);
|
|
|
|
uchar g = (uchar)(std::max(0.f, std::min (res.y, 1.f)) * 255.f);
|
|
|
|
uchar r = (uchar)(std::max(0.f, std::min (res.z, 1.f)) * 255.f);
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
rgb.at<Point3_<uchar> >(y,x) = Point3_<uchar>(b, g, r);
|
2011-01-22 01:00:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static float getMaxDisparity( VideoCapture& capture )
|
2011-01-26 18:38:31 +08:00
|
|
|
{
|
|
|
|
const int minDistance = 400; // mm
|
2013-04-08 02:45:38 +08:00
|
|
|
float b = (float)capture.get( CAP_OPENNI_DEPTH_GENERATOR_BASELINE ); // mm
|
|
|
|
float F = (float)capture.get( CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH ); // pixels
|
2011-01-26 18:38:31 +08:00
|
|
|
return b * F / minDistance;
|
2011-02-04 21:41:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void printCommandLineParams()
|
2011-02-04 21:41:10 +08:00
|
|
|
{
|
2015-08-01 23:24:23 +08:00
|
|
|
cout << "-cd= Colorized disparity? (0 or 1; 1 by default) Ignored if disparity map is not selected to show." << endl;
|
|
|
|
cout << "-fmd= Fixed max disparity? (0 or 1; 0 by default) Ignored if disparity map is not colorized (-cd 0)." << endl;
|
|
|
|
cout << "-mode= image mode: resolution and fps, supported three values: 0 - CAP_OPENNI_VGA_30HZ, 1 - CAP_OPENNI_SXGA_15HZ," << endl;
|
2015-04-25 00:19:03 +08:00
|
|
|
cout << " 2 - CAP_OPENNI_SXGA_30HZ (0 by default). Ignored if rgb image or gray image are not selected to show." << endl;
|
2015-08-01 23:24:23 +08:00
|
|
|
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;
|
2016-06-28 01:04:59 +08:00
|
|
|
cout << " determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently), ir image" << endl ;
|
|
|
|
cout << " By default -m=010100 i.e. disparity map and rgb image will be shown." << endl ;
|
2015-08-01 23:24:23 +08:00
|
|
|
cout << "-r= Filename of .oni video file. The data will grabbed from it." << endl ;
|
2011-02-04 21:41:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[],
|
2012-03-21 22:31:40 +08:00
|
|
|
string& filename, bool& isFileReading )
|
2011-02-04 21:41:10 +08:00
|
|
|
{
|
2012-03-21 22:31:40 +08:00
|
|
|
filename.clear();
|
2016-06-28 01:04:59 +08:00
|
|
|
cv::CommandLineParser parser(argc, argv, "{h help||}{cd|1|}{fmd|0|}{mode|-1|}{m|010100|}{r||}");
|
2015-08-01 23:24:23 +08:00
|
|
|
if (parser.has("h"))
|
2011-02-04 21:41:10 +08:00
|
|
|
{
|
|
|
|
help();
|
2015-08-01 23:24:23 +08:00
|
|
|
printCommandLineParams();
|
|
|
|
exit(0);
|
2011-02-04 21:41:10 +08:00
|
|
|
}
|
2015-08-01 23:24:23 +08:00
|
|
|
isColorizeDisp = (parser.get<int>("cd") != 0);
|
|
|
|
isFixedMaxDisp = (parser.get<int>("fmd") != 0);
|
|
|
|
imageMode = parser.get<int>("mode");
|
|
|
|
int flags = parser.get<int>("m");
|
|
|
|
isFileReading = parser.has("r");
|
|
|
|
if (isFileReading)
|
|
|
|
filename = parser.get<string>("r");
|
|
|
|
if (!parser.check())
|
2011-02-04 21:41:10 +08:00
|
|
|
{
|
2015-08-01 23:24:23 +08:00
|
|
|
parser.printErrors();
|
|
|
|
help();
|
|
|
|
exit(-1);
|
|
|
|
}
|
2016-06-28 01:04:59 +08:00
|
|
|
if (flags % 1000000 == 0)
|
2015-08-01 23:24:23 +08:00
|
|
|
{
|
|
|
|
cout << "No one output image is selected." << endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-06-28 01:04:59 +08:00
|
|
|
for (int i = 0; i < 6; i++)
|
2015-08-01 23:24:23 +08:00
|
|
|
{
|
2016-06-28 01:04:59 +08:00
|
|
|
retrievedImageFlags[5 - i] = (flags % 10 != 0);
|
2015-08-01 23:24:23 +08:00
|
|
|
flags /= 10;
|
2011-02-04 21:41:10 +08:00
|
|
|
}
|
2011-01-26 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 01:00:08 +08:00
|
|
|
/*
|
2012-03-13 00:07:42 +08:00
|
|
|
* To work with Kinect or XtionPRO the user must install OpenNI library and PrimeSensorModule for OpenNI and
|
2011-01-22 01:00:08 +08:00
|
|
|
* configure OpenCV with WITH_OPENNI flag is ON (using CMake).
|
|
|
|
*/
|
2011-02-04 21:41:10 +08:00
|
|
|
int main( int argc, char* argv[] )
|
2011-01-22 01:00:08 +08:00
|
|
|
{
|
2012-03-13 00:07:42 +08:00
|
|
|
bool isColorizeDisp, isFixedMaxDisp;
|
|
|
|
int imageMode;
|
2016-06-28 01:04:59 +08:00
|
|
|
bool retrievedImageFlags[6];
|
2012-03-21 22:31:40 +08:00
|
|
|
string filename;
|
|
|
|
bool isVideoReading;
|
|
|
|
parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags, filename, isVideoReading );
|
2011-01-22 01:00:08 +08:00
|
|
|
|
2012-03-13 00:07:42 +08:00
|
|
|
cout << "Device opening ..." << endl;
|
2012-03-21 22:31:40 +08:00
|
|
|
VideoCapture capture;
|
|
|
|
if( isVideoReading )
|
|
|
|
capture.open( filename );
|
|
|
|
else
|
2014-11-08 07:22:17 +08:00
|
|
|
{
|
|
|
|
capture.open( CAP_OPENNI2 );
|
|
|
|
if( !capture.isOpened() )
|
|
|
|
capture.open( CAP_OPENNI );
|
|
|
|
}
|
2012-03-21 22:31:40 +08:00
|
|
|
|
2011-01-22 01:00:08 +08:00
|
|
|
cout << "done." << endl;
|
|
|
|
|
|
|
|
if( !capture.isOpened() )
|
|
|
|
{
|
|
|
|
cout << "Can not open a capture object." << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:04:59 +08:00
|
|
|
if( !isVideoReading && imageMode >= 0 )
|
2012-03-13 00:07:42 +08:00
|
|
|
{
|
2012-03-21 22:31:40 +08:00
|
|
|
bool modeRes=false;
|
|
|
|
switch ( imageMode )
|
|
|
|
{
|
|
|
|
case 0:
|
2013-04-08 02:45:38 +08:00
|
|
|
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_VGA_30HZ );
|
2012-03-21 22:31:40 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2013-04-08 02:45:38 +08:00
|
|
|
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_SXGA_15HZ );
|
2012-03-21 22:31:40 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-04-08 02:45:38 +08:00
|
|
|
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_SXGA_30HZ );
|
2012-03-21 22:31:40 +08:00
|
|
|
break;
|
2012-10-10 02:01:43 +08:00
|
|
|
//The following modes are only supported by the Xtion Pro Live
|
|
|
|
case 3:
|
2013-04-08 02:45:38 +08:00
|
|
|
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_QVGA_30HZ );
|
2012-10-10 02:01:43 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
2013-04-08 02:45:38 +08:00
|
|
|
modeRes = capture.set( CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_QVGA_60HZ );
|
2012-10-10 02:01:43 +08:00
|
|
|
break;
|
2012-03-21 22:31:40 +08:00
|
|
|
default:
|
2013-04-08 02:45:38 +08:00
|
|
|
CV_Error( Error::StsBadArg, "Unsupported image mode property.\n");
|
2012-03-21 22:31:40 +08:00
|
|
|
}
|
|
|
|
if (!modeRes)
|
|
|
|
cout << "\nThis image mode is not supported by the device, the default value (CV_CAP_OPENNI_SXGA_15HZ) will be used.\n" << endl;
|
2012-03-13 00:07:42 +08:00
|
|
|
}
|
2011-01-26 18:38:31 +08:00
|
|
|
|
2016-06-28 01:04:59 +08:00
|
|
|
// turn on depth, color and IR if needed
|
|
|
|
if (retrievedImageFlags[0] || retrievedImageFlags[1] || retrievedImageFlags[2])
|
|
|
|
capture.set(CAP_OPENNI_DEPTH_GENERATOR_PRESENT, true);
|
|
|
|
else
|
|
|
|
capture.set(CAP_OPENNI_DEPTH_GENERATOR_PRESENT, false);
|
|
|
|
if (retrievedImageFlags[3] || retrievedImageFlags[4])
|
|
|
|
capture.set(CAP_OPENNI_IMAGE_GENERATOR_PRESENT, true);
|
|
|
|
else
|
|
|
|
capture.set(CAP_OPENNI_IMAGE_GENERATOR_PRESENT, false);
|
|
|
|
if (retrievedImageFlags[5])
|
|
|
|
capture.set(CAP_OPENNI_IR_GENERATOR_PRESENT, true);
|
|
|
|
else
|
|
|
|
capture.set(CAP_OPENNI_IR_GENERATOR_PRESENT, false);
|
|
|
|
|
2012-03-13 00:07:42 +08:00
|
|
|
// Print some avalible device settings.
|
2016-06-28 01:04:59 +08:00
|
|
|
if (capture.get(CAP_OPENNI_DEPTH_GENERATOR_PRESENT))
|
|
|
|
{
|
|
|
|
cout << "\nDepth generator output mode:" << endl <<
|
|
|
|
"FRAME_WIDTH " << capture.get(CAP_PROP_FRAME_WIDTH) << endl <<
|
|
|
|
"FRAME_HEIGHT " << capture.get(CAP_PROP_FRAME_HEIGHT) << endl <<
|
|
|
|
"FRAME_MAX_DEPTH " << capture.get(CAP_PROP_OPENNI_FRAME_MAX_DEPTH) << " mm" << endl <<
|
|
|
|
"FPS " << capture.get(CAP_PROP_FPS) << endl <<
|
|
|
|
"REGISTRATION " << capture.get(CAP_PROP_OPENNI_REGISTRATION) << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "\nDevice doesn't contain depth generator or it is not selected." << endl;
|
|
|
|
}
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if( capture.get( CAP_OPENNI_IMAGE_GENERATOR_PRESENT ) )
|
2012-03-15 23:55:07 +08:00
|
|
|
{
|
|
|
|
cout <<
|
|
|
|
"\nImage generator output mode:" << endl <<
|
2013-04-08 02:45:38 +08:00
|
|
|
"FRAME_WIDTH " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FRAME_WIDTH ) << endl <<
|
|
|
|
"FRAME_HEIGHT " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FRAME_HEIGHT ) << endl <<
|
|
|
|
"FPS " << capture.get( CAP_OPENNI_IMAGE_GENERATOR+CAP_PROP_FPS ) << endl;
|
2012-03-15 23:55:07 +08:00
|
|
|
}
|
2012-03-13 00:07:42 +08:00
|
|
|
else
|
|
|
|
{
|
2016-06-28 01:04:59 +08:00
|
|
|
cout << "\nDevice doesn't contain image generator or it is not selected." << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( capture.get(CAP_OPENNI_IR_GENERATOR_PRESENT) )
|
|
|
|
{
|
|
|
|
cout <<
|
|
|
|
"\nIR generator output mode:" << endl <<
|
|
|
|
"FRAME_WIDTH " << capture.get(CAP_OPENNI_IR_GENERATOR + CAP_PROP_FRAME_WIDTH) << endl <<
|
|
|
|
"FRAME_HEIGHT " << capture.get(CAP_OPENNI_IR_GENERATOR + CAP_PROP_FRAME_HEIGHT) << endl <<
|
|
|
|
"FPS " << capture.get(CAP_OPENNI_IR_GENERATOR + CAP_PROP_FPS) << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "\nDevice doesn't contain IR generator or it is not selected." << endl;
|
2012-03-13 00:07:42 +08:00
|
|
|
}
|
2011-01-26 18:38:31 +08:00
|
|
|
|
2011-01-22 01:00:08 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
Mat depthMap;
|
|
|
|
Mat validDepthMap;
|
|
|
|
Mat disparityMap;
|
|
|
|
Mat bgrImage;
|
|
|
|
Mat grayImage;
|
2016-06-28 01:04:59 +08:00
|
|
|
Mat irImage;
|
2011-01-22 01:00:08 +08:00
|
|
|
|
|
|
|
if( !capture.grab() )
|
|
|
|
{
|
|
|
|
cout << "Can not grab images." << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-08 02:45:38 +08:00
|
|
|
if( retrievedImageFlags[0] && capture.retrieve( depthMap, CAP_OPENNI_DEPTH_MAP ) )
|
2011-01-22 01:00:08 +08:00
|
|
|
{
|
|
|
|
const float scaleFactor = 0.05f;
|
|
|
|
Mat show; depthMap.convertTo( show, CV_8UC1, scaleFactor );
|
|
|
|
imshow( "depth map", show );
|
|
|
|
}
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if( retrievedImageFlags[1] && capture.retrieve( disparityMap, CAP_OPENNI_DISPARITY_MAP ) )
|
2011-01-22 01:00:08 +08:00
|
|
|
{
|
2011-02-04 21:41:10 +08:00
|
|
|
if( isColorizeDisp )
|
|
|
|
{
|
|
|
|
Mat colorDisparityMap;
|
|
|
|
colorizeDisparity( disparityMap, colorDisparityMap, isFixedMaxDisp ? getMaxDisparity(capture) : -1 );
|
|
|
|
Mat validColorDisparityMap;
|
|
|
|
colorDisparityMap.copyTo( validColorDisparityMap, disparityMap != 0 );
|
|
|
|
imshow( "colorized disparity map", validColorDisparityMap );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imshow( "original disparity map", disparityMap );
|
|
|
|
}
|
2011-01-22 01:00:08 +08:00
|
|
|
}
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if( retrievedImageFlags[2] && capture.retrieve( validDepthMap, CAP_OPENNI_VALID_DEPTH_MASK ) )
|
2011-01-25 01:09:45 +08:00
|
|
|
imshow( "valid depth mask", validDepthMap );
|
2011-01-22 01:00:08 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if( retrievedImageFlags[3] && capture.retrieve( bgrImage, CAP_OPENNI_BGR_IMAGE ) )
|
2011-01-22 01:00:08 +08:00
|
|
|
imshow( "rgb image", bgrImage );
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if( retrievedImageFlags[4] && capture.retrieve( grayImage, CAP_OPENNI_GRAY_IMAGE ) )
|
2011-01-22 01:00:08 +08:00
|
|
|
imshow( "gray image", grayImage );
|
2016-06-28 01:04:59 +08:00
|
|
|
|
|
|
|
if( retrievedImageFlags[5] && capture.retrieve( irImage, CAP_OPENNI_IR_IMAGE ) )
|
|
|
|
{
|
|
|
|
Mat ir8;
|
|
|
|
irImage.convertTo(ir8, CV_8U, 256.0 / 3500, 0.0);
|
|
|
|
imshow("IR image", ir8);
|
|
|
|
}
|
2011-01-22 01:00:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( waitKey( 30 ) >= 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|