Advertisement
lamiastella

Visualize.cpp

May 30th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include "Visualizer.h"
  2.  
  3. pcl::visualization::PCLVisualizer Visualizer::viewer = pcl::visualization::PCLVisualizer("Point Cloud");
  4.  
  5. /***
  6. Maps matrix values to [0, 255] for viewing
  7. ***/
  8. cv::Mat Visualizer::visualizeMatrix(cv::Mat &input)
  9. {
  10.     cv::Mat img;
  11.     cv::normalize(input, img, 0, 255, cv::NORM_MINMAX, CV_8UC3);
  12.     return img;
  13. }
  14.  
  15. /***
  16. RGB depth map visualization
  17. ***/
  18. cv::Mat Visualizer::visualizeDepthMap(cv::Mat &depthMap)
  19. {
  20.     /*cout << "depthMap nrows: " << depthMap.rows << endl;
  21.     cout << "depthMap ncols: " << depthMap.cols << endl;
  22.     cout << "depthMap channels: " << depthMap.channels() << endl;*/
  23.     cv::Mat dp_img;
  24.     cv::normalize(depthMap, dp_img, 0, 255, cv::NORM_MINMAX, CV_8UC1);
  25.     cv::applyColorMap(dp_img, dp_img, cv::COLORMAP_HOT);
  26.     //cv::applyColorMap(dp_img, dp_img, cv::COLORMAP_JET);
  27.     /*cout << "nrows: " << dp_img.rows << endl;
  28.     cout << "ncols: " << dp_img.cols << endl;
  29.     cout << "channels: " << dp_img.channels() << endl;*/
  30.     return dp_img;
  31. }
  32.  
  33. cv::Mat Visualizer::visualizeXYZMap(cv::Mat &xyzMap)
  34. {
  35.     cv::Mat channels[3];
  36.     cv::split(xyzMap, channels);
  37.     return visualizeDepthMap(channels[2]);
  38. }
  39.  
  40. cv::Mat Visualizer::visualizeHand(cv::Mat xyzMap, cv::Point2i finger, cv::Point2i centroid)
  41. {
  42.     cv::Mat displayImg;
  43.     if (xyzMap.type() == CV_32FC3) {
  44.         displayImg = Visualizer::visualizeXYZMap(xyzMap);
  45.     }
  46.     else {
  47.         displayImg = xyzMap;
  48.     }
  49.  
  50.     cv::circle(displayImg, cv::Point(finger.x, finger.y), 2, cv::Scalar(0, 255, 255), 2);
  51.     cv::circle(displayImg, cv::Point(centroid.x, centroid.y), 2, cv::Scalar(0, 255, 0), 2);
  52.     return displayImg;
  53. }
  54.  
  55. void Visualizer::visualizeCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
  56. {
  57.     viewer.setBackgroundColor(0, 0, 0);
  58.     if (!viewer.updatePointCloud(cloud))
  59.         viewer.addPointCloud(cloud);
  60.     viewer.spinOnce();
  61. }
  62.  
  63. cv::Mat Visualizer::visualizePlaneRegression(cv::Mat &input_mat, std::vector<double> &equation, const double threshold, bool clicked)
  64. {
  65.     cv::Mat output_mat;
  66.     if (input_mat.type() == CV_32FC3) {
  67.         output_mat = Visualizer::visualizeXYZMap(input_mat);
  68.     } else {
  69.         output_mat = input_mat;
  70.     }
  71.  
  72.     if (equation.size() < 3)
  73.     {
  74.         return output_mat;
  75.     }
  76.     int rowSize = input_mat.rows;
  77.     int colSize = input_mat.cols;
  78.     cv::Scalar color;
  79.     if (clicked) {
  80.         color = cv::Scalar(255, 255, 0);
  81.     }
  82.     else {
  83.         color = cv::Scalar(0, 255, 0);
  84.     }
  85.  
  86.     int pointsDetected = 0;
  87.     for (int r = 0; r < rowSize; r++) {
  88.         for (int c = 0; c < colSize; c++) {
  89.             double x = input_mat.at<cv::Vec3f>(r, c)[0];
  90.             double y = input_mat.at<cv::Vec3f>(r, c)[1];
  91.             double z = input_mat.at<cv::Vec3f>(r, c)[2];
  92.  
  93.             if (z == 0) {
  94.                 continue;
  95.             }
  96.  
  97.             double z_hat = equation[0] * x + equation[1] * y + equation[2];
  98.             double r_squared = (z - z_hat) * (z - z_hat);
  99.            
  100.             if (r_squared < threshold) {
  101.                 cv::circle(output_mat, cv::Point(c, r), 1, color, -1);
  102.                 pointsDetected++;
  103.             }
  104.         }
  105.     }
  106.     return output_mat;
  107. }
  108.  
  109. void Visualizer::visualizePlanePoints(cv::Mat &input_mat, std::vector<cv::Point2i> indicies)
  110. {
  111.     for (int i = 0; i < indicies.size(); i++) {
  112.         input_mat.at<uchar>(indicies[i].y, indicies[i].x) = (uchar) 255;
  113.     }
  114. }
  115.  
  116. void Visualizer::visulizePolygonMesh(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
  117. {
  118.     if (cloud.get()->width == 0) {
  119.         return;
  120.     }
  121.     // Normal estimation*
  122.     pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
  123.     pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
  124.     pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
  125.     tree->setInputCloud(cloud);
  126.     n.setInputCloud(cloud);
  127.     n.setSearchMethod(tree);
  128.     n.setKSearch(20);
  129.     n.compute(*normals);
  130.     //* normals should not contain the point normals + surface curvatures
  131.  
  132.     // Concatenate the XYZ and normal fields*
  133.     pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
  134.     pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);
  135.     //* cloud_with_normals = cloud + normals
  136.  
  137.     // Create search tree*
  138.     pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
  139.     tree2->setInputCloud(cloud_with_normals);
  140.  
  141.     // Initialize objects
  142.     pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;
  143.     pcl::PolygonMesh triangles;
  144.  
  145.     // Set the maximum distance between connected points (maximum edge length)
  146.     gp3.setSearchRadius(0.025);
  147.  
  148.     // Set typical values for the parameters
  149.     gp3.setMu(2.5);
  150.     gp3.setMaximumNearestNeighbors(100);
  151.     gp3.setMaximumSurfaceAngle(M_PI / 4); // 45 degrees
  152.     gp3.setMinimumAngle(M_PI / 18); // 10 degrees
  153.     gp3.setMaximumAngle(2 * M_PI / 3); // 120 degrees
  154.     gp3.setNormalConsistency(false);
  155.  
  156.     // Get result
  157.     gp3.setInputCloud(cloud_with_normals);
  158.     gp3.setSearchMethod(tree2);
  159.     gp3.reconstruct(triangles);
  160.  
  161.     viewer.setBackgroundColor(0, 0, 0);
  162.     if (!viewer.updatePolygonMesh(triangles))
  163.         viewer.addPolygonMesh(triangles);
  164.     viewer.spinOnce();
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement