Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include "highgui.h"
  2. #include <stdio.h>
  3.  
  4. #include <string.h>
  5. #include <math.h>
  6. #include <libusb.h>
  7.  
  8. #include "libfreenect.h"
  9.  
  10. #include <pthread.h>
  11.  
  12. #include <GL/glut.h>
  13. #include <GL/gl.h>
  14. #include <GL/glu.h>
  15.  
  16. #define CV_NO_BACKWARD_COMPATIBILITY
  17.  
  18. #include <opencv/cv.h>
  19. #include <opencv/highgui.h>
  20.  
  21.  
  22. // written by Arne Bernin <arne@alamut.de>
  23. // Nov 13, 2010
  24.  
  25.  
  26. /*
  27.  * Makefile for ubuntu, assumes that libfreenect.a is in /usr/local/lib, and libfreenect.h is in /usr/local/include
  28.  *
  29.  * make sure that libfreenect.h includes the extern C stuff:
  30.  *
  31.  
  32.  
  33. ***************************************************************************************************************************
  34. * libfreenect.h
  35. ***************************************************************************************************************************
  36.  
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40.  
  41. void cams_init(libusb_device_handle *d, depthcb depth_cb, rgbcb rgb_cb);
  42.  
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46.  
  47.  
  48.  
  49. ***************************************************************************************************************************
  50. * end of libfreenect.h
  51. ***************************************************************************************************************************
  52.  
  53.  
  54. ***************************************************************************************************************************
  55. * Makefile
  56. ***************************************************************************************************************************
  57.  CXXFLAGS = -O2 -g -Wall -fmessage-length=0 `pkg-config opencv --cflags ` -I /usr/include/libusb-1.0 -I /usr/local/include
  58.  
  59.  
  60.  OBJS =     freenectopencv.o
  61.  
  62. LIBS =    `pkg-config opencv --libs` -L/usr/local/lib -lfreenect
  63.  
  64. TARGET =    kinectopencv
  65.  
  66. $(TARGET):  $(OBJS)
  67.     $(CXX) -o $(TARGET) $(OBJS) $(LIBS)
  68.  
  69. all:    $(TARGET)
  70.  
  71. clean:
  72.     rm -f $(OBJS) $(TARGET)
  73.  
  74.  
  75. ***************************************************************************************************************************
  76. * End of Makefile
  77. ***************************************************************************************************************************
  78.  
  79.  
  80.  
  81. */
  82.  
  83.  
  84.  
  85.  
  86. #define FREENECTOPENCV_WINDOW_D "Depthimage"
  87. #define FREENECTOPENCV_WINDOW_N "Normalimage"
  88. #define FREENECTOPENCV_RGB_DEPTH 3
  89. #define FREENECTOPENCV_DEPTH_DEPTH 1
  90. #define FREENECTOPENCV_RGB_WIDTH 640
  91. #define FREENECTOPENCV_RGB_HEIGHT 480
  92. #define FREENECTOPENCV_DEPTH_WIDTH 640
  93. #define FREENECTOPENCV_DEPTH_HEIGHT 480
  94.  
  95.  
  96.  
  97. IplImage* depthimg = 0;
  98. IplImage* rgbimg = 0;
  99. pthread_mutex_t mutex_depth = PTHREAD_MUTEX_INITIALIZER;
  100. pthread_mutex_t mutex_rgb = PTHREAD_MUTEX_INITIALIZER;
  101. pthread_t cv_thread;
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. // callback for depthimage, called by libfreenect
  109. void depthimg_callback(uint16_t *buf, int width, int height)
  110. {
  111.  
  112.     // convert depth image to 8bit
  113.     cv::Mat depth8;
  114.     cv::Mat depth = cv::Mat( width, height, CV_16UC1, buf );
  115.     depth.convertTo(depth8, CV_8UC1, 1.0/8.0);
  116.  
  117.     // lock mutex for opencv depth image
  118.     pthread_mutex_lock( &mutex_depth );
  119.     // copy image to opencv buffer
  120.     memcpy(depthimg->imageData, depth8.data, width*height);
  121.     // unlock mutex
  122.     pthread_mutex_unlock( &mutex_depth );
  123.  
  124. }
  125.  
  126. // callback for rgbimage, called by libfreenect
  127.  
  128. void rgbimg_callback(uint8_t *buf, int width, int height)
  129. {
  130.     // lock mutex for opencv rgb image
  131.     pthread_mutex_lock( &mutex_rgb );
  132.     // copy image to opencv buffer
  133.     memcpy(rgbimg->imageData, buf, width*height*FREENECTOPENCV_RGB_DEPTH);
  134.     // unlock mutex
  135.     pthread_mutex_unlock( &mutex_rgb );
  136.  
  137. }
  138.  
  139.  
  140. /*
  141.  * thread for displaying the opencv content
  142.  */
  143. void *cv_threadfunc (void *ptr) {
  144.     cvNamedWindow( FREENECTOPENCV_WINDOW_D, CV_WINDOW_AUTOSIZE );
  145.     cvNamedWindow( FREENECTOPENCV_WINDOW_N, CV_WINDOW_AUTOSIZE );
  146.     depthimg = cvCreateImage(cvSize(FREENECTOPENCV_DEPTH_WIDTH, FREENECTOPENCV_DEPTH_HEIGHT), IPL_DEPTH_8U, FREENECTOPENCV_DEPTH_DEPTH);
  147.     rgbimg = cvCreateImage(cvSize(FREENECTOPENCV_RGB_WIDTH, FREENECTOPENCV_RGB_HEIGHT), IPL_DEPTH_8U, FREENECTOPENCV_RGB_DEPTH);
  148.  
  149.  
  150.     // use image polling
  151.     while (1) {
  152.         //lock mutex for depth image
  153.         pthread_mutex_lock( &mutex_depth );
  154.         // show image to window
  155.         cvShowImage(FREENECTOPENCV_WINDOW_D, depthimg);
  156.         //unlock mutex for depth image
  157.         pthread_mutex_unlock( &mutex_depth );
  158.  
  159.         //lock mutex for rgb image
  160.         pthread_mutex_lock( &mutex_rgb );
  161.         // show image to window
  162.         cvShowImage(FREENECTOPENCV_WINDOW_N, rgbimg);
  163.         //unlock mutex
  164.         pthread_mutex_unlock( &mutex_rgb );
  165.  
  166.         // wait for quit key
  167.         if( cvWaitKey( 15 )==27 )
  168.                 break;
  169.  
  170.     }
  171.     pthread_exit(NULL);
  172.  
  173. }
  174.  
  175.  
  176. int main(int argc, char **argv)
  177. {
  178.  
  179.     int res = 0;
  180.     libusb_device_handle *dev;
  181.     printf("Kinect camera test\n");
  182.  
  183.     libusb_init(NULL);
  184.  
  185.     dev = libusb_open_device_with_vid_pid(NULL, 0x45e, 0x2ae);
  186.     if (!dev) {
  187.         printf("Could not open device\n");
  188.         return 1;
  189.     }
  190.  
  191.     // create opencv display thread
  192.     res = pthread_create(&cv_thread, NULL, cv_threadfunc, (void*) depthimg);
  193.     if (res) {
  194.         printf("pthread_create failed\n");
  195.         return 1;
  196.     }
  197.  
  198.     // set callback functions
  199.     cams_init(dev, depthimg_callback, rgbimg_callback);
  200.     printf("init done\n");
  201.  
  202.     // main loop, forever
  203.     while(libusb_handle_events(NULL) == 0);
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement