Home | History | Annotate | Download | only in cpp
      1 // testOpenCVCam.cpp : Defines the entry point for the console application.
      2 //
      3 
      4 #include "opencv2/videoio/videoio.hpp"
      5 #include "opencv2/highgui/highgui.hpp"
      6 
      7 #include <iostream>
      8 
      9 using namespace cv;
     10 using namespace std;
     11 
     12 static bool g_printStreamSetting        = false;
     13 static int g_imageStreamProfileIdx      = -1;
     14 static int g_depthStreamProfileIdx      = -1;
     15 static bool g_irStreamShow              = false;
     16 static double g_imageBrightness         = -DBL_MAX;
     17 static double g_imageContrast           = -DBL_MAX;
     18 static bool g_printTiming               = false;
     19 static bool g_showClosedPoint           = false;
     20 
     21 
     22 static int g_closedDepthPoint[2];
     23 
     24 static void printUsage(const char *arg0)
     25 {
     26     const char *filename = arg0;
     27     while (*filename)
     28         filename++;
     29     while ((arg0 <= filename) && ('\\' != *filename) && ('/' != *filename))
     30         filename--;
     31     filename++;
     32 
     33     cout << "This program demonstrates usage of camera supported\nby Intel Perceptual computing SDK." << endl << endl;
     34     cout << "usage: " << filename << "[-ps] [-isp IDX] [-dsp IDX]\n [-ir] [-imb VAL] [-imc VAL]" << endl << endl;
     35     cout << "   -ps,            print streams setting and profiles" << endl;
     36     cout << "   -isp IDX,       set profile index of the image stream" << endl;
     37     cout << "   -dsp IDX,       set profile index of the depth stream" << endl;
     38     cout << "   -ir,            show data from IR stream" << endl;
     39     cout << "   -imb VAL,       set brighness value for a image stream" << endl;
     40     cout << "   -imc VAL,       set contrast value for a image stream" << endl;
     41     cout << "   -pts,           print frame index and frame time" << endl;
     42     cout << "   --show-closed,  print frame index and frame time" << endl;
     43     cout <<  endl;
     44 }
     45 
     46 static void parseCMDLine(int argc, char* argv[])
     47 {
     48     if( argc == 1 )
     49     {
     50         printUsage(argv[0]);
     51     }
     52     else
     53     {
     54         for( int i = 1; i < argc; i++ )
     55         {
     56             if ((0 == strcmp(argv[i], "--help")) || (0 == strcmp( argv[i], "-h")))
     57             {
     58                 printUsage(argv[0]);
     59                 exit(0);
     60             }
     61             else if ((0 == strcmp( argv[i], "--print-streams")) || (0 == strcmp( argv[i], "-ps")))
     62             {
     63                 g_printStreamSetting = true;
     64             }
     65             else if ((0 == strcmp( argv[i], "--image-stream-prof")) || (0 == strcmp( argv[i], "-isp")))
     66             {
     67                 g_imageStreamProfileIdx = atoi(argv[++i]);
     68             }
     69             else if ((0 == strcmp( argv[i], "--depth-stream-prof")) || (0 == strcmp( argv[i], "-dsp")))
     70             {
     71                 g_depthStreamProfileIdx = atoi(argv[++i]);
     72             }
     73             else if (0 == strcmp( argv[i], "-ir"))
     74             {
     75                 g_irStreamShow = true;
     76             }
     77             else if (0 == strcmp( argv[i], "-imb"))
     78             {
     79                 g_imageBrightness = atof(argv[++i]);
     80             }
     81             else if (0 == strcmp( argv[i], "-imc"))
     82             {
     83                 g_imageContrast = atof(argv[++i]);
     84             }
     85             else if (0 == strcmp(argv[i], "-pts"))
     86             {
     87                 g_printTiming = true;
     88             }
     89             else if (0 == strcmp(argv[i], "--show-closed"))
     90             {
     91                 g_showClosedPoint = true;
     92             }
     93             else
     94             {
     95                 cout << "Unsupported command line argument: " << argv[i] << "." << endl;
     96                 exit(-1);
     97             }
     98         }
     99         if (g_showClosedPoint && (-1 == g_depthStreamProfileIdx))
    100         {
    101             cerr << "For --show-closed depth profile has be selected" << endl;
    102             exit(-1);
    103         }
    104     }
    105 }
    106 
    107 static void printStreamProperties(VideoCapture &capture)
    108 {
    109     size_t profilesCount = (size_t)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_COUNT);
    110     cout << "Image stream." << endl;
    111     cout << "  Brightness = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS) << endl;
    112     cout << "  Contrast = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_CONTRAST) << endl;
    113     cout << "  Saturation = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_SATURATION) << endl;
    114     cout << "  Hue = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_HUE) << endl;
    115     cout << "  Gamma = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_GAMMA) << endl;
    116     cout << "  Sharpness = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_SHARPNESS) << endl;
    117     cout << "  Gain = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_GAIN) << endl;
    118     cout << "  Backligh = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BACKLIGHT) << endl;
    119     cout << "Image streams profiles:" << endl;
    120     for (size_t i = 0; i < profilesCount; i++)
    121     {
    122         capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)i);
    123         cout << "  Profile[" << i << "]: ";
    124         cout << "width = " <<
    125             (int)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FRAME_WIDTH);
    126         cout << ", height = " <<
    127             (int)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FRAME_HEIGHT);
    128         cout << ", fps = " <<
    129             capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FPS);
    130         cout << endl;
    131     }
    132 
    133     profilesCount = (size_t)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_COUNT);
    134     cout << "Depth stream." << endl;
    135     cout << "  Low confidence value = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_LOW_CONFIDENCE_VALUE) << endl;
    136     cout << "  Saturation value = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE) << endl;
    137     cout << "  Confidence threshold = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_CONFIDENCE_THRESHOLD) << endl;
    138     cout << "  Focal length = (" << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_HORZ) << ", "
    139         << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_VERT) << ")" << endl;
    140     cout << "Depth streams profiles:" << endl;
    141     for (size_t i = 0; i < profilesCount; i++)
    142     {
    143         capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)i);
    144         cout << "  Profile[" << i << "]: ";
    145         cout << "width = " <<
    146             (int)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FRAME_WIDTH);
    147         cout << ", height = " <<
    148             (int)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FRAME_HEIGHT);
    149         cout << ", fps = " <<
    150             capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FPS);
    151         cout << endl;
    152     }
    153 }
    154 
    155 static void imshowImage(const char *winname, Mat &image, VideoCapture &capture)
    156 {
    157     if (g_showClosedPoint)
    158     {
    159         Mat uvMap;
    160         if (capture.retrieve(uvMap, CAP_INTELPERC_UVDEPTH_MAP))
    161         {
    162             float *uvmap = (float *)uvMap.ptr() + 2 * (g_closedDepthPoint[0] * uvMap.cols + g_closedDepthPoint[1]);
    163             int x = (int)((*uvmap) * image.cols); uvmap++;
    164             int y = (int)((*uvmap) * image.rows);
    165 
    166             if ((0 <= x) && (0 <= y))
    167             {
    168                 static const int pointSize = 4;
    169                 for (int row = y; row < min(y + pointSize, image.rows); row++)
    170                 {
    171                     uchar* ptrDst = image.ptr(row) + x * 3 + 2;//+2 -> Red
    172                     for (int col = 0; col < min(pointSize, image.cols - x); col++, ptrDst+=3)
    173                     {
    174                         *ptrDst = 255;
    175                     }
    176                 }
    177             }
    178         }
    179     }
    180     imshow(winname, image);
    181 }
    182 static void imshowIR(const char *winname, Mat &ir)
    183 {
    184     Mat image;
    185     if (g_showClosedPoint)
    186     {
    187         image.create(ir.rows, ir.cols, CV_8UC3);
    188         for (int row = 0; row < ir.rows; row++)
    189         {
    190             uchar* ptrDst = image.ptr(row);
    191             short* ptrSrc = (short*)ir.ptr(row);
    192             for (int col = 0; col < ir.cols; col++, ptrSrc++)
    193             {
    194                 uchar val = (uchar) ((*ptrSrc) >> 2);
    195                 *ptrDst = val;  ptrDst++;
    196                 *ptrDst = val;  ptrDst++;
    197                 *ptrDst = val;  ptrDst++;
    198             }
    199         }
    200 
    201         static const int pointSize = 4;
    202         for (int row = g_closedDepthPoint[0]; row < min(g_closedDepthPoint[0] + pointSize, image.rows); row++)
    203         {
    204             uchar* ptrDst = image.ptr(row) + g_closedDepthPoint[1] * 3 + 2;//+2 -> Red
    205             for (int col = 0; col < min(pointSize, image.cols - g_closedDepthPoint[1]); col++, ptrDst+=3)
    206             {
    207                 *ptrDst = 255;
    208             }
    209         }
    210     }
    211     else
    212     {
    213         image.create(ir.rows, ir.cols, CV_8UC1);
    214         for (int row = 0; row < ir.rows; row++)
    215         {
    216             uchar* ptrDst = image.ptr(row);
    217             short* ptrSrc = (short*)ir.ptr(row);
    218             for (int col = 0; col < ir.cols; col++, ptrSrc++, ptrDst++)
    219             {
    220                 *ptrDst = (uchar) ((*ptrSrc) >> 2);
    221             }
    222         }
    223     }
    224 
    225     imshow(winname, image);
    226 }
    227 static void imshowDepth(const char *winname, Mat &depth, VideoCapture &capture)
    228 {
    229     short lowValue = (short)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_LOW_CONFIDENCE_VALUE);
    230     short saturationValue = (short)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE);
    231 
    232     Mat image;
    233     if (g_showClosedPoint)
    234     {
    235         image.create(depth.rows, depth.cols, CV_8UC3);
    236         for (int row = 0; row < depth.rows; row++)
    237         {
    238             uchar* ptrDst = image.ptr(row);
    239             short* ptrSrc = (short*)depth.ptr(row);
    240             for (int col = 0; col < depth.cols; col++, ptrSrc++)
    241             {
    242                 if ((lowValue == (*ptrSrc)) || (saturationValue == (*ptrSrc)))
    243                 {
    244                     *ptrDst = 0; ptrDst++;
    245                     *ptrDst = 0; ptrDst++;
    246                     *ptrDst = 0; ptrDst++;
    247                 }
    248                 else
    249                 {
    250                     uchar val = (uchar) ((*ptrSrc) >> 2);
    251                     *ptrDst = val;  ptrDst++;
    252                     *ptrDst = val;  ptrDst++;
    253                     *ptrDst = val;  ptrDst++;
    254                 }
    255             }
    256         }
    257 
    258         static const int pointSize = 4;
    259         for (int row = g_closedDepthPoint[0]; row < min(g_closedDepthPoint[0] + pointSize, image.rows); row++)
    260         {
    261             uchar* ptrDst = image.ptr(row) + g_closedDepthPoint[1] * 3 + 2;//+2 -> Red
    262             for (int col = 0; col < min(pointSize, image.cols - g_closedDepthPoint[1]); col++, ptrDst+=3)
    263             {
    264                 *ptrDst = 255;
    265             }
    266         }
    267     }
    268     else
    269     {
    270         image.create(depth.rows, depth.cols, CV_8UC1);
    271         for (int row = 0; row < depth.rows; row++)
    272         {
    273             uchar* ptrDst = image.ptr(row);
    274             short* ptrSrc = (short*)depth.ptr(row);
    275             for (int col = 0; col < depth.cols; col++, ptrSrc++, ptrDst++)
    276             {
    277                 if ((lowValue == (*ptrSrc)) || (saturationValue == (*ptrSrc)))
    278                     *ptrDst = 0;
    279                 else
    280                     *ptrDst = (uchar) ((*ptrSrc) >> 2);
    281             }
    282         }
    283     }
    284     imshow(winname, image);
    285 }
    286 
    287 int main(int argc, char* argv[])
    288 {
    289     parseCMDLine(argc, argv);
    290 
    291     VideoCapture capture;
    292     capture.open(CAP_INTELPERC);
    293     if (!capture.isOpened())
    294     {
    295         cerr << "Can not open a capture object." << endl;
    296         return -1;
    297     }
    298 
    299     if (g_printStreamSetting)
    300         printStreamProperties(capture);
    301 
    302     if (-1 != g_imageStreamProfileIdx)
    303     {
    304         if (!capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)g_imageStreamProfileIdx))
    305         {
    306             cerr << "Can not setup a image stream." << endl;
    307             return -1;
    308         }
    309     }
    310     if (-1 != g_depthStreamProfileIdx)
    311     {
    312         if (!capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)g_depthStreamProfileIdx))
    313         {
    314             cerr << "Can not setup a depth stream." << endl;
    315             return -1;
    316         }
    317     }
    318     else if (g_irStreamShow)
    319     {
    320         if (!capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, 0.0))
    321         {
    322             cerr << "Can not setup a IR stream." << endl;
    323             return -1;
    324         }
    325     }
    326     else
    327     {
    328         cout << "Streams not selected" << endl;
    329         return 0;
    330     }
    331 
    332     //Setup additional properies only after set profile of the stream
    333     if ( (-10000.0 < g_imageBrightness) && (g_imageBrightness < 10000.0))
    334         capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS, g_imageBrightness);
    335     if ( (0 < g_imageContrast) && (g_imageContrast < 10000.0))
    336         capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS, g_imageContrast);
    337 
    338     int frame = 0;
    339     for(;;frame++)
    340     {
    341         Mat bgrImage;
    342         Mat depthImage;
    343         Mat irImage;
    344 
    345         if (!capture.grab())
    346         {
    347             cout << "Can not grab images." << endl;
    348             return -1;
    349         }
    350 
    351         if ((-1 != g_depthStreamProfileIdx) && (capture.retrieve(depthImage, CAP_INTELPERC_DEPTH_MAP)))
    352         {
    353             if (g_showClosedPoint)
    354             {
    355                 double minVal = 0.0; double maxVal = 0.0;
    356                 minMaxIdx(depthImage, &minVal, &maxVal, g_closedDepthPoint);
    357             }
    358             imshowDepth("depth image", depthImage, capture);
    359         }
    360         if ((g_irStreamShow) && (capture.retrieve(irImage, CAP_INTELPERC_IR_MAP)))
    361             imshowIR("ir image", irImage);
    362         if ((-1 != g_imageStreamProfileIdx) && (capture.retrieve(bgrImage, CAP_INTELPERC_IMAGE)))
    363             imshowImage("color image", bgrImage, capture);
    364 
    365         if (g_printTiming)
    366         {
    367             cout << "Image frame: " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_POS_FRAMES)
    368                  << ", Depth(IR) frame: " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_POS_FRAMES) << endl;
    369             cout << "Image frame: " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_POS_MSEC)
    370                  << ", Depth(IR) frame: " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_POS_MSEC) << endl;
    371         }
    372         if( waitKey(30) >= 0 )
    373             break;
    374     }
    375 
    376     return 0;
    377 }
    378