Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd1f9cd

Browse files
authored
Merge pull request #25410 from kaingwade:add_videocapture_depth_sample
Add videocapture_depth.cpp sample #25410 The PR is to combine the examples `videocapture_openni.cpp`, `videocapture_realsense.cpp` and `videocapture_obsensor.cpp` into `videocapture_depth.cpp`. Tested cameras and OS using this sample are listed below: | | Windows 10 | Ubuntu 22.04 | Mac M1 14.3 | |------------------------|--------------|--------------|---------------| | Orbbec Gemini 2 Series | ✓ | ✓ | ✓ | | RealSense D435, D455 | ✓ | ✓ | ✗ | | Kinect, XtionPRO | - | - | - | Note: - OpenNI based cameras (Kinect, XtionPRO) are not tested as I don't have them. - RealSense D435 and D455 don't work on Mac with OpenCV.
1 parent 48b457f commit bd1f9cd

File tree

6 files changed

+176
-398
lines changed

6 files changed

+176
-398
lines changed

doc/tutorials/app/intelperc.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ there are two flags that should be used to set/get property of the needed genera
8484
flag value is assumed by default if neither of the two possible values of the property is set.
8585

8686
For more information please refer to the example of usage
87-
[videocapture_realsense.cpp](https://github.com/opencv/opencv/tree/5.x/samples/cpp/videocapture_realsense.cpp)
87+
[videocapture_depth.cpp](https://github.com/opencv/opencv/tree/5.x/samples/cpp/videocapture_depth.cpp)
8888
in opencv/samples/cpp folder.

doc/tutorials/app/kinect_openni.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ property. The following properties of cameras available through OpenNI interface
140140
- CAP_OPENNI_DEPTH_GENERATOR_REGISTRATION = CAP_OPENNI_DEPTH_GENERATOR + CAP_PROP_OPENNI_REGISTRATION
141141

142142
For more information please refer to the example of usage
143-
[videocapture_openni.cpp](https://github.com/opencv/opencv/tree/5.x/samples/cpp/videocapture_openni.cpp) in
143+
[videocapture_depth.cpp](https://github.com/opencv/opencv/tree/5.x/samples/cpp/videocapture_depth.cpp) in
144144
opencv/samples/cpp folder.

samples/cpp/videocapture_depth.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#include "opencv2/videoio.hpp"
2+
#include "opencv2/highgui.hpp"
3+
#include "opencv2/imgproc.hpp"
4+
5+
#include <iostream>
6+
7+
using namespace cv;
8+
9+
static void colorizeDisparity(const Mat& gray, Mat& rgb, double maxDisp=-1.f)
10+
{
11+
CV_Assert(!gray.empty());
12+
CV_Assert(gray.type() == CV_8UC1);
13+
14+
if(maxDisp <= 0)
15+
{
16+
maxDisp = 0;
17+
minMaxLoc(gray, nullptr, &maxDisp);
18+
}
19+
20+
rgb = Mat::zeros(gray.size(), CV_8UC3);
21+
if(maxDisp < 1)
22+
return;
23+
24+
Mat tmp;
25+
convertScaleAbs(gray, tmp, 255.f / maxDisp);
26+
applyColorMap(tmp, rgb, COLORMAP_JET);
27+
}
28+
29+
static float getMaxDisparity(VideoCapture& capture, int minDistance)
30+
{
31+
float b = (float)capture.get(CAP_OPENNI_DEPTH_GENERATOR_BASELINE); // mm
32+
float F = (float)capture.get(CAP_OPENNI_DEPTH_GENERATOR_FOCAL_LENGTH); // pixels
33+
return b * F / minDistance;
34+
}
35+
36+
static void colorizeDepth(const Mat& depth, Mat& rgb)
37+
{
38+
CV_Assert(!depth.empty());
39+
40+
normalize(depth, rgb, 0, 255, NORM_MINMAX, CV_8UC1);
41+
applyColorMap(rgb, rgb, COLORMAP_JET);
42+
}
43+
44+
const char* keys = "{type t | | Camera Type: OpenNI, RealSense, Orbbec}"
45+
"{dist d | 400 | The minimum measurable distance in milimeter between the camera and the object}"
46+
"{help h | | Help}";
47+
48+
const char* about =
49+
"\nThis example demostrates how to get data from 3D cameras via OpenCV.\n"
50+
"Currently OpenCV supports 3 types of 3D cameras:\n"
51+
"1. Depth sensors compatible with OpenNI (Kinect, XtionPRO). "
52+
"Users must install OpenNI library and PrimeSensorModule for OpenNI and configure OpenCV with WITH_OPENNI flag ON in CMake.\n"
53+
"2. Depth sensors compatible with Intel® RealSense SDK (RealSense). "
54+
"Users must install Intel RealSense SDK 2.0 and configure OpenCV with WITH_LIBREALSENSE flag ON in CMake.\n"
55+
"3. Orbbec UVC depth sensors. "
56+
"For the use of OpenNI based Orbbec cameras, please refer to the example openni_orbbec_astra.cpp\n";
57+
58+
int main(int argc, char *argv[])
59+
{
60+
CommandLineParser parser(argc, argv, keys);
61+
parser.about(about);
62+
63+
if(parser.has("help"))
64+
{
65+
parser.printMessage();
66+
return 0;
67+
}
68+
69+
if (parser.has("type"))
70+
{
71+
int backend;
72+
int flagDepth, flagBGR, flagIR;
73+
bool hasDisparity = false;
74+
75+
String camType = parser.get<String>("type");
76+
if (camType == "OpenNI")
77+
{
78+
backend = CAP_OPENNI2;
79+
flagDepth = CAP_OPENNI_DEPTH_MAP;
80+
flagBGR = CAP_OPENNI_BGR_IMAGE;
81+
flagIR = CAP_OPENNI_IR_IMAGE;
82+
hasDisparity = true;
83+
}
84+
else if (camType == "RealSense")
85+
{
86+
backend = CAP_INTELPERC;
87+
flagDepth = CAP_INTELPERC_DEPTH_MAP;
88+
flagBGR = CAP_INTELPERC_IMAGE;
89+
flagIR = CAP_INTELPERC_IR_MAP;
90+
}
91+
else if (camType == "Orbbec")
92+
{
93+
backend = CAP_OBSENSOR;
94+
flagDepth = CAP_OBSENSOR_DEPTH_MAP;
95+
flagBGR = CAP_OBSENSOR_BGR_IMAGE;
96+
flagIR = CAP_OBSENSOR_IR_IMAGE; // Note output IR stream is not implemented for now.
97+
}
98+
else
99+
{
100+
std::cerr << "Invalid input of camera type." << std::endl;
101+
return -1;
102+
}
103+
104+
VideoCapture capture(backend);
105+
if(!capture.isOpened())
106+
{
107+
std::cerr << "Fail to open depth camera." << std::endl;
108+
return -1;
109+
}
110+
111+
if (camType == "OpenNI")
112+
{
113+
capture.set(CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, CAP_OPENNI_VGA_30HZ);
114+
capture.set(CAP_OPENNI_DEPTH_GENERATOR_PRESENT, true);
115+
capture.set(CAP_OPENNI_IMAGE_GENERATOR_PRESENT, true);
116+
capture.set(CAP_OPENNI_IR_GENERATOR_PRESENT, true);
117+
}
118+
119+
Mat depthMap;
120+
Mat bgrImage;
121+
Mat irImage;
122+
Mat disparityMap;
123+
124+
for(;;)
125+
{
126+
if(capture.grab())
127+
{
128+
if(capture.retrieve(depthMap, flagDepth))
129+
{
130+
Mat colorDepthMap;
131+
colorizeDepth(depthMap, colorDepthMap);
132+
imshow("Colorized Depth Map", colorDepthMap);
133+
}
134+
135+
if(capture.retrieve(bgrImage, flagBGR))
136+
imshow("RGB Image", bgrImage);
137+
138+
if(capture.retrieve(irImage, flagIR))
139+
{
140+
if (camType == "OpenNI")
141+
{
142+
Mat ir8;
143+
irImage.convertTo(ir8, CV_8U, 256.0 / 3500, 0.0);
144+
imshow("Infrared Image", ir8);
145+
}
146+
else
147+
imshow("Infrared Image", irImage);
148+
}
149+
150+
if (hasDisparity)
151+
{
152+
int minDist = parser.get<int>("dist"); // mm
153+
if(capture.retrieve(disparityMap, CAP_OPENNI_DISPARITY_MAP))
154+
{
155+
Mat colorDisparityMap;
156+
colorizeDisparity(disparityMap, colorDisparityMap, getMaxDisparity(capture, minDist));
157+
colorDisparityMap.setTo(Scalar(0, 0, 0), disparityMap == 0);
158+
imshow("Colorized Disparity Map", colorDisparityMap);
159+
}
160+
}
161+
}
162+
163+
if(waitKey(30) >= 0)
164+
break;
165+
}
166+
}
167+
else
168+
{
169+
parser.printMessage();
170+
return 0;
171+
}
172+
173+
return 0;
174+
}

samples/cpp/videocapture_obsensor.cpp

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)