Advertisement
lamiastella

SR300Camera.cpp vertices[0].x/y/z not zero now

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