1. 程式人生 > >如何從點雲建立距離影象(How to create a range image from a point cloud)

如何從點雲建立距離影象(How to create a range image from a point cloud)

本教程演示如何從點雲和給定感測器位置建立距離影象。該程式碼建立了漂浮在觀察者前方的矩形示例點雲。

#程式碼 首先,在你最喜歡的編輯器中建立一個叫做range_image_creation.cpp的檔案,並在其中放置下面的程式碼:

#include <pcl/range_image/range_image.h>

int main (int argc, char** argv) {
  pcl::PointCloud<pcl::PointXYZ> pointCloud;
  
  // Generate the data
  for (float y=-0.5f; y<=0.5f; y+=0.01f) {
    for (float z=-0.5f; z<=0.5f; z+=0.01f) {
      pcl::PointXYZ point;
      point.x = 2.0f - y;
      point.y = y;
      point.z = z;
      pointCloud.points.push_back(point);
    }
  }
  pointCloud.width = (uint32_t) pointCloud.points.size();
  pointCloud.height = 1;
  
  // We now want to create a range image from the above point cloud, with a 1deg angular resolution
  float angularResolution = (float) (  1.0f * (M_PI/180.0f));  //  1.0 degree in radians
  float maxAngleWidth    = (float) (360.0f * (M_PI/180.0f));  // 360.0 degree in radians
  float maxAngleHeight    = (float) (180.0f * (M_PI/180.0f));  // 180.0 degree in radians
  Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
  pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
  float noiseLevel=0.00;
  float minRange = 0.0f;
  int borderSize = 1;
  
  pcl::RangeImage rangeImage;
  rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,
                                  sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
  
  std::cout << rangeImage << "\n";
}

#說明 讓我們看看這個部分:

#include <pcl/range_image/range_image.h>

int main (int argc, char** argv) {
  pcl::PointCloud<pcl::PointXYZ> pointCloud;
  
  // Generate the data
  for (float y=-0.5f; y<=0.5f; y+=0.01f) {
    for (float z=-0.5f; z<=0.5f; z+=0.01f) {
      pcl::PointXYZ point;
      point.x = 2.0f - y;
      point.y = y;
      point.z = z;
      pointCloud.points.push_back(point);
    }
  }
  pointCloud.width = (uint32_t) pointCloud.points.size();
  pointCloud.height = 1;

This includes the necessary range image header, starts the main and generates a point cloud that represents a rectangle.

  float angularResolution = (float) (  1.0f * (M_PI/180.0f));  //  1.0 degree in radians
  float maxAngleWidth    = (float) (360.0f * (M_PI/180.0f));  // 360.0 degree in radians
  float maxAngleHeight    = (float) (180.0f * (M_PI/180.0f));  // 180.0 degree in radians
  Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
  pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
  float noiseLevel=0.00;
  float minRange = 0.0f;
  int borderSize = 1;

這部分定義了我們想要建立的距離影象的引數。 角解析度假定為1度,這意味著由相鄰畫素表示的光束相差一度。 maxAngleWidth = 360maxAngleHeight = 180意味著我們模擬的距離感測器具有完整的周圍360度檢視。您始終可以使用此設定,因為範圍影象只會被裁剪為只有自動觀察到某些區域的區域。但是,您可以通過減少值來節省一些計算。例如,對於面向前方的180度視角的鐳射掃描器,在感測器後面不能觀察到任何點,maxAngleWidth = 180就足夠了。 sensorPose將虛擬感測器的6DOF位置定義為roll = pitch = yaw = 0的原點。 coordinate_frame = CAMERA_FRAME告訴系統x向右,y向下,z軸向前。另一種選擇是LASER_FRAMEx向前,y向左,z向上。 對於noiseLevel = 0,範圍影象是使用正常的z緩衝區建立的。然而,如果你想平均落在同一個單元格中的點數,你可以使用更高的值。0.05意味著,所有與最近點距離5cm的點都用於計算範圍。 如果minRange大於0,所有靠近的點都將被忽略。 borderSize大於0會在裁剪影象時在影象周圍留下未觀察點的邊框。

  pcl::RangeImage rangeImage;
  rangeImage.createFromPointCloud(pointCloud, angularResolution, maxAngleWidth, maxAngleHeight,sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
  std::cout << rangeImage << "\n";

剩下的程式碼使用給定的引數從點雲中建立距離影象,並在終端上輸出一些資訊。

範圍影象來自PointCloud類,其點包含成員xyz和範圍。有三種觀點。有效點的實際範圍大於零。未觀察點具有x = y = z = NANrange= -INFINITY。遠端點具有x = y = z = NANrange= INFINITY(無窮大)

#編譯和執行程式 將以下行新增到您的CMakeLists.txt檔案中:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(range_image_creation)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (range_image_creation range_image_creation.cpp)
target_link_libraries (range_image_creation ${PCL_LIBRARIES})

在建立可執行檔案後,您可以執行它。簡單地做:

./range_image_creation

你應該看到以下內容:

range image of size 42x36 with angular resolution 1deg/pixel and 1512 points