-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
135 lines (102 loc) · 3.58 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "OpenNI.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
// OpenCV Header
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
using namespace openni;
/*
* Depth Max/Min adjust
*
*/
cv::Mat adjustDepth(const cv::Mat& inImage)
{
// from https://orbbec3d.com/product-astra/
// Astra S has a depth in the range 0.35m to 2.5m
//int maxDepth = 2500;
//int minDepth = 350; // in mm
// Astra Pro has a depth in the range 0.6m to 8m
int maxDepth = 3000;
int minDepth = 600; // in mm
cv::Mat retImage = inImage;
for(int j = 0; j < retImage.rows; j++)
for(int i = 0; i < retImage.cols; i++) {
if (retImage.at<ushort>(j, i) < maxDepth) //convert points in range
retImage.at<ushort>(j, i) = maxDepth - (retImage.at<ushort>(j, i) - minDepth);
else
retImage.at<ushort>(j, i) = 10; //dark it
}
return retImage;
}
int main(int argc, char* argv[])
{
auto version = openni::OpenNI::getVersion();
std::cout << "OpenNI " << version.major << "." << version.minor << "." <<
version.maintenance << "." << version.build << std::endl;
VideoFrameRef oniDepthImg;
OpenNI::shutdown();
Status result = STATUS_OK;
result = OpenNI::initialize();
// open device
Device device;
result = device.open( openni::ANY_DEVICE );
//// create depth stream
VideoStream oniDepthStream;
result = oniDepthStream.create( device, openni::SENSOR_DEPTH );
// set depth video mode
VideoMode modeDepth;
modeDepth.setResolution( 640, 480 );
modeDepth.setFps( 30 );
modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );
oniDepthStream.setVideoMode(modeDepth);
//oniDepthStream.setMirroringEnabled(true);
device.setImageRegistrationMode(ImageRegistrationMode::IMAGE_REGISTRATION_DEPTH_TO_COLOR);
/*
//Set UVC camera capture
VideoCapture cap;
cap.open(1); //Astra on USB list #1
if(!cap.isOpened())
return false;
cap.set(CAP_PROP_FRAME_WIDTH,720); //Opencv4
cap.set(CAP_PROP_FRAME_HEIGHT,480);//Opencv4
*/
// start depth stream
result = oniDepthStream.start();
while( true ) {
/*
//Capture UVC
//Mat frame,rgb;
//cap>>frame;
//if(frame.empty())
// break;
//cvtColor(frame,rgb,CV_RGB2GRAY);
//cvtColor(frame,rgb, COLOR_RGB2GRAY);//Opencv4
*/
if ( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK ) {
cv::Mat cDepthImg( oniDepthImg.getHeight(), oniDepthImg.getWidth(),
CV_16UC1, (void*)oniDepthImg.getData() );
//cv::Mat c8BitDepth,c24Bit;
//cDepthImg.convertTo( c8BitDepth, CV_8U, 255.0 / (1000) );
//Mat dst;
//addWeighted(c8BitDepth,0.5,rgb,0.5,0,dst);
//cv::imshow( "Astra Depth", dst );
//cv::imshow( "Astra Depth", cDepthImg);
cv::Mat adjustedDepth = adjustDepth(cDepthImg);
cv::Mat dispImage;
adjustedDepth.convertTo(dispImage, CV_8UC1, 255.0f/4000); //Darker
cv::imshow("Adjusted Depth", dispImage);
}
waitKey(10);
}
oniDepthStream.destroy();
device.close();
OpenNI::shutdown();
//return 0;
}