openni_capture.cpp 11.3 KB
Newer Older
1 2 3 4 5 6 7 8
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

9
static void help()
10
{
11
        cout << "\nThis program demonstrates usage of depth sensors (Kinect, XtionPRO,...).\n"
12 13 14 15 16 17 18 19 20 21 22 23 24
                        "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;
}
25

26
static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
27 28 29 30
{
    CV_Assert( !gray.empty() );
    CV_Assert( gray.type() == CV_8UC1 );

31 32 33 34 35
    if( maxDisp <= 0 )
    {
        maxDisp = 0;
        minMaxLoc( gray, 0, &maxDisp );
    }
36 37

    rgb.create( gray.size(), CV_8UC3 );
38 39 40 41
    rgb = Scalar::all(0);
    if( maxDisp < 1 )
        return;

42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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;
56 57

            if( hi == 0 ) //R = V,  G = t,  B = p
58
                res = Point3f( p, t, V );
59
            if( hi == 1 ) // R = q, G = V,  B = p
60
                res = Point3f( p, V, q );
61
            if( hi == 2 ) // R = p, G = V,  B = t
62
                res = Point3f( t, V, p );
63
            if( hi == 3 ) // R = p, G = q,  B = V
64
                res = Point3f( V, q, p );
65
            if( hi == 4 ) // R = t, G = p,  B = V
66
                res = Point3f( V, p, t );
67
            if( hi == 5 ) // R = V, G = p,  B = q
68 69 70 71 72 73
                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);

74
            rgb.at<Point3_<uchar> >(y,x) = Point3_<uchar>(b, g, r);
75 76 77 78
        }
    }
}

79
static float getMaxDisparity( VideoCapture& capture )
80 81
{
    const int minDistance = 400; // mm
82 83
    float b = (float)capture.get( CV_CAP_OPENNI_DEPTH_GENERATOR_BASELINE ); // mm
    float F = (float)capture.get( CV_CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH ); // pixels
84
    return b * F / minDistance;
85 86
}

87
static void printCommandLineParams()
88 89 90
{
    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;
91 92
    cout << "-mode     image mode: resolution and fps, supported three values:  0 - CV_CAP_OPENNI_VGA_30HZ, 1 - CV_CAP_OPENNI_SXGA_15HZ," << endl;
    cout << "          2 - CV_CAP_OPENNI_SXGA_30HZ (0 by default). Ignored if rgb image or gray image are not selected to show." << endl;
93 94 95
    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 ;
96
    cout << "-r        Filename of .oni video file. The data will grabbed from it." << endl ;
97 98
}

99
static void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[],
100
                       string& filename, bool& isFileReading )
101 102 103
{
    // set defaut values
    isColorizeDisp = true;
104
    isFixedMaxDisp = false;
105
    imageMode = 0;
106

107 108 109 110 111 112
    retrievedImageFlags[0] = false;
    retrievedImageFlags[1] = true;
    retrievedImageFlags[2] = false;
    retrievedImageFlags[3] = true;
    retrievedImageFlags[4] = false;

113 114 115
    filename.clear();
    isFileReading = false;

116 117 118 119 120 121 122 123
    if( argc == 1 )
    {
        help();
    }
    else
    {
        for( int i = 1; i < argc; i++ )
        {
124
            if( !strcmp( argv[i], "--help" ) || !strcmp( argv[i], "-h" ) )
125 126 127 128 129 130 131 132 133 134 135 136
            {
                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;
            }
137
            else if( !strcmp( argv[i], "-mode" ) )
138
            {
139
                imageMode = atoi(argv[++i]);
140 141 142 143 144 145 146 147 148
            }
            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;
149
                for( int j = 0; j < 5; j++ )
150
                {
151
                    retrievedImageFlags[j] = ((val % l) / r ) == 0 ? false : true;
152
                    l /= 10; r /= 10;
153
                    if( retrievedImageFlags[j] ) sum++;
154 155 156 157 158 159 160 161
                }

                if( sum == 0 )
                {
                    cout << "No one output image is selected." << endl;
                    exit(0);
                }
            }
162 163 164 165 166
            else if( !strcmp( argv[i], "-r" ) )
            {
                filename = argv[++i];
                isFileReading = true;
            }
167 168 169 170 171
            else
            {
                cout << "Unsupported command line argument: " << argv[i] << "." << endl;
                exit(-1);
            }
172 173
        }
    }
174 175
}

176
/*
177
 * To work with Kinect or XtionPRO the user must install OpenNI library and PrimeSensorModule for OpenNI and
178 179
 * configure OpenCV with WITH_OPENNI flag is ON (using CMake).
 */
180
int main( int argc, char* argv[] )
181
{
182 183
    bool isColorizeDisp, isFixedMaxDisp;
    int imageMode;
184
    bool retrievedImageFlags[5];
185 186 187
    string filename;
    bool isVideoReading;
    parseCommandLine( argc, argv, isColorizeDisp, isFixedMaxDisp, imageMode, retrievedImageFlags, filename, isVideoReading );
188

189
    cout << "Device opening ..." << endl;
190 191 192 193 194 195
    VideoCapture capture;
    if( isVideoReading )
        capture.open( filename );
    else
        capture.open( CV_CAP_OPENNI );

196 197 198 199 200 201 202 203
    cout << "done." << endl;

    if( !capture.isOpened() )
    {
        cout << "Can not open a capture object." << endl;
        return -1;
    }

204
    if( !isVideoReading )
205
    {
206 207 208 209 210 211 212 213 214 215 216 217
        bool modeRes=false;
        switch ( imageMode )
        {
            case 0:
                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_VGA_30HZ );
                break;
            case 1:
                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_15HZ );
                break;
            case 2:
                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_SXGA_30HZ );
                break;
218 219 220 221 222 223 224
                //The following modes are only supported by the Xtion Pro Live
            case 3:
                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_QVGA_30HZ );
                break;
            case 4:
                modeRes = capture.set( CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CV_CAP_OPENNI_QVGA_60HZ );
                break;
225 226 227 228 229
            default:
                CV_Error( CV_StsBadArg, "Unsupported image mode property.\n");
        }
        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;
230
    }
231

232
    // Print some avalible device settings.
233
    cout << "\nDepth generator output mode:" << endl <<
234 235 236 237 238
            "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( CV_CAP_PROP_OPENNI_FRAME_MAX_DEPTH ) << " mm" << endl <<
            "FPS              " << capture.get( CV_CAP_PROP_FPS ) << endl <<
            "REGISTRATION     " << capture.get( CV_CAP_PROP_OPENNI_REGISTRATION ) << endl;
239
    if( capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR_PRESENT ) )
240 241 242
    {
        cout <<
            "\nImage generator output mode:" << endl <<
243 244 245
            "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;
246
    }
247 248
    else
    {
249
        cout << "\nDevice doesn't contain image generator." << endl;
250 251 252
        if (!retrievedImageFlags[0] && !retrievedImageFlags[1] && !retrievedImageFlags[2])
            return 0;
    }
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    for(;;)
    {
        Mat depthMap;
        Mat validDepthMap;
        Mat disparityMap;
        Mat bgrImage;
        Mat grayImage;

        if( !capture.grab() )
        {
            cout << "Can not grab images." << endl;
            return -1;
        }
        else
        {
269
            if( retrievedImageFlags[0] && capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ) )
270 271 272 273 274 275
            {
                const float scaleFactor = 0.05f;
                Mat show; depthMap.convertTo( show, CV_8UC1, scaleFactor );
                imshow( "depth map", show );
            }

276
            if( retrievedImageFlags[1] && capture.retrieve( disparityMap, CV_CAP_OPENNI_DISPARITY_MAP ) )
277
            {
278 279 280 281 282 283 284 285 286 287 288 289
                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 );
                }
290 291
            }

292
            if( retrievedImageFlags[2] && capture.retrieve( validDepthMap, CV_CAP_OPENNI_VALID_DEPTH_MASK ) )
293
                imshow( "valid depth mask", validDepthMap );
294

295
            if( retrievedImageFlags[3] && capture.retrieve( bgrImage, CV_CAP_OPENNI_BGR_IMAGE ) )
296 297
                imshow( "rgb image", bgrImage );

298
            if( retrievedImageFlags[4] && capture.retrieve( grayImage, CV_CAP_OPENNI_GRAY_IMAGE ) )
299 300 301 302 303 304 305 306 307
                imshow( "gray image", grayImage );
        }

        if( waitKey( 30 ) >= 0 )
            break;
    }

    return 0;
}