refactored OpenNI integration
This commit is contained in:
@@ -6,9 +6,22 @@
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
#define COLORIZED_DISP 1
|
||||
#define IMAGE_GENERATOR_VGA_30HZ 1
|
||||
#define FIXED_MAX_DISP 0
|
||||
void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates usage of Kinect sensor.\n"
|
||||
"The user gets some of the supported output images.\n"
|
||||
"\nAll supported output map types:\n"
|
||||
"1.) Data given from depth generator\n"
|
||||
" OPENNI_DEPTH_MAP - depth values in mm (CV_16UC1)\n"
|
||||
" OPENNI_POINT_CLOUD_MAP - XYZ in meters (CV_32FC3)\n"
|
||||
" OPENNI_DISPARITY_MAP - disparity in pixels (CV_8UC1)\n"
|
||||
" OPENNI_DISPARITY_MAP_32F - disparity in pixels (CV_32FC1)\n"
|
||||
" OPENNI_VALID_DEPTH_MASK - mask of valid pixels (not ocluded, not shaded etc.) (CV_8UC1)\n"
|
||||
"2.) Data given from RGB image generator\n"
|
||||
" OPENNI_BGR_IMAGE - color image (CV_8UC3)\n"
|
||||
" OPENNI_GRAY_IMAGE - gray image (CV_8UC1)\n"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
|
||||
{
|
||||
@@ -59,45 +72,98 @@ void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=
|
||||
}
|
||||
}
|
||||
|
||||
void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates usage of Kinect sensor.\n"
|
||||
"The user gets some of the supported output images.\n"
|
||||
"\nAll supported output map types:\n"
|
||||
"1.) Data given from depth generator\n"
|
||||
" OPENNI_DEPTH_MAP - depth values in mm (CV_16UC1)\n"
|
||||
" OPENNI_POINT_CLOUD_MAP - XYZ in meters (CV_32FC3)\n"
|
||||
" OPENNI_DISPARITY_MAP - disparity in pixels (CV_8UC1)\n"
|
||||
" OPENNI_DISPARITY_MAP_32F - disparity in pixels (CV_32FC1)\n"
|
||||
" OPENNI_VALID_DEPTH_MASK - mask of valid pixels (not ocluded, not shaded etc.) (CV_8UC1)\n"
|
||||
"2.) Data given from RGB image generator\n"
|
||||
" OPENNI_BGR_IMAGE - color image (CV_8UC3)\n"
|
||||
" OPENNI_GRAY_IMAGE - gray image (CV_8UC1)\n"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
float getMaxDisparity( VideoCapture& capture )
|
||||
{
|
||||
#if FIXED_MAX_DISP
|
||||
const int minDistance = 400; // mm
|
||||
float b = capture.get( OPENNI_DEPTH_GENERATOR_BASELINE ); // mm
|
||||
float F = capture.get( OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH ); // pixels
|
||||
float b = capture.get( CV_CAP_OPENNI_DEPTH_GENERATOR_BASELINE ); // mm
|
||||
float F = capture.get( CV_CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH ); // pixels
|
||||
return b * F / minDistance;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void printCommandLineParams()
|
||||
{
|
||||
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 << "-sxga SXGA resolution of image? (0 or 1; 0 by default) Ignored if rgb image or gray image are not selected to show." << endl;
|
||||
cout << " If -sxga is 0 then vga resolution will be set by default." << endl;
|
||||
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;
|
||||
cout << " determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently)?" << endl ;
|
||||
cout << " By default -m 01010 i.e. disparity map and rgb image will be shown." << endl ;
|
||||
}
|
||||
|
||||
void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, bool& isSetSXGA, bool retrievedImageFlags[] )
|
||||
{
|
||||
// set defaut values
|
||||
isColorizeDisp = true;
|
||||
isSetSXGA = false;
|
||||
retrievedImageFlags[0] = false;
|
||||
retrievedImageFlags[1] = true;
|
||||
retrievedImageFlags[2] = false;
|
||||
retrievedImageFlags[3] = true;
|
||||
retrievedImageFlags[4] = false;
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
help();
|
||||
}
|
||||
else
|
||||
{
|
||||
for( int i = 1; i < argc; i++ )
|
||||
{
|
||||
if( !strcmp( argv[i], "--help" ) )
|
||||
{
|
||||
printCommandLineParams();
|
||||
exit(0);
|
||||
}
|
||||
else if( !strcmp( argv[i], "-cd" ) )
|
||||
{
|
||||
isColorizeDisp = atoi(argv[++i]) == 0 ? false : true;
|
||||
}
|
||||
else if( !strcmp( argv[i], "-fmd" ) )
|
||||
{
|
||||
isFixedMaxDisp = atoi(argv[++i]) == 0 ? false : true;
|
||||
}
|
||||
else if( !strcmp( argv[i], "-sxga" ) )
|
||||
{
|
||||
isSetSXGA = atoi(argv[++i]) == 0 ? false : true;
|
||||
}
|
||||
else if( !strcmp( argv[i], "-m" ) )
|
||||
{
|
||||
string mask( argv[++i] );
|
||||
if( mask.size() != 5)
|
||||
CV_Error( CV_StsBadArg, "Incorrect length of -m argument string" );
|
||||
int val = atoi(mask.c_str());
|
||||
|
||||
int l = 100000, r = 10000, sum = 0;
|
||||
for( int i = 0; i < 5; i++ )
|
||||
{
|
||||
retrievedImageFlags[i] = ((val % l) / r ) == 0 ? false : true;
|
||||
l /= 10; r /= 10;
|
||||
if( retrievedImageFlags[i] ) sum++;
|
||||
}
|
||||
|
||||
if( sum == 0 )
|
||||
{
|
||||
cout << "No one output image is selected." << endl;
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* To work with Kinect the user must install OpenNI library and PrimeSensorModule for OpenNI and
|
||||
* configure OpenCV with WITH_OPENNI flag is ON (using CMake).
|
||||
*/
|
||||
int main()
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
help();
|
||||
bool isColorizeDisp, isFixedMaxDisp, isSetSXGA;
|
||||
bool retrievedImageFlags[5];
|
||||
parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, isSetSXGA, retrievedImageFlags );
|
||||
|
||||
cout << "Kinect opening ..." << endl;
|
||||
VideoCapture capture(0); // or CV_CAP_OPENNI
|
||||
VideoCapture capture( CV_CAP_OPENNI );
|
||||
cout << "done." << endl;
|
||||
|
||||
if( !capture.isOpened() )
|
||||
@@ -106,23 +172,22 @@ int main()
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if IMAGE_GENERATOR_VGA_30HZ
|
||||
capture.set( OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, OPENNI_VGA_30HZ ); // default
|
||||
#else
|
||||
capture.set( OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, OPENNI_SXGA_15HZ );
|
||||
#endif
|
||||
if( isSetSXGA )
|
||||
capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_15HZ );
|
||||
else
|
||||
capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_VGA_30HZ ); // default
|
||||
|
||||
// Print some avalible Kinect settings.
|
||||
cout << "\nDepth generator output mode:" << endl <<
|
||||
"FRAME_WIDTH " << capture.get( CV_CAP_PROP_FRAME_WIDTH ) << endl <<
|
||||
"FRAME_HEIGHT " << capture.get( CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
|
||||
"FRAME_MAX_DEPTH " << capture.get( OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
|
||||
"FRAME_MAX_DEPTH " << capture.get( CV_CAP_PROP_OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
|
||||
"FPS " << capture.get( CV_CAP_PROP_FPS ) << endl;
|
||||
|
||||
cout << "\nImage generator output mode:" << endl <<
|
||||
"FRAME_WIDTH " << capture.get( OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_WIDTH ) << endl <<
|
||||
"FRAME_HEIGHT " << capture.get( OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
|
||||
"FPS " << capture.get( OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FPS ) << endl;
|
||||
"FRAME_WIDTH " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_WIDTH ) << endl <<
|
||||
"FRAME_HEIGHT " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_HEIGHT ) << endl <<
|
||||
"FPS " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FPS ) << endl;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
@@ -139,33 +204,36 @@ int main()
|
||||
}
|
||||
else
|
||||
{
|
||||
if( capture.retrieve( depthMap, OPENNI_DEPTH_MAP ) )
|
||||
if( retrievedImageFlags[0] && capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ) )
|
||||
{
|
||||
const float scaleFactor = 0.05f;
|
||||
Mat show; depthMap.convertTo( show, CV_8UC1, scaleFactor );
|
||||
imshow( "depth map", show );
|
||||
}
|
||||
|
||||
if( capture.retrieve( disparityMap, OPENNI_DISPARITY_MAP ) )
|
||||
if( retrievedImageFlags[1] && capture.retrieve( disparityMap, CV_CAP_OPENNI_DISPARITY_MAP ) )
|
||||
{
|
||||
#if COLORIZED_DISP // colorized disparity for more visibility
|
||||
Mat colorDisparityMap;
|
||||
colorizeDisparity( disparityMap, colorDisparityMap, getMaxDisparity( capture ) );
|
||||
Mat validColorDisparityMap;
|
||||
colorDisparityMap.copyTo( validColorDisparityMap, disparityMap != OPENNI_BAD_DISP_VAL );
|
||||
imshow( "colorized disparity map", validColorDisparityMap );
|
||||
#else // original disparity
|
||||
imshow( "original disparity map", disparityMap );
|
||||
#endif
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
if( capture.retrieve( validDepthMap, OPENNI_VALID_DEPTH_MASK ) )
|
||||
if( retrievedImageFlags[2] && capture.retrieve( validDepthMap, CV_CAP_OPENNI_VALID_DEPTH_MASK ) )
|
||||
imshow( "valid depth mask", validDepthMap );
|
||||
|
||||
if( capture.retrieve( bgrImage, OPENNI_BGR_IMAGE ) )
|
||||
if( retrievedImageFlags[3] && capture.retrieve( bgrImage, CV_CAP_OPENNI_BGR_IMAGE ) )
|
||||
imshow( "rgb image", bgrImage );
|
||||
|
||||
if( capture.retrieve( grayImage, OPENNI_GRAY_IMAGE ) )
|
||||
if( retrievedImageFlags[4] && capture.retrieve( grayImage, CV_CAP_OPENNI_GRAY_IMAGE ) )
|
||||
imshow( "gray image", grayImage );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user