Advertisement
Guest User

main.cpp

a guest
Dec 13th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. /**
  2. @author Fabio Dominio
  3. @version 0.1
  4. @date 12/07/2015
  5. */
  6.  
  7. // Include standard headers
  8. #include <iostream>
  9. #include <fstream>
  10. #include <vector>
  11. #include <chrono>
  12. #include <thread>
  13. #include <functional>
  14.  
  15. // Include opencv headers
  16. #include <opencv2\highgui\highgui.hpp>
  17. #include <opencv2\imgproc\imgproc.hpp>
  18.  
  19. // Include Kinect2 manager headers
  20. #include <Kinect2Manager.h>
  21.  
  22. // Define employed namespaces
  23. using namespace std;
  24. using namespace cv;
  25.  
  26. // Define constants
  27. const int DEPTH_WIDTH = 512;
  28. const int DEPTH_HEIGHT = 424;
  29. const int COLOR_WIDTH = 1920;
  30. const int COLOR_HEIGHT = 1080;
  31. const int DEPTH_WIDTH_PREVIEW = 512;
  32. const int DEPTH_HEIGHT_PREVIEW = 424;
  33. const int COLOR_WIDTH_PREVIEW = 754;
  34. const int COLOR_HEIGHT_PREVIEW = 424;
  35. const unsigned short MAX_DEPTH = 5000; // 5 m
  36. const string SAVE_PATH = "";
  37.  
  38. // Define variables;
  39. int maxDepth = MAX_DEPTH;
  40. int fps = 0;
  41. int fpsCounter = 0;
  42. long frameNr = 0;
  43. int savedFrames = 0;
  44. char key = 0;
  45.  
  46. // Initialize opencv data structures
  47. Mat depthMat(DEPTH_HEIGHT, DEPTH_WIDTH, CV_16U);
  48. Mat depthBuffer(DEPTH_HEIGHT, DEPTH_WIDTH, CV_8U);
  49. Mat colorBuffer(COLOR_HEIGHT_PREVIEW, COLOR_WIDTH_PREVIEW, CV_8UC4);
  50. Mat colorImage(COLOR_HEIGHT, COLOR_WIDTH, CV_8UC4);
  51. Mat preview(DEPTH_HEIGHT_PREVIEW, DEPTH_WIDTH_PREVIEW + COLOR_WIDTH_PREVIEW, CV_8UC3);
  52. Mat depthImage = preview(Rect(COLOR_WIDTH_PREVIEW, 0, DEPTH_WIDTH_PREVIEW, DEPTH_HEIGHT_PREVIEW));
  53. Mat colorImagePreview = preview(Rect(0, 0, COLOR_WIDTH_PREVIEW, COLOR_HEIGHT_PREVIEW));
  54.  
  55. // FPS update
  56. void fpsUpdate()
  57. {
  58.     fps = fpsCounter;
  59.     fpsCounter = 0;
  60. }
  61.  
  62. // FPS counter method
  63. void timer_start(std::function<void(void)> func, unsigned int interval)
  64. {
  65.     std::thread([func, interval]() {
  66.         while (true)
  67.         {
  68.             func();
  69.             std::this_thread::sleep_for(std::chrono::milliseconds(interval));
  70.         }
  71.     }).detach();
  72. }
  73.  
  74. // Memory cleanup method
  75. void cleanup() {
  76.     // Cleanup memory
  77.     depthMat.release();
  78.     depthBuffer.release();
  79.     colorBuffer.release();
  80.     colorImage.release();
  81.     preview.release();
  82.     depthImage.release();
  83.     colorImagePreview.release();
  84. }
  85.  
  86. // Main method
  87. void main() {
  88.     // Connect to default kinect2
  89.     Kinect2Manager kinect;
  90.     bool res = kinect.connect();
  91.     if (!res) {
  92.         cerr << "Error opening kinect2 sensor: " + kinect.getLastError() << endl;
  93.         cleanup();
  94.         system("pause");
  95.         exit(-1);
  96.     }
  97.  
  98.     // Else kinect2 successfully connected
  99.     cout << "Kinect2 sensor open" << endl;
  100.  
  101.     // Create preview window   
  102.     namedWindow("Kinect2 stream");
  103.     createTrackbar("Max depth", "Kinect2 stream", &maxDepth, MAX_DEPTH);
  104.  
  105.     // Start acquisition
  106.     kinect.acquireColorData(true);
  107.     kinect.acquireDepthData(true);
  108.     res = kinect.startAcquisition();
  109.     if (!res) {
  110.         cerr << "Unable to start data acquisition: " + kinect.getLastError() << endl;
  111.         destroyAllWindows();
  112.         cleanup();
  113.         system("pause");
  114.         exit(-1);
  115.     }
  116.     cout << "Acquisition started" << endl;
  117.  
  118.     // Start fps counter thread
  119.     timer_start(fpsUpdate, 1000);
  120.  
  121.  
  122.     while (key != 27) {
  123.         frameNr++;
  124.         // Grab frame
  125.         res = kinect.grabSingleFrame(reinterpret_cast<unsigned short*>(depthMat.data), reinterpret_cast<unsigned char*>(colorImage.data));
  126.         if (!res) {
  127.             //cerr <<"Unable to grab frame " << frameNr << ": " + kinect.getLastError() << endl;
  128.             continue;
  129.         }
  130.         else {
  131.             frameNr++;
  132.             fpsCounter++;
  133.             depthMat.convertTo(depthBuffer, CV_8U, 255.0 / maxDepth);
  134.             cvtColor(depthBuffer, depthImage, CV_GRAY2BGR);
  135.             resize(colorImage, colorBuffer, Size(COLOR_WIDTH_PREVIEW, COLOR_HEIGHT_PREVIEW));
  136.             cvtColor(colorBuffer, colorImagePreview, CV_BGRA2BGR);
  137.             // Update frame counter
  138.             putText(preview, "Current FPS: " + to_string(fps), Point(10, 20), CV_FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255));
  139.         }
  140.  
  141.         // Display frame
  142.         imshow("Kinect2 stream", preview);
  143.  
  144.         // Grab key
  145.         key = waitKey(5);
  146.  
  147.         // Save frame if required
  148.         if (key == 'g' || key == 'G') {
  149.             // Save grabbed frame
  150.             imwrite(SAVE_PATH + to_string(++savedFrames) + "_RGB.png", colorImage);
  151.             imwrite(SAVE_PATH + to_string(savedFrames) + "_DEPTH.png", depthImage);
  152.             ofstream out;
  153.             out.open(SAVE_PATH + to_string(savedFrames) + "_DEPTH.dat");
  154.             for (int i = 0; i < depthMat.rows; ++i) {
  155.                 unsigned short* rowPtr = (unsigned short*)depthMat.ptr(i);
  156.                 for (int j = 0; j < depthMat.cols; ++j)
  157.                     out << rowPtr[j] << " ";
  158.                 out << endl;
  159.             }
  160.             out.close();
  161.             cout <<"Frame " << savedFrames << " saved." << endl;
  162.         }
  163.     }
  164.  
  165.     // Stop acquisition
  166.     res = kinect.stopAcquisition();
  167.     if (!res) {
  168.         cerr << "Unable to stop acquisition!" << endl;
  169.         system("pause");       
  170.     }
  171.  
  172.     // Cleanup
  173.     destroyAllWindows();
  174.     cleanup();
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement