Advertisement
lamiastella

SR300Camera.cpp

May 23rd, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. #define NOMINMAX
  2. #define _WINSOCKAPI_  
  3.  
  4. #include "SR300Camera.h"
  5.  
  6. #include "util_render.h"
  7. #include "Visualizer.h"
  8. #include "Converter.h"
  9.  
  10. #include <iostream>
  11.  
  12. /***
  13. Private constructor for the SR300 Camera depth sensor
  14. ***/
  15. using namespace std;
  16. //using namespace Intel::RealSense;
  17.  
  18. PXCSenseManager *pp = PXCSenseManager::CreateInstance();
  19. PXCCapture::Device *device;
  20. PXCCaptureManager *cm;
  21.  
  22. const int depth_fps = 30;
  23. const int depth_width = 640;
  24. const int depth_height = 480;
  25. cv::Size bufferSize;
  26. const PXCCapture::Sample *sample;
  27. //std::vector<CV_32FC3> xyzBuffer;
  28. vector<cv::Point3f> xyzBuffer;
  29.  
  30.  
  31. SR300Camera::SR300Camera(bool use_live_sensor)
  32. {
  33.  
  34.     CONFIDENCE_THRESHHOLD = (60.0f / 255.0f*500.0f);
  35.     X_DIMENSION = depth_width;
  36.     Y_DIMENSION = depth_height;
  37.  
  38.     int num_pixels = X_DIMENSION * Y_DIMENSION;
  39.     PXCPoint3DF32* vertices = new PXCPoint3DF32[num_pixels];
  40.  
  41.     if (!use_live_sensor) {
  42.         return;
  43.     }
  44.  
  45.     int res;
  46.     std::cout << "Trying to open SR300 camera\n";
  47.  
  48.  
  49.  
  50.     if (!pp) {
  51.         wprintf_s(L"Unable to create the SenseManager\n");
  52.     }
  53.  
  54.     cm = pp->QueryCaptureManager();
  55.     //Status sts= STATUS_NO_ERROR;
  56.     Status sts = STATUS_DATA_UNAVAILABLE; //mona is this the correct initialization?
  57.     UtilRender renderc(L"Color"), renderd(L"Depth"), renderi(L"IR"), renderr(L"Right"), renderl(L"Left");
  58.  
  59.     //do {
  60.         bool revert = false;
  61.         pp->EnableStream(PXCCapture::StreamType::STREAM_TYPE_DEPTH,
  62.             X_DIMENSION, Y_DIMENSION, depth_fps);
  63.         revert = true;
  64.         sts = pp->Init();
  65.         if (sts < Status::STATUS_NO_ERROR) {
  66.  
  67.             if (revert) {
  68.  
  69.                 pp->Close();
  70.                 pp->EnableStream(PXCCapture::STREAM_TYPE_DEPTH);
  71.                 sts = pp->Init();
  72.                 if (sts < Status::STATUS_NO_ERROR) {
  73.                     pp->Close();
  74.                     //pp->EnableStream(Capture::STREAM_TYPE_COLOR);
  75.                     sts = pp->Init();
  76.                 }
  77.             }
  78.             if (sts < Status::STATUS_NO_ERROR) {
  79.                 wprintf_s(L"Failed to locate any video stream(s)\n");
  80.                 pp->Release();
  81.             }
  82.         }
  83.  
  84.         device = pp->QueryCaptureManager()->QueryDevice();
  85.  
  86.         //device->ResetProperties(PXCCapture::STREAM_TYPE_ANY);
  87.  
  88.         for (int nframes = 0; ; nframes++) {
  89.             sts = pp->AcquireFrame(false);
  90.  
  91.             if (sts < Status::STATUS_NO_ERROR) {
  92.                 if (sts == Status::STATUS_STREAM_CONFIG_CHANGED) {
  93.                     wprintf_s(L"Stream configuration was changed, re-initilizing\n");
  94.                     pp->Close();
  95.                 }
  96.                 break;
  97.             }
  98.  
  99.             sample = pp->QuerySample();
  100.  
  101.             if (sample) {
  102.                 cv::Mat img;
  103.                 Converter::ConvertPXCImageToOpenCVMat(sample->depth, &img);
  104.  
  105.                 if (sample->depth && !renderd.RenderFrame(sample->depth)) break;
  106.                 PXCImage::ImageData depthImage;
  107.                 PXCImage *depthMap = sample->depth;
  108.                 depthMap->AcquireAccess(PXCImage::ACCESS_READ, &depthImage);
  109.                 //depthMap->AcquireAccess(PXCImage::ACCESS_WRITE, &depthImage); //mona is this the correct flag?
  110.                 PXCImage::ImageInfo imgInfo = depthMap->QueryInfo();
  111.                 int depth_stride = depthImage.pitches[0] / sizeof(pxcU16);
  112.                 PXCProjection * projection = device->CreateProjection();
  113.                 cout << "projection is: " << projection;
  114.                 cout << "device is: " << device;
  115.                 pxcU16 *dpixels = (pxcU16*)depthImage.planes[0];
  116.                 unsigned int dpitch = depthImage.pitches[0] / sizeof(pxcU16);
  117.                 PXCPoint3DF32 *pos3D = new PXCPoint3DF32[num_pixels];
  118.  
  119.                 projection->QueryVertices(depthMap, pos3D);
  120.                
  121.                
  122.                 for (int k = 0; k < num_pixels; k++) {
  123.                    
  124.                     /*if (pos3D[k].x != 0) {
  125.                         cout << " xx is: " << pos3D[k].x << endl;
  126.                     }
  127.                     if (pos3D[k].y != 0) {
  128.                         cout << " yy is: " << pos3D[k].y << endl;
  129.                     }
  130.                     if (pos3D[k].z != 0) {
  131.                         cout << " zz is: " << pos3D[k].z << endl;
  132.                     }*/
  133.                     //xyzBuffer.push_back(cv::Point3f(pos3D[k].x, pos3D[k].y, pos3D[k].z));
  134.                     xyzBuffer.emplace_back(cv::Point3f(pos3D[k].x, pos3D[k].y, pos3D[k].z));
  135.                    
  136.                    
  137.                 }
  138.  
  139.                 //xyzMap = cv::Mat(xyzBuffer);
  140.                 cv::Mat xyzMap{ xyzBuffer };
  141.                
  142.  
  143.             }
  144.             printf("opened sensor\n");
  145.             pp->ReleaseFrame();
  146.             printf("acquired image\n");
  147.             //cout << "xyzMap = " << endl << " " << xyzMap << endl << endl;
  148.  
  149.         }
  150.  
  151.     //} while (sts == Status::STATUS_STREAM_CONFIG_CHANGED);
  152.  
  153.     //numRows = width and numCol = height? ?
  154.     //numPixels = dd.img.numRows * dd.img.numColumns; // Number of pixels in camera
  155.     numPixels = depth_width * depth_height;
  156.     dists = new float[3 * numPixels]; // Dists contains XYZ values. needs to be 3x the size of numPixels
  157.     amps = new float[numPixels];
  158.     //frame = cvCreateImage(cvSize(dd.img.numColumns, dd.img.numRows), 8, 3); // Create the frame
  159.     frame = cvCreateImage(cvSize(depth_width, depth_height), 8, 3); // Create the frame
  160.  
  161.     KF.init(6, 3, 0);
  162.     KF.transitionMatrix = (cv::Mat_<float>(6, 6) << 1, 0, 0, 1, 0, 0,
  163.         0, 1, 0, 0, 1, 0,
  164.         0, 0, 1, 0, 0, 1,
  165.         0, 0, 0, 1, 0, 0,
  166.         0, 0, 0, 0, 1, 0,
  167.         0, 0, 0, 0, 0, 1);
  168.     measurement = cv::Mat_<float>::zeros(3, 1);
  169.  
  170.     //Initaite Kalman
  171.     KF.statePre.setTo(0);
  172.     setIdentity(KF.measurementMatrix);
  173.     setIdentity(KF.processNoiseCov, cv::Scalar::all(.001)); // Adjust this for faster convergence - but higher noise
  174.     setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-1));
  175.     setIdentity(KF.errorCovPost, cv::Scalar::all(.1));
  176. }
  177.  
  178. /***
  179. Public deconstructor for the SR300 Camera depth sensor
  180. ***/
  181. SR300Camera::~SR300Camera() {};
  182.  
  183. void SR300Camera::destroyInstance()
  184. {
  185.     printf("closing sensor\n");
  186.     pp->Release();
  187.     printf("sensor closed\n");
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. /***
  198. Create xyzMap, zMap, ampMap, and flagMap from sensor input
  199. ***/
  200. void SR300Camera::update()
  201. {
  202.     initilizeImages();
  203.  
  204.     fillInAmps();
  205. }
  206.  
  207.  
  208.  
  209. /***
  210. Reads the amplitude data from the sensor and fills in the matrix
  211. ***/
  212. void SR300Camera::fillInAmps()
  213. {
  214.     //int res = pmdGetAmplitudes(hnd, amps, numPixels * sizeof(float));
  215.     //float * dataPtr = amps;
  216.     //ampMap.data = (uchar *)amps;
  217. }
  218.  
  219.  
  220.  
  221.  
  222. /***
  223. Returns the X value at (i, j)
  224. ***/
  225. float SR300Camera::getX(int i, int j) const
  226. {
  227.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  228.     int flat = j * depth_width * 3 + i * 3;
  229.     return dists[flat];
  230. }
  231.  
  232. /***
  233. Returns the Y value at (i, j)
  234. ***/
  235. float SR300Camera::getY(int i, int j) const
  236. {
  237.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  238.     int flat = j * depth_width * 3 + i * 3;
  239.     return dists[flat + 1];
  240. }
  241.  
  242. /***
  243. Returns the Z value at (i, j)
  244. ***/
  245. float SR300Camera::getZ(int i, int j) const
  246. {
  247.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  248.     int flat = j * depth_width * 3 + i * 3;
  249.     return dists[flat + 2];
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement