1
// testOpenCVCam.cpp : Defines the entry point for the console application.
4
#include "opencv2/videoio/videoio.hpp"
5
#include "opencv2/highgui/highgui.hpp"
12
static bool g_printStreamSetting;
13
static int g_imageStreamProfileIdx;
14
static int g_depthStreamProfileIdx;
15
static bool g_irStreamShow;
16
static double g_imageBrightness;
17
static double g_imageContrast;
18
static bool g_printTiming;
19
static bool g_showClosedPoint;
22
static int g_closedDepthPoint[2];
24
static void printUsage(const char *arg0)
26
const char *filename = arg0;
29
while ((arg0 <= filename) && ('\\' != *filename) && ('/' != *filename))
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;
46
static void parseCMDLine(int argc, char* argv[])
48
cv::CommandLineParser parser(argc, argv,
50
"{ ps print-streams | | }"
51
"{ isp image-stream-prof | -1 | }"
52
"{ dsp depth-stream-prof | -1 | }"
53
"{ir||}{imb||}{imc||}{pts||}{show-closed||}");
59
g_printStreamSetting = parser.has("ps");
60
g_imageStreamProfileIdx = parser.get<int>("isp");
61
g_depthStreamProfileIdx = parser.get<int>("dsp");
62
g_irStreamShow = parser.has("ir");
63
if (parser.has("imb"))
64
g_imageBrightness = parser.get<double>("imb");
66
g_imageBrightness = -DBL_MAX;
67
if (parser.has("imc"))
68
g_imageContrast = parser.get<double>("imc");
70
g_imageContrast = -DBL_MAX;
71
g_printTiming = parser.has("pts");
72
g_showClosedPoint = parser.has("show-closed");
78
if (g_showClosedPoint && (-1 == g_depthStreamProfileIdx))
80
cerr << "For --show-closed depth profile has be selected" << endl;
85
static void printStreamProperties(VideoCapture &capture)
87
size_t profilesCount = (size_t)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_COUNT);
88
cout << "Image stream." << endl;
89
cout << " Brightness = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS) << endl;
90
cout << " Contrast = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_CONTRAST) << endl;
91
cout << " Saturation = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_SATURATION) << endl;
92
cout << " Hue = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_HUE) << endl;
93
cout << " Gamma = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_GAMMA) << endl;
94
cout << " Sharpness = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_SHARPNESS) << endl;
95
cout << " Gain = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_GAIN) << endl;
96
cout << " Backligh = " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BACKLIGHT) << endl;
97
cout << "Image streams profiles:" << endl;
98
for (size_t i = 0; i < profilesCount; i++)
100
capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)i);
101
cout << " Profile[" << i << "]: ";
102
cout << "width = " <<
103
(int)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FRAME_WIDTH);
104
cout << ", height = " <<
105
(int)capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FRAME_HEIGHT);
106
cout << ", fps = " <<
107
capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_FPS);
111
profilesCount = (size_t)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_COUNT);
112
cout << "Depth stream." << endl;
113
cout << " Low confidence value = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_LOW_CONFIDENCE_VALUE) << endl;
114
cout << " Saturation value = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE) << endl;
115
cout << " Confidence threshold = " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_CONFIDENCE_THRESHOLD) << endl;
116
cout << " Focal length = (" << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_HORZ) << ", "
117
<< capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_VERT) << ")" << endl;
118
cout << "Depth streams profiles:" << endl;
119
for (size_t i = 0; i < profilesCount; i++)
121
capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)i);
122
cout << " Profile[" << i << "]: ";
123
cout << "width = " <<
124
(int)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FRAME_WIDTH);
125
cout << ", height = " <<
126
(int)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FRAME_HEIGHT);
127
cout << ", fps = " <<
128
capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_FPS);
133
static void imshowImage(const char *winname, Mat &image, VideoCapture &capture)
135
if (g_showClosedPoint)
138
if (capture.retrieve(uvMap, CAP_INTELPERC_UVDEPTH_MAP))
140
float *uvmap = (float *)uvMap.ptr() + 2 * (g_closedDepthPoint[0] * uvMap.cols + g_closedDepthPoint[1]);
141
int x = (int)((*uvmap) * image.cols); uvmap++;
142
int y = (int)((*uvmap) * image.rows);
144
if ((0 <= x) && (0 <= y))
146
static const int pointSize = 4;
147
for (int row = y; row < min(y + pointSize, image.rows); row++)
149
uchar* ptrDst = image.ptr(row) + x * 3 + 2;//+2 -> Red
150
for (int col = 0; col < min(pointSize, image.cols - x); col++, ptrDst+=3)
158
imshow(winname, image);
160
static void imshowIR(const char *winname, Mat &ir)
163
if (g_showClosedPoint)
165
image.create(ir.rows, ir.cols, CV_8UC3);
166
for (int row = 0; row < ir.rows; row++)
168
uchar* ptrDst = image.ptr(row);
169
short* ptrSrc = (short*)ir.ptr(row);
170
for (int col = 0; col < ir.cols; col++, ptrSrc++)
172
uchar val = (uchar) ((*ptrSrc) >> 2);
173
*ptrDst = val; ptrDst++;
174
*ptrDst = val; ptrDst++;
175
*ptrDst = val; ptrDst++;
179
static const int pointSize = 4;
180
for (int row = g_closedDepthPoint[0]; row < min(g_closedDepthPoint[0] + pointSize, image.rows); row++)
182
uchar* ptrDst = image.ptr(row) + g_closedDepthPoint[1] * 3 + 2;//+2 -> Red
183
for (int col = 0; col < min(pointSize, image.cols - g_closedDepthPoint[1]); col++, ptrDst+=3)
191
image.create(ir.rows, ir.cols, CV_8UC1);
192
for (int row = 0; row < ir.rows; row++)
194
uchar* ptrDst = image.ptr(row);
195
short* ptrSrc = (short*)ir.ptr(row);
196
for (int col = 0; col < ir.cols; col++, ptrSrc++, ptrDst++)
198
*ptrDst = (uchar) ((*ptrSrc) >> 2);
203
imshow(winname, image);
205
static void imshowDepth(const char *winname, Mat &depth, VideoCapture &capture)
207
short lowValue = (short)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_LOW_CONFIDENCE_VALUE);
208
short saturationValue = (short)capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE);
211
if (g_showClosedPoint)
213
image.create(depth.rows, depth.cols, CV_8UC3);
214
for (int row = 0; row < depth.rows; row++)
216
uchar* ptrDst = image.ptr(row);
217
short* ptrSrc = (short*)depth.ptr(row);
218
for (int col = 0; col < depth.cols; col++, ptrSrc++)
220
if ((lowValue == (*ptrSrc)) || (saturationValue == (*ptrSrc)))
222
*ptrDst = 0; ptrDst++;
223
*ptrDst = 0; ptrDst++;
224
*ptrDst = 0; ptrDst++;
228
uchar val = (uchar) ((*ptrSrc) >> 2);
229
*ptrDst = val; ptrDst++;
230
*ptrDst = val; ptrDst++;
231
*ptrDst = val; ptrDst++;
236
static const int pointSize = 4;
237
for (int row = g_closedDepthPoint[0]; row < min(g_closedDepthPoint[0] + pointSize, image.rows); row++)
239
uchar* ptrDst = image.ptr(row) + g_closedDepthPoint[1] * 3 + 2;//+2 -> Red
240
for (int col = 0; col < min(pointSize, image.cols - g_closedDepthPoint[1]); col++, ptrDst+=3)
248
image.create(depth.rows, depth.cols, CV_8UC1);
249
for (int row = 0; row < depth.rows; row++)
251
uchar* ptrDst = image.ptr(row);
252
short* ptrSrc = (short*)depth.ptr(row);
253
for (int col = 0; col < depth.cols; col++, ptrSrc++, ptrDst++)
255
if ((lowValue == (*ptrSrc)) || (saturationValue == (*ptrSrc)))
258
*ptrDst = (uchar) ((*ptrSrc) >> 2);
262
imshow(winname, image);
265
int main(int argc, char* argv[])
267
parseCMDLine(argc, argv);
269
VideoCapture capture;
270
capture.open(CAP_INTELPERC);
271
if (!capture.isOpened())
273
cerr << "Can not open a capture object." << endl;
277
if (g_printStreamSetting)
278
printStreamProperties(capture);
280
if (-1 != g_imageStreamProfileIdx)
282
if (!capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)g_imageStreamProfileIdx))
284
cerr << "Can not setup a image stream." << endl;
288
if (-1 != g_depthStreamProfileIdx)
290
if (!capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, (double)g_depthStreamProfileIdx))
292
cerr << "Can not setup a depth stream." << endl;
296
else if (g_irStreamShow)
298
if (!capture.set(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_INTELPERC_PROFILE_IDX, 0.0))
300
cerr << "Can not setup a IR stream." << endl;
306
cout << "Streams not selected" << endl;
310
//Setup additional properies only after set profile of the stream
311
if ( (-10000.0 < g_imageBrightness) && (g_imageBrightness < 10000.0))
312
capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS, g_imageBrightness);
313
if ( (0 < g_imageContrast) && (g_imageContrast < 10000.0))
314
capture.set(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_BRIGHTNESS, g_imageContrast);
325
cout << "Can not grab images." << endl;
329
if ((-1 != g_depthStreamProfileIdx) && (capture.retrieve(depthImage, CAP_INTELPERC_DEPTH_MAP)))
331
if (g_showClosedPoint)
333
double minVal = 0.0; double maxVal = 0.0;
334
minMaxIdx(depthImage, &minVal, &maxVal, g_closedDepthPoint);
336
imshowDepth("depth image", depthImage, capture);
338
if ((g_irStreamShow) && (capture.retrieve(irImage, CAP_INTELPERC_IR_MAP)))
339
imshowIR("ir image", irImage);
340
if ((-1 != g_imageStreamProfileIdx) && (capture.retrieve(bgrImage, CAP_INTELPERC_IMAGE)))
341
imshowImage("color image", bgrImage, capture);
345
cout << "Image frame: " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_POS_FRAMES)
346
<< ", Depth(IR) frame: " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_POS_FRAMES) << endl;
347
cout << "Image frame: " << capture.get(CAP_INTELPERC_IMAGE_GENERATOR | CAP_PROP_POS_MSEC)
348
<< ", Depth(IR) frame: " << capture.get(CAP_INTELPERC_DEPTH_GENERATOR | CAP_PROP_POS_MSEC) << endl;
350
if( waitKey(30) >= 0 )