Skip to content

Commit f7df3f7

Browse files
larshgfran6co
authored andcommitted
Added viewer to Protonect
Added define for opencv to be able to use either opencv or opengl. Removed dublicate of flextGL .c/.h
1 parent 28ecb12 commit f7df3f7

File tree

8 files changed

+560
-27
lines changed

8 files changed

+560
-27
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ SET(LIBRARY_OUTPUT_PATH ${MY_DIR}/lib)
3939

4040
# dependencies
4141
FIND_PACKAGE(PkgConfig) # try find PKGConfig as it will be used if found
42-
FIND_PACKAGE(LibUSB REQUIRED)
4342
FIND_PACKAGE(OpenCV)
43+
FIND_PACKAGE(LibUSB REQUIRED)
4444
FIND_PACKAGE(TurboJPEG REQUIRED) #does not provide a package-config file
45+
IF(OPENCV_FOUND)
46+
SET(LIBFREENECT2_OPENCV_FOUND 1)
47+
ENDIF()
4548

4649
# Add includes
4750
INCLUDE_DIRECTORIES(
4851
"${MY_DIR}/include"
4952
${LIBFREENECT2_THREADING_INCLUDE_DIR}
50-
# ${OpenCV_INCLUDE_DIRS}
53+
${OpenCV_INCLUDE_DIRS}
5154
${LibUSB_INCLUDE_DIRS}
5255
${TurboJPEG_INCLUDE_DIRS}
5356
)
@@ -103,7 +106,7 @@ SET(SOURCES
103106
)
104107

105108
SET(LIBRARIES
106-
# ${OpenCV_LIBRARIES}
109+
${OpenCV_LIBRARIES}
107110
${LibUSB_LIBRARIES}
108111
${TurboJPEG_LIBRARIES}
109112
${LIBFREENECT2_THREADING_LIBRARIES}
@@ -182,8 +185,21 @@ ENDIF()
182185
MESSAGE("Linking with these libraries: ${LIBRARIES}")
183186
TARGET_LINK_LIBRARIES(freenect2shared ${LIBRARIES})
184187

188+
SET(Protonect_src
189+
Protonect.cpp
190+
)
191+
192+
IF (ENABLE_OPENGL AND OPENGL_FOUND)
193+
LIST(APPEND Protonect_src
194+
viewer.h
195+
viewer.cpp
196+
src/flextGL.c
197+
)
198+
ENDIF()
199+
185200
ADD_EXECUTABLE(Protonect
186-
Protonect.cpp
201+
${Protonect_src}
202+
187203
)
188204

189205
TARGET_LINK_LIBRARIES(Protonect

examples/protonect/Protonect.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828
#include <iostream>
2929
#include <signal.h>
3030

31-
//#include <opencv2/opencv.hpp>
32-
31+
#ifdef LIBFREENECT2_OPENCV_FOUND
32+
#include <opencv2/opencv.hpp>
33+
#endif
3334
#include <libfreenect2/libfreenect2.hpp>
3435
#include <libfreenect2/frame_listener_impl.h>
3536
#include <libfreenect2/threading.h>
3637
#include <libfreenect2/registration.h>
3738
#include <libfreenect2/packet_pipeline.h>
39+
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
40+
#include "viewer.h"
41+
#endif
42+
3843

3944
bool protonect_shutdown = false;
4045

@@ -135,24 +140,34 @@ int main(int argc, char *argv[])
135140

136141
libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams());
137142

143+
Viewer viewer;
144+
viewer.initialize();
145+
138146
while(!protonect_shutdown)
139147
{
140148
listener.waitForNewFrame(frames);
141149
libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color];
142150
libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir];
143151
libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth];
144152

145-
//cv::imshow("rgb", cv::Mat(rgb->height, rgb->width, CV_8UC4, rgb->data));
146-
//cv::imshow("ir", cv::Mat(ir->height, ir->width, CV_32FC1, ir->data) / 20000.0f);
147-
//cv::imshow("depth", cv::Mat(depth->height, depth->width, CV_32FC1, depth->data) / 4500.0f);
148-
149-
registration->apply(rgb,depth,&undistorted,&registered);
153+
registration->apply(rgb, depth, &undistorted, &registered);
150154

151-
//cv::imshow("undistorted", cv::Mat(undistorted.height, undistorted.width, CV_32FC1, undistorted.data) / 4500.0f);
152-
//cv::imshow("registered", cv::Mat(registered.height, registered.width, CV_8UC4, registered.data));
155+
#if defined(LIBFREENECT2_WITH_OPENGL_SUPPORT) && !defined(LIBFREENECT2_OPENCV_FOUND)
156+
viewer.addFrame("RGB", rgb);
157+
viewer.addFrame("ir", ir);
158+
viewer.addFrame("depth", depth);
159+
viewer.addFrame("registered", &registered);
153160

154-
int key = 1;//cv::waitKey(1);
161+
protonect_shutdown = viewer.render();
162+
#else
163+
cv::imshow("rgb", cv::Mat(rgb->height, rgb->width, CV_8UC4, rgb->data));
164+
cv::imshow("ir", cv::Mat(ir->height, ir->width, CV_32FC1, ir->data) / 20000.0f);
165+
cv::imshow("depth", cv::Mat(depth->height, depth->width, CV_32FC1, depth->data) / 4500.0f);
166+
cv::imshow("undistorted", cv::Mat(undistorted.height, undistorted.width, CV_32FC1, undistorted.data) / 4500.0f);
167+
cv::imshow("registered", cv::Mat(registered.height, registered.width, CV_8UC4, registered.data));
168+
int key = cv::waitKey(1);
155169
protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape
170+
#endif
156171

157172
listener.release(frames);
158173
//libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(100));

examples/protonect/include/libfreenect2/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@
4747

4848
#cmakedefine LIBFREENECT2_THREADING_TINYTHREAD
4949

50+
#cmakedefine LIBFREENECT2_OPENCV_FOUND
51+
5052
#endif // LIBFREENECT2_CONFIG_H

examples/protonect/src/cpu_depth_packet_processor.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include <libfreenect2/resource.h>
2929
#include <libfreenect2/protocol/response.h>
3030

31+
#ifdef LIBFREENECT2_OPENCV_FOUND
32+
#include <opencv2/opencv.hpp>
33+
#endif
3134
#include <iostream>
3235
#include <fstream>
3336

@@ -245,12 +248,15 @@ class CpuDepthPacketProcessorImpl
245248

246249
void startTiming()
247250
{
248-
//timing_current_start = cv::getTickCount();
251+
#ifdef LIBFREENECT2_OPENCV_FOUND
252+
timing_current_start = cv::getTickCount();
253+
#endif
249254
}
250255

251256
void stopTiming()
252257
{
253-
//timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
258+
#ifdef LIBFREENECT2_OPENCV_FOUND
259+
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
254260
timing_acc_n += 1.0;
255261

256262
if(timing_acc_n >= 100.0)
@@ -260,6 +266,7 @@ class CpuDepthPacketProcessorImpl
260266
timing_acc = 0.0;
261267
timing_acc_n = 0.0;
262268
}
269+
#endif
263270
}
264271

265272
void newIrFrame()
@@ -889,12 +896,13 @@ void CpuDepthPacketProcessor::process(const DepthPacket &packet)
889896
{
890897
if(listener_ == 0) return;
891898

892-
//impl_->startTiming();
899+
impl_->startTiming();
893900

894901
impl_->ir_frame->timestamp = packet.timestamp;
895902
impl_->depth_frame->timestamp = packet.timestamp;
896903
impl_->ir_frame->sequence = packet.sequence;
897904
impl_->depth_frame->sequence = packet.sequence;
905+
898906
Mat<Vec<float, 9> >
899907
m(424, 512),
900908
m_filtered(424, 512)
@@ -977,7 +985,7 @@ void CpuDepthPacketProcessor::process(const DepthPacket &packet)
977985
impl_->newDepthFrame();
978986
}
979987

980-
//impl_->stopTiming();
988+
impl_->stopTiming();
981989
}
982990

983991
} /* namespace libfreenect2 */

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include <libfreenect2/resource.h>
2929
#include <libfreenect2/protocol/response.h>
3030

31-
//#include <opencv2/opencv.hpp>
31+
#ifdef LIBFREENECT2_OPENCV_FOUND
32+
#include <opencv2/opencv.hpp>
33+
#endif
3234
#include <iostream>
3335
#include <fstream>
3436
#include <sstream>
@@ -545,12 +547,15 @@ class OpenCLDepthPacketProcessorImpl
545547

546548
void startTiming()
547549
{
548-
//timing_current_start = cv::getTickCount();
550+
#ifdef LIBFREENECT2_OPENCV_FOUND
551+
timing_current_start = cv::getTickCount();
552+
#endif
549553
}
550554

551555
void stopTiming()
552556
{
553-
//timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
557+
#ifdef LIBFREENECT2_OPENCV_FOUND
558+
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
554559
timing_acc_n += 1.0;
555560

556561
if(timing_acc_n >= 100.0)
@@ -560,6 +565,7 @@ class OpenCLDepthPacketProcessorImpl
560565
timing_acc = 0.0;
561566
timing_acc_n = 0.0;
562567
}
568+
#endif
563569
}
564570

565571
void newIrFrame()

examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
*/
2626

2727
#include <libfreenect2/rgb_packet_processor.h>
28-
29-
//#include <opencv2/opencv.hpp>
28+
#ifdef LIBFREENECT2_OPENCV_FOUND
29+
#include <opencv2/opencv.hpp>
30+
#endif
3031
#include <turbojpeg.h>
3132
#include <iostream>
3233

@@ -79,12 +80,15 @@ class TurboJpegRgbPacketProcessorImpl
7980

8081
void startTiming()
8182
{
82-
//timing_current_start = cv::getTickCount();
83+
#ifdef LIBFREENECT2_OPENCV_FOUND
84+
timing_current_start = cv::getTickCount();
85+
#endif
8386
}
8487

8588
void stopTiming()
8689
{
87-
//timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
90+
#ifdef LIBFREENECT2_OPENCV_FOUND
91+
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
8892
timing_acc_n += 1.0;
8993

9094
if(timing_acc_n >= 100.0)
@@ -94,6 +98,7 @@ class TurboJpegRgbPacketProcessorImpl
9498
timing_acc = 0.0;
9599
timing_acc_n = 0.0;
96100
}
101+
#endif
97102
}
98103
};
99104

@@ -111,7 +116,7 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet)
111116
{
112117
if(impl_->decompressor != 0 && listener_ != 0)
113118
{
114-
//impl_->startTiming();
119+
impl_->startTiming();
115120

116121
impl_->frame->timestamp = packet.timestamp;
117122
impl_->frame->sequence = packet.sequence;
@@ -130,7 +135,7 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet)
130135
std::cerr << "[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'" << std::endl;
131136
}
132137

133-
//impl_->stopTiming();
138+
impl_->stopTiming();
134139
}
135140
}
136141

0 commit comments

Comments
 (0)