1. 程式人生 > >利用三維模型生成點雲總結

利用三維模型生成點雲總結

在做三維的深度學習時,可以利用已有的CAD模型得到點雲資料

1.利用pcl庫

pcl庫中有多個函式可以實現模型的讀入和點雲的生成。
I/O模組下有三個函式可以載入資料:

pcl::io::loadPCDFile()
pcl::io::loadOBJFile()
pcl::io::loadPLYFile()

同時tools模組下就包含了兩個轉換函式obj2pcdply2pcd
其中obj2pcd的實現程式碼如下:

int
main (int argc, char** argv)
{
  print_info ("Convert a OBJ file to PCD format. For more information, use: %s -h\n"
, argv[0]); if (argc < 3) { printHelp (argc, argv); return (-1); } // Parse the command line arguments for .pcd and .obj files std::vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd"); std::vector<int> obj_file_indices = parse_file_extension_argument (
argc, argv, ".obj"); if (pcd_file_indices.size () != 1 || obj_file_indices.size () != 1) { print_error ("Need one input OBJ file and one output PCD file.\n"); return (-1); } // Load the OBJ file TicToc tt; print_highlight ("Loading "); print_value ("%s ", argv[obj_file_indices[0]])
; // Load the input file vtkSmartPointer<vtkPolyData> polydata; vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New (); reader->SetFileName (argv[obj_file_indices[0]]); reader->Update (); polydata = reader->GetOutput (); print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", polydata->GetNumberOfPoints ()); print_info (" points]\n"); bool copy_normals = false; parse_argument (argc, argv, "-copy_normals", copy_normals); PCL_INFO ("Copy normals: %s.\n", copy_normals ? "true" : "false"); if (copy_normals) { vtkSmartPointer<vtkPolyDataNormals> ng = vtkSmartPointer<vtkPolyDataNormals>::New (); #if VTK_MAJOR_VERSION < 6 ng->SetInput (polydata); #else ng->SetInputData (polydata); #endif ng->ComputePointNormalsOn (); ng->ComputeCellNormalsOff (); ng->Update (); polydata = ng->GetOutput (); pcl::PointCloud<pcl::PointNormal> cloud; vtkPolyDataToPointCloud (polydata, cloud); // Convert to pcd and save saveCloud (argv[pcd_file_indices[0]], cloud); } else { pcl::PointCloud<pcl::PointXYZ> cloud; vtkPolyDataToPointCloud (polydata, cloud); // Convert to pcd and save saveCloud (argv[pcd_file_indices[0]], cloud); } return (0); }

ply2pcd程式碼實現如下:

//https://github.com/PointCloudLibrary/pcl/tree/master/tools
int
main (int argc, char** argv)
{
  print_info ("Convert a PLY file to PCD format. For more information, use: %s -h\n", argv[0]);

  if (argc < 3)
  {
    printHelp (argc, argv);
    return (-1);
  }

  // Parse the command line arguments for .pcd and .ply files
  std::vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
  std::vector<int> ply_file_indices = parse_file_extension_argument (argc, argv, ".ply");
  if (pcd_file_indices.size () != 1 || ply_file_indices.size () != 1)
  {
    print_error ("Need one input PLY file and one output PCD file.\n");
    return (-1);
  }

  // Command line parsing
  bool format = 1;
  parse_argument (argc, argv, "-format", format);
  print_info ("PCD output format: "); print_value ("%s\n", (format ? "binary" : "ascii"));

  // Load the first file
  pcl::PCLPointCloud2 cloud;
  if (!loadCloud (argv[ply_file_indices[0]], cloud)) 
    return (-1);

  // Convert to PLY and save
  saveCloud (argv[pcd_file_indices[0]], cloud, format);

  return (0);
}

2.使用深度圖恢復點雲

如果在有深度圖的情況下,可以使用相機的內參來獲取點雲資料,下面是利用pcl庫的簡單演算法(from Dominik13993)
tips資料集:
華盛頓大學300個家庭常見物體
斯坦福三維掃描資料集

//詳細解釋:http://www.pcl-users.org/Getting-strange-results-when-moving-from-depth-map-to-point-cloud-tt4025104.html#a4025141

//core process
pointcloud.width = width; 
pointcloud.height = height; 
pointcloud.points.resize (pointcloud.height * pointcloud.width); 

int* depth_data = new int[pointcloud.height * pointcloud.width]; 
//copy the depth values of every pixel in here 

register float constant = 1.0f / 525; 
register int centerX = (pointcloud.width >> 1); 
int centerY = (pointcloud.height >> 1); 
register int depth_idx = 0; 
for (int v = -centerY; v < centerY; ++v) 
{ 
        for (register int u = -centerX; u < centerX; ++u, ++depth_idx) 
        { 
                pcl::PointXYZ& pt = pointcloud.points[depth_idx]; 
                pt.z = depth_data[depth_idx] * 0.001f; 
                pt.x = static_cast<float> (u) * pt.z * constant; 
                pt.y = static_cast<float> (v) * pt.z * constant; 
        } 
} 
pointcloud.sensor_origin_.setZero (); 
pointcloud.sensor_orientation_.w () = 0.0f; 
pointcloud.sensor_orientation_.x () = 1.0f; 
pointcloud.sensor_orientation_.y () = 0.0f; 
pointcloud.sensor_orientation_.z () = 0.0f; 

3.利用渲染工具

TODO from Sharon

4.點雲資料庫

參考:三維點雲資料集總結

在這裡插入圖片描述
icon from easyicon


ref:
pcl:http://pointclouds.org/documentation/
pcl.cn:http://www.pclcn.org
zhihu:https://www.zhihu.com/question/37577447