Advertisement
lamiastella

SR300Camera.cpp with tests

May 30th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.26 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.  
  14. /***
  15. Private constructor for the SR300 Camera depth sensor
  16. ***/
  17. using namespace std;
  18. //using namespace Intel::RealSense;
  19.  
  20.  
  21.  
  22. const int depth_fps = 30;
  23. int depth_width;
  24. int depth_height;
  25. cv::Size bufferSize;
  26. const PXCCapture::Sample *sample;
  27. PXCSenseManager *sm = PXCSenseManager::CreateInstance();
  28. PXCSession *session = sm->QuerySession();
  29.  
  30. PXCCapture::Device *device;
  31. PXCCaptureManager *cm;
  32.  
  33. UtilRender renderc(L"Color"), renderd(L"Depth"), renderi(L"IR"), renderr(L"Right"), renderl(L"Left");
  34. int num_pixels;
  35. vector<cv::Point3f>  xyzBuffer;
  36.  
  37. SR300Camera::SR300Camera(bool use_live_sensor) {
  38.  
  39.     session->SetCoordinateSystem(PXCSession::CoordinateSystem::COORDINATE_SYSTEM_FRONT_DEFAULT);
  40.     //X_DIMENSION = depth_width;
  41.     //Y_DIMENSION = depth_height;
  42.    
  43.     if (!sm) {
  44.         wprintf_s(L"Unable to create the SenseManager\n");
  45.     }
  46.     cm = sm->QueryCaptureManager();
  47.     Status sts = STATUS_DATA_UNAVAILABLE;
  48.                                        
  49.     sm->EnableStream(PXCCapture::StreamType::STREAM_TYPE_DEPTH,
  50.         X_DIMENSION, Y_DIMENSION, depth_fps);
  51.     //revert = true;
  52.     sts = sm->Init();
  53.     if (sts < Status::STATUS_NO_ERROR) {
  54.         sm->Close();
  55.         sm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH);
  56.         sts = sm->Init();
  57.         if (sts < Status::STATUS_NO_ERROR) {
  58.             sm->Close();
  59.             //pp->EnableStream(Capture::STREAM_TYPE_COLOR);
  60.             sts = sm->Init();
  61.         }
  62.     }
  63.     device = cm->QueryDevice();
  64.     //device->ResetProperties(PXCCapture::STREAM_TYPE_ANY);
  65.  
  66. }
  67.  
  68.  
  69. /***
  70. Public deconstructor for the SR300 Camera depth sensor
  71. ***/
  72. SR300Camera::~SR300Camera() {};
  73.  
  74. void SR300Camera::destroyInstance()
  75. {
  76.     printf("closing sensor\n");
  77.     //pp->Release();
  78.     sm->Close();
  79.     printf("sensor closed\n");
  80. }
  81.  
  82.  
  83.  
  84.  
  85. /***
  86. Create xyzMap, zMap, ampMap, and flagMap from sensor input
  87. ***/
  88. void SR300Camera::update()
  89. {
  90.     initilizeImages();
  91.     fillInAmps();
  92.     fillInZCoords();
  93.     sm->ReleaseFrame();
  94. }
  95.  
  96. void SR300Camera::fillInZCoords(){
  97.     int res;
  98.     //sts = pp->AcquireFrame(false);
  99.     Status sts = sm ->AcquireFrame(true);
  100.    
  101.  
  102.     if (sts < STATUS_NO_ERROR) {
  103.         if (sts == Status::STATUS_STREAM_CONFIG_CHANGED) {
  104.             wprintf_s(L"Stream configuration was changed, re-initilizing\n");
  105.             sm ->Close();
  106.         }
  107.     }
  108.  
  109.     sample = sm->QuerySample();
  110.     PXCImage *depthMap = sample->depth;
  111.  
  112.     //mona if (sample->depth && !renderd.RenderFrame(sample->depth)) break;
  113.     //renderd.RenderFrame(sample->depth);
  114.     //cv::namedWindow("depth", CV_WINDOW_AUTOSIZE);
  115.     //cv::imshow("depth", sample->depth);
  116.  
  117.     PXCImage::ImageData depthImage;
  118.     depthMap->AcquireAccess(PXCImage::ACCESS_READ, &depthImage);
  119.     cv::Mat img;
  120.     Converter::ConvertPXCImageToOpenCVMat(depthMap, depthImage, &img);
  121.     cv::imshow("Depth Image by OpenARK", Visualizer::visualizeDepthMap(img));
  122.     //cout << "nrows: " << img.rows << endl;
  123.     //cout << "ncols: " << img.cols << endl;
  124.     //cout << " img channels: " << img.channels() << endl;
  125.     depthMap->AcquireAccess(PXCImage::ACCESS_WRITE, &depthImage); //mona is this the correct flag?
  126.     PXCImage::ImageInfo imgInfo = depthMap->QueryInfo();
  127.     depth_width = imgInfo.width;
  128.     depth_height = imgInfo.height;
  129.     num_pixels = depth_width * depth_height;
  130.     cout << "height is: " << depth_height << endl;
  131.     cout << "width is: " << depth_width << endl;
  132.     PXCProjection * projection = device->CreateProjection();
  133.     pxcU16 *dpixels = (pxcU16*)depthImage.planes[0];
  134.     unsigned int dpitch = depthImage.pitches[0] / sizeof(pxcU16);
  135.     PXCPoint3DF32 *pos3D = new PXCPoint3DF32[num_pixels];
  136.  
  137.     //sts = projection->QueryVertices(depthMap, pos3D);
  138.     sts = projection->QueryVertices(depthMap, &pos3D[0]);
  139.     if (sts < Status::STATUS_NO_ERROR) {
  140.         wprintf_s(L"Projection was unsuccessful! \n");
  141.         sm->Close();
  142.     }
  143.  
  144.     xyzBuffer.clear();
  145.     for (int k = 0; k < num_pixels; k++) {
  146.        
  147.             /*if (pos3D[k].x != 0) {
  148.             cout << " xx is: " << pos3D[k].x << endl;
  149.             }
  150.             if (pos3D[k].y != 0) {
  151.             cout << " yy is: " << pos3D[k].y << endl;
  152.             }
  153.             if (pos3D[k].z != 0) {
  154.             cout << " zz is: " << pos3D[k].z << endl;
  155.             }*/
  156.             xyzBuffer.push_back(cv::Point3f(pos3D[k].x/1000.0f, pos3D[k].y/1000.0f, pos3D[k].z/1000.0f));
  157.            
  158.             /*for (int i = 0; i < xyzBuffer.size(); i++) {
  159.                 cout << "xyz buffer x: " << xyzBuffer.at(i).x << endl;
  160.                 cout << "xyz buffer y: " << xyzBuffer.at(i).y << endl;
  161.                 cout << "xyz buffer z: " << xyzBuffer.at(i).z << endl;
  162.             }*/
  163.             //xyzBuffer.emplace_back(cv::Point3f(pos3D[k].x, pos3D[k].y, pos3D[k].z));
  164.             /*if (xyzBuffer.at(k).x != 0) {
  165.                 cout << "xyz buffer x: " << xyzBuffer.at(k).x << endl;
  166.             }if (xyzBuffer.at(k).y != 0) {
  167.                 cout << "xyz buffer y: " << xyzBuffer.at(k).y << endl;
  168.             }if (xyzBuffer.at(k).z != 0) {
  169.                 cout << "xyz buffer z: " << xyzBuffer.at(k).z << endl;
  170.             }*/
  171.     }
  172.  
  173.  
  174.     //cout << "vector length is: " << xyzBuffer.size() << endl;
  175.     //cout << "size of " << sizeof(CV_32FC3);
  176.    
  177.     //cout << "xyzBuffer = " << endl << " " << xyzBuffer << endl << endl;
  178.     //xyzMap = cv::Mat(xyzBuffer);
  179.     //cv::Mat xyzMap{ xyzBuffer };
  180.    
  181.     /*double max_x = xyzBuffer[0].x;
  182.     double max_y = xyzBuffer[1].y;
  183.     double max_z = xyzBuffer[2].z;
  184.     for (int i = 0; i < xyzBuffer.size(); i++) {
  185.         if (xyzBuffer[i].x > max_x) {
  186.             max_x = xyzBuffer[i].x;
  187.         }
  188.         if (xyzBuffer[i].y > max_y) {
  189.             max_y = xyzBuffer[i].y;
  190.         }
  191.         if (xyzBuffer[i].z > max_z) {
  192.             max_z = xyzBuffer[i].z;
  193.         }
  194.     }
  195.     */
  196.     //xyzMap = (CV_32FC3) xyzBuffer;
  197.  
  198.     //cv::Point3f pmax= std::accumulate(xyzBuffer.cbegin(), xyzBuffer.cend(), [](const cv::Point3f&a, const cv::Point3f&b) {return cv::Point3f(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)); }
  199.     //memcpy(xyzMap.data, xyzBuffer.data(), xyzBuffer.size()* xyzMap.elemSize());
  200.     /*cv::Mat xyzBuffMat = cv::Mat(xyzBuffer.size(), 1, CV_32FC3);
  201.     for (int i = 0; i < xyzBuffer.size(); i++) {
  202.         xyzBuffMat.at<float>(i, 0, 0) = xyzBuffer[i].x;
  203.         xyzBuffMat.at<float>(i, 0, 1) = xyzBuffer[i].y;
  204.         xyzBuffMat.at<float>(i, 0, 2) = xyzBuffer[i].z;
  205.     }
  206.    
  207.     xyzMap = xyzBuffMat.reshape(3, 480);
  208.     cvtColor(xyzMap, xyzMap, CV_RGB2BGR);
  209.     */
  210.    
  211.     xyzMap = cv::Mat(xyzBuffer, true).reshape(3, 480);
  212.     //cvtColor(xyzMap, xyzMap, CV_RGB2BGR);
  213.    
  214.  
  215.  
  216.     //testMat.convertTo(testMat, CV_32FC3);
  217.     //xyzMap = testMat;
  218.     //xyzMap = cv::Mat(xyzBuffer);
  219.     //cout << "nrows"<<xyzMap.rows << endl;
  220.     //cout << "ncols" << xyzMap.cols << endl;
  221.  
  222.     //imshow("xyz mona", xyzMap);
  223.     //double min, max;
  224.     //cv::minMaxLoc(xyzMap, &min, &max);
  225.     //cout << "max min: "<<min<<" "<<max<<endl;
  226.     cv::namedWindow("XYZ Image by OpenARK", CV_WINDOW_AUTOSIZE);
  227.     cv::imshow("XYZ Image by OpenARK", Visualizer::visualizeXYZMap(xyzMap));
  228.     //cv::waitKey();
  229.  
  230.     //cv::destroyWindow("Depth Image by OpenARK");
  231.  
  232.    
  233.     //cout << "numrows: " << xyzMap.rows << endl;
  234.     //cout << "numcols: " << xyzMap.cols << endl;
  235.     //cout << "nrows: "<<xyzMap.rows<<endl;
  236.     //cout << "ncols: "<<xyzMap.cols << endl;
  237.  
  238.     /*double min;
  239.     double max;
  240.     cv::minMaxIdx(xyzMap, &min, &max);
  241.     cv::Mat adjMap;
  242.     cv::convertScaleAbs(xyzMap, adjMap, 255 / max);
  243.     cv::imshow("Out xyz", adjMap);*/
  244.     //cv::imshow("xyz window", xyzMap);
  245.     //cout << "xyzMap = " << endl << " " << xyzMap << endl << endl;
  246.     /*
  247.     double min;
  248.     double max;
  249.     cv::minMaxIdx(xyzMap, &min, &max);
  250.     cv::Mat adjMap;
  251.     // expand your range to 0..255. Similar to histEq();
  252.     //xyzMap.convertTo(adjMap, CV_32FC3, 255 / (max - min), -min);
  253.     xyzMap.convertTo(adjMap, CV_32FC3, 255 / (max - min), (-255 * min / (max - min)));
  254.    
  255.     cv::Mat falseColorsMap;
  256.     applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_AUTUMN);
  257.  
  258.     cv::imshow("Out", falseColorsMap); */
  259.  
  260. /*  dists = new float[3 * numPixels]; // Dists contains XYZ values. needs to be 3x the size of numPixels
  261.     amps = new float[numPixels];
  262.     //frame = cvCreateImage(cvSize(depth_width, depth_height), 8, 3); // Create the frame
  263.     frame = cv::Mat(cv::Size(depth_width, depth_height), CV_8UC3);
  264.  
  265.     KF.init(6, 3, 0);
  266.     KF.transitionMatrix = (cv::Mat_<float>(6, 6) << 1, 0, 0, 1, 0, 0,
  267.         0, 1, 0, 0, 1, 0,
  268.         0, 0, 1, 0, 0, 1,
  269.         0, 0, 0, 1, 0, 0,
  270.         0, 0, 0, 0, 1, 0,
  271.         0, 0, 0, 0, 0, 1);
  272.     measurement = cv::Mat_<float>::zeros(3, 1);
  273.  
  274.     //Initaite Kalman
  275.     KF.statePre.setTo(0);
  276.     setIdentity(KF.measurementMatrix);
  277.     setIdentity(KF.processNoiseCov, cv::Scalar::all(.001)); // Adjust this for faster convergence - but higher noise
  278.     setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-1));
  279.     setIdentity(KF.errorCovPost, cv::Scalar::all(.1)); */
  280.    
  281.     //cv::destroyWindow("Depth Image by OpenARK");
  282.    
  283.    
  284. }
  285.  
  286.  
  287.  
  288.  
  289. /***
  290. Reads the amplitude data from the sensor and fills in the matrix
  291. ***/
  292. void SR300Camera::fillInAmps()
  293. {
  294.     //int res = pmdGetAmplitudes(hnd, amps, numPixels * sizeof(float));
  295.     //float * dataPtr = amps;
  296.     //ampMap.data = (uchar *)amps;
  297. }
  298.  
  299.  
  300.  
  301.  
  302. /***
  303. Returns the X value at (i, j)
  304. ***/
  305. float SR300Camera::getX(int i, int j) const
  306. {
  307.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  308.     int flat = j * depth_width * 3 + i * 3;
  309.     return dists[flat];
  310. }
  311.  
  312. /***
  313. Returns the Y value at (i, j)
  314. ***/
  315. float SR300Camera::getY(int i, int j) const
  316. {
  317.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  318.     int flat = j * depth_width * 3 + i * 3;
  319.     return dists[flat + 1];
  320. }
  321.  
  322. /***
  323. Returns the Z value at (i, j)
  324. ***/
  325. float SR300Camera::getZ(int i, int j) const
  326. {
  327.     //int flat = j * dd.img.numColumns * 3 + i * 3;
  328.     int flat = j * depth_width * 3 + i * 3;
  329.     return dists[flat + 2];
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement