Advertisement
lamiastella

sr300camera.cpp test

May 12th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 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. Private constructor for the PMD depth sensor
  8. ***/
  9.  
  10. using namespace Intel::RealSense;
  11.  
  12. SenseManager *pp = SenseManager::CreateInstance();
  13. int depth_width;
  14. int depth_height;
  15.  
  16.  
  17. SR300Camera::SR300Camera(bool use_live_sensor)
  18. {
  19.  
  20.     CONFIDENCE_THRESHHOLD = (60.0f / 255.0f*500.0f);
  21.     //INVALID_FLAG_VALUE = PMD_FLAG_INVALID;
  22.     X_DIMENSION = 176;
  23.     Y_DIMENSION = 120;
  24.  
  25.     if (!use_live_sensor) {
  26.         return;
  27.     }
  28.  
  29.     int res;
  30.     std::cout << "Trying to open SR300 camera\n";
  31.  
  32.    
  33.    
  34.     if (!pp) {
  35.         wprintf_s(L"Unable to create the SenseManager\n");
  36.     }
  37.  
  38.     /* Sets file recording or playback */
  39.     CaptureManager *cm = pp->QueryCaptureManager();
  40.  
  41.     // Create stream renders
  42.     //UtilRender renderc(L"Color"), renderd(L"Depth"), renderi(L"IR"), renderr(L"Right"), renderl(L"Left");
  43.     Status sts;
  44.     UtilRender renderc(L"Color"), renderd(L"Depth"), renderi(L"IR"), renderr(L"Right"), renderl(L"Left");
  45.  
  46.  
  47.     do {
  48.         bool revert = false;
  49.        
  50.         DataDesc desc = {};
  51.         if (cm->QueryCapture()) {
  52.             cm->QueryCapture()->QueryDeviceInfo(0, &desc.deviceInfo);
  53.         }
  54.         else {
  55.             desc.deviceInfo.streams = Capture::STREAM_TYPE_COLOR | Capture::STREAM_TYPE_DEPTH;
  56.             revert = true;
  57.         }
  58.         pp->EnableStreams(&desc);
  59.  
  60.         /* Initializes the pipeline */
  61.    
  62.         sts = pp->Init();
  63.         if (sts < Status::STATUS_NO_ERROR) {
  64.             if (revert) {
  65.                 /* Enable a single stream */
  66.                 pp->Close();
  67.                 pp->EnableStream(Capture::STREAM_TYPE_DEPTH);
  68.                 sts = pp->Init();
  69.                 if (sts < Status::STATUS_NO_ERROR) {
  70.                     pp->Close();
  71.                     //pp->EnableStream(Capture::STREAM_TYPE_COLOR);
  72.                     sts = pp->Init();
  73.                 }
  74.             }
  75.             if (sts < Status::STATUS_NO_ERROR) {
  76.                 wprintf_s(L"Failed to locate any video stream(s)\n");
  77.                 pp->Release();
  78.             }
  79.         }
  80.  
  81.         /* Reset all properties */
  82.         Capture::Device *device = pp->QueryCaptureManager()->QueryDevice();
  83.         device->ResetProperties(Capture::STREAM_TYPE_ANY);
  84.  
  85.         for (int nframes = 0; ; nframes++) {
  86.             /* Waits until new frame is available and locks it for application processing */
  87.             sts = pp->AcquireFrame(false);
  88.  
  89.             if (sts < Status::STATUS_NO_ERROR) {
  90.                 if (sts == Status::STATUS_STREAM_CONFIG_CHANGED) {
  91.                     wprintf_s(L"Stream configuration was changed, re-initilizing\n");
  92.                     pp->Close();
  93.                 }
  94.                 break;
  95.             }
  96.  
  97.             /* Render streams, unless -noRender is selected */
  98.             const PXCCapture::Sample *sample = pp->QuerySample();
  99.             //const Capture::Sample *sample = pp->QuerySample();
  100.             if (sample) {
  101.                 cv::Mat img;
  102.                 //Copy all the vertices from Point3DF32 to the mat image
  103.                 //QueryVertices(Image *depth, Point3DF32 *vertices)
  104.                 //sample->color->QueryInfo()
  105.                 depth_width = sample->depth->QueryInfo().width;
  106.                 depth_height = sample->depth->QueryInfo().height;
  107.  
  108.  
  109.                 //cv::imshow("depth from OpenARK", Visualizer::visualizeDepthMap(img))
  110.                 if (sample->depth && !renderd.RenderFrame(sample->depth)) break;
  111.                 if (sample->color && !renderc.RenderFrame(sample->color)) break;
  112.                 if (sample->ir && !renderi.RenderFrame(sample->ir))    break;
  113.  
  114.             }
  115.  
  116.  
  117.             printf("opened sensor\n");
  118.  
  119.             // Updating the sensor is necessary before any data can be retrieved
  120.             //res = pmdUpdate(hnd);
  121.             /* Releases lock so pipeline can process next frame */
  122.             pp->ReleaseFrame();
  123.             printf("acquired image\n");
  124.         }
  125.  
  126.     } while (sts == Status::STATUS_STREAM_CONFIG_CHANGED);
  127.  
  128.  
  129.  
  130.        
  131.     /*res = pmdOpen(&hnd, SOURCE_PLUGIN, SOURCE_PARAM, PROC_PLUGIN, PROC_PARAM); //Open the PMD sensor
  132.     if (res != PMD_OK)
  133.     {
  134.         pmdGetLastError(0, err, 128);
  135.         fprintf(stderr, "Could not connect: %s\n", err);
  136.         return;
  137.     }
  138.  
  139.     */
  140.  
  141.  
  142.     // res: Structure which contains various meta-information about the data delivered by your Nano.
  143.     // It is advisabe to always use the data delivered by this struct (for example the width and height of the imager
  144.     // and the image format). Please refer to the PMSDSK documentation for more information
  145.    
  146.     /*
  147.     res = pmdGetSourceDataDescription(hnd, &dd);
  148.     if (sts < STATUS_NO_ERROR) {
  149.         pp->Release();
  150.         return;
  151.     }
  152.     */
  153.  
  154.     /*
  155.     if (res != PMD_OK)
  156.     {
  157.         pmdGetLastError(hnd, err, 128);
  158.         fprintf(stderr, "Couldn't get data description: %s\n", err);
  159.         pp->Release();
  160.         //pmdClose(hnd);
  161.         return;
  162.     }
  163.    
  164.     printf("retrieved source data description\n");
  165.     if (dd.subHeaderType != PMD_IMAGE_DATA)
  166.     {
  167.         fprintf(stderr, "Source data is not an image!\n");
  168.         pmdClose(hnd);
  169.         return;
  170.     }
  171.     */
  172.     //numRows = width and numCol = height? ?
  173.     //numPixels = dd.img.numRows * dd.img.numColumns; // Number of pixels in camera
  174.     numPixels = depth_width * depth_height;
  175.     dists = new float[3 * numPixels]; // Dists contains XYZ values. needs to be 3x the size of numPixels
  176.     amps = new float[numPixels];
  177.     //frame = cvCreateImage(cvSize(dd.img.numColumns, dd.img.numRows), 8, 3); // Create the frame
  178.     frame = cvCreateImage(cvSize(depth_width, depth_height), 8, 3); // Create the frame
  179.  
  180.     KF.init(6, 3, 0);
  181.     KF.transitionMatrix = (cv::Mat_<float>(6, 6) << 1, 0, 0, 1, 0, 0,
  182.                                                     0, 1, 0, 0, 1, 0,
  183.                                                     0, 0, 1, 0, 0, 1,
  184.                                                     0, 0, 0, 1, 0, 0,
  185.                                                     0, 0, 0, 0, 1, 0,
  186.                                                     0, 0, 0, 0, 0, 1);
  187.     measurement = cv::Mat_<float>::zeros(3, 1);
  188.  
  189.     //Initaite Kalman
  190.     KF.statePre.setTo(0);
  191.     setIdentity(KF.measurementMatrix);
  192.     setIdentity(KF.processNoiseCov, cv::Scalar::all(.001)); // Adjust this for faster convergence - but higher noise
  193.     setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-1));
  194.     setIdentity(KF.errorCovPost, cv::Scalar::all(.1));
  195.  
  196.     /* mona will test this
  197.     setIdentity(KF.measurementMatrix);
  198.     setIdentity(KF.processNoiseCov, Scalar::all(smoothness));
  199.     setIdentity(KF.measurementNoiseCov, Scalar::all(rapidness));
  200.     setIdentity(KF.errorCovPost, Scalar::all(.1));
  201.     */
  202. }
  203.  
  204. /***
  205. Public deconstructor for he PMD depth sensor
  206. ***/
  207. SR300Camera::~SR300Camera() {};
  208.  
  209. void SR300Camera::destroyInstance()
  210. {
  211.     printf("closing sensor\n");
  212.     pp->Release();
  213.     printf("sensor closed\n");
  214. }
  215.  
  216. /***
  217. Create xyzMap, zMap, ampMap, and flagMap from sensor input
  218. ***/
  219. void SR300Camera::update()
  220. {
  221.     initilizeImages();
  222.    
  223.     fillInAmps();
  224.     fillInZCoords();
  225.     // Flags. Helps with denoising.
  226.     /*
  227.     unsigned *flags = new unsigned[ampMap.cols*ampMap.rows];
  228.     int res = pmdGetFlags(hnd, flags, numPixels * sizeof(unsigned));
  229.     flagMap.data = (uchar *)flags;
  230.  
  231.     pmdUpdate(hnd);
  232.     delete(flags);
  233.     */
  234.     pp->ReleaseFrame(); //correct?
  235.     pxcStatus sts = pp->AcquireFrame(true);
  236.     if (sts < PXC_STATUS_NO_ERROR)
  237.         return;
  238.  
  239.  
  240.  
  241. }
  242.  
  243.  
  244. /***
  245. Reads the depth data from the sensor and fills in the matrix
  246. ***/
  247. void SR300Camera::fillInZCoords()
  248. {
  249.     int res = pmdGet3DCoordinates(hnd, dists, 3 * numPixels * sizeof(float)); //store x,y,z coordinates dists (type: float*)
  250.     //float * zCoords = new float[1]; //store z-Coordinates of dists in zCoords
  251.     xyzMap = cv::Mat(xyzMap.size(), xyzMap.type(), dists);
  252. }
  253.  
  254. /***
  255. Reads the amplitude data from the sensor and fills in the matrix
  256. ***/
  257. void SR300Camera::fillInAmps()
  258. {
  259.     //int res = pmdGetAmplitudes(hnd, amps, numPixels * sizeof(float));
  260.     //float * dataPtr = amps;
  261.     //ampMap.data = (uchar *)amps;
  262. }
  263.  
  264. /***
  265. Returns the X value at (i, j)
  266. ***/
  267. float SR300Camera::getX(int i, int j) const
  268. {
  269.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  270.     int flat = j * depth_width * 3 + i * 3;
  271.     return dists[flat];
  272. }
  273.  
  274. /***
  275. Returns the Y value at (i, j)
  276. ***/
  277. float SR300Camera::getY(int i, int j) const
  278. {
  279.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  280.     int flat = j * depth_width * 3 + i * 3;
  281.     return dists[flat + 1];
  282. }
  283.  
  284. /***
  285. Returns the Z value at (i, j)
  286. ***/
  287. float SR300Camera::getZ(int i, int j) const
  288. {
  289.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  290.     int flat = j * depth_width * 3 + i * 3;
  291.     return dists[flat + 2];
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement