1.ubuntu 18.04 安裝pcl,命令安装
sudo add-apt-repository ppa:v-launchpad-jochen-sprickerhof-de/pcl
sudo apt-get update
sudo apt-get install libpcl-dev
2.源安装
https://github.com/PointCloudLibrary/pcl
Compiling PCL from source on POSIX compliant systems — Point Cloud Library 0.0 documentation
依赖项:
sudo apt-get update
sudo apt-get install git build-essential linux-libc-dev
sudo apt-get install cmake cmake-gui
sudo apt-get install libusb-1.0-0-dev libusb-dev libudev-dev
sudo apt-get install mpi-default-dev openmpi-bin openmpi-common
sudo apt-get install libflann-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libboost-all-dev
sudo apt-get install vtk6 libvtk6.3 libvtk6-dev libvtk6.3-qt libvtk6-qt-dev
sudo apt-get install libqhull* libgtest-dev
sudo apt-get install freeglut3-dev pkg-config
sudo apt-get install libxmu-dev libxi-dev
sudo apt-get install mono-complete
sudo apt-get install openjdk-8-jdk openjdk-8-jre
git clone https://github.com/PointCloudLibrary/pcl.git
cd pcl
mkdir release
cd release
cmake -DCMAKE_BUILD_TYPE=None -DCMAKE_INSTALL_PREFIX=/usr \ -DBUILD_GPU=ON-DBUILD_apps=ON -DBUILD_examples=ON \ -DCMAKE_INSTALL_PREFIX=/usr ..
make
sudo make install
3.创建pcl_test.cpp文件
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
int main(int argc, char **argv) {
std::cout << "Test PCL !!!" << std::endl;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
uint8_t r(255), g(15), b(15);
for (float z(-1.0); z <= 1.0; z += 0.05)
{
for (float angle(0.0); angle <= 360.0; angle += 5.0)
{
pcl::PointXYZRGB point;
point.x = 0.5 * cosf (pcl::deg2rad(angle));
point.y = sinf (pcl::deg2rad(angle));
point.z = z;
uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
point.rgb = *reinterpret_cast<float*>(&rgb);
point_cloud_ptr->points.push_back (point);
}
if (z < 0.0)
{
r -= 12;
g += 12;
}
else
{
g -= 12;
b += 12;
}
}
point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
point_cloud_ptr->height = 1;
pcl::visualization::CloudViewer viewer ("test");
viewer.showCloud(point_cloud_ptr);
while (!viewer.wasStopped()){ };
return 0;
}
创建CMakeList文件
cmake_minimum_required(VERSION 2.6)
project(pcl_test)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(pcl_test pcl_test.cpp)
target_link_libraries (pcl_test ${PCL_LIBRARIES})
install(TARGETS pcl_test RUNTIME DESTINATION bin)
4.遇到的错误
c++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-5/README.Bugs> for instructions.
search/CMakeFiles/pcl_search.dir/build.make:134: recipe for target 'search/CMakeFiles/pcl_search.dir/src/organized.cpp.o' failed
(1)查找交换空间不足,创建交换空间,tx2有4G交换空间,内存8G,不存在这个问题,创建了很多空间没有用处。
查看交换空间
swapon -s
整体内存
free
内存使用情况
free -m
cat /proc/swaps
(2)linux查看修改线程默认栈空间大小(ulimit -s),默认空间1024=1M,过小,改为10240=10M,可以编译。
查看ulimit -s
临时设置ulimit -s 10240
ulimit -n 10240 进行设置,但是此方法此终端有效,重启之后也会消失
1.打开/etc/security/limits.conf,*代表所有用户。
* soft stack 102400
reboot
5.realsense 點暈數據獲取
安装realsense 2.0
https://github.com/IntelRealSense/librealsense/tree/master/wrappers/pcl
(1)整体编译直接编译cmake时添加 -DBUILD_PCL_EXAMPLES=true下在build wrappers下(2)下载源码realsense,创建rs-pcl.cpp内容如下,注意修改下面example.hpp位置
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
//#include "../../../examples/example.hpp" // Include short list of convenience functions for rendering
#include "/home/user-e290/Desktop/realsense_document/software/librealsense-2.48.0/examples/example.hpp"
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
// Struct for managing rotation of pointcloud view
struct state {
state() : yaw(0.0), pitch(0.0), last_x(0.0), last_y(0.0),
ml(false), offset_x(0.0f), offset_y(0.0f) {}
double yaw, pitch, last_x, last_y; bool ml; float offset_x, offset_y;
};
using pcl_ptr = pcl::PointCloud<pcl::PointXYZ>::Ptr;
// Helper functions
void register_glfw_callbacks(window& app, state& app_state);
void draw_pointcloud(window& app, state& app_state, const std::vector<pcl_ptr>& points);
pcl_ptr points_to_pcl(const rs2::points& points)
{
pcl_ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
auto sp = points.get_profile().as<rs2::video_stream_profile>();
cloud->width = sp.width();
cloud->height = sp.height();
cloud->is_dense = false;
cloud->points.resize(points.size());
auto ptr = points.get_vertices();
for (auto& p : cloud->points)
{
p.x = ptr->x;
p.y = ptr->y;
p.z = ptr->z;
ptr++;
}
return cloud;
}
float3 colors[] { { 0.8f, 0.1f, 0.3f },
{ 0.1f, 0.9f, 0.5f },
};
int main(int argc, char * argv[]) try
{
// Create a simple OpenGL window for rendering:
window app(1280, 720, "RealSense PCL Pointcloud Example");
// Construct an object to manage view state
state app_state;
// register callbacks to allow manipulation of the pointcloud
register_glfw_callbacks(app, app_state);
// Declare pointcloud object, for calculating pointclouds and texture mappings
rs2::pointcloud pc;
// We want the points object to be persistent so we can display the last cloud when a frame drops
rs2::points points;
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Start streaming with default recommended configuration
pipe.start();
while (app) // Application still alive?
{
// Wait for the next set of frames from the camera
auto frames = pipe.wait_for_frames();
auto depth = frames.get_depth_frame();//傳感器讀取深度數據
auto m_width=depth.get_width();//提取寬
auto m_height=depth.get_height();//提取長
float my_dist_to_center =depth.get_distance(m_width / 2, m_height / 2);
std::cout << "The camera is facing an object " << my_dist_to_center << " meters away \r";
// Generate the pointcloud and texture mappings
points = pc.calculate(depth);//calculate 根據depth可以結算出點
auto pcl_points = points_to_pcl(points);//點放入pcl
pcl_ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PassThrough<pcl::PointXYZ> pass;//創建pcl變量,並設置參數
pass.setInputCloud(pcl_points);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*cloud_filtered);
std::vector<pcl_ptr> layers;//創建容器
layers.push_back(pcl_points);//點放入容器內
layers.push_back(cloud_filtered);
draw_pointcloud(app, app_state, layers);
}
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
// Registers the state variable and callbacks to allow mouse control of the pointcloud
void register_glfw_callbacks(window& app, state& app_state)
{
app.on_left_mouse = [&](bool pressed)
{
app_state.ml = pressed;
};
app.on_mouse_scroll = [&](double xoffset, double yoffset)
{
app_state.offset_x += static_cast<float>(xoffset);
app_state.offset_y += static_cast<float>(yoffset);
};
app.on_mouse_move = [&](double x, double y)
{
if (app_state.ml)
{
app_state.yaw -= (x - app_state.last_x);
app_state.yaw = std::max(app_state.yaw, -120.0);
app_state.yaw = std::min(app_state.yaw, +120.0);
app_state.pitch += (y - app_state.last_y);
app_state.pitch = std::max(app_state.pitch, -80.0);
app_state.pitch = std::min(app_state.pitch, +80.0);
}
app_state.last_x = x;
app_state.last_y = y;
};
app.on_key_release = [&](int key)
{
if (key == 32) // Escape
{
app_state.yaw = app_state.pitch = 0; app_state.offset_x = app_state.offset_y = 0.0;
}
};
}
// Handles all the OpenGL calls needed to display the point cloud
void draw_pointcloud(window& app, state& app_state, const std::vector<pcl_ptr>& points)
{
// OpenGL commands that prep screen for the pointcloud
glPopMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
float width = app.width(), height = app.height();
glClearColor(153.f / 255, 153.f / 255, 153.f / 255, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluPerspective(60, width / height, 0.01f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0);
glTranslatef(0, 0, +0.5f + app_state.offset_y*0.05f);
glRotated(app_state.pitch, 1, 0, 0);
glRotated(app_state.yaw, 0, 1, 0);
glTranslatef(0, 0, -0.5f);
glPointSize(width / 640);
glEnable(GL_TEXTURE_2D);
int color = 0;
for (auto&& pc : points)
{
auto c = colors[(color++) % (sizeof(colors) / sizeof(float3))];
glBegin(GL_POINTS);
glColor3f(c.x, c.y, c.z);
/* this segment actually prints the pointcloud */
for (int i = 0; i < pc->points.size(); i++)
{
auto&& p = pc->points[i];
if (p.z)
{
// upload the point and texture coordinates only for points we have depth data for
glVertex3f(p.x, p.y, p.z);
}
}
glEnd();
}
// OpenGL cleanup
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glPushMatrix();
}
创建CMakeLists.txt文件
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(RealSensePCLExample)
if(NOT WIN32)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
endif()
find_package( PCL REQUIRED )
if (PCL_FOUND)
include_directories(${PCL_INCLUDE_DIRS})
add_definitions(${PCL_DEFINITIONS})
link_directories(${PCL_LIBRARY_DIRS})
endif()
set(DEPENDENCIES realsense2 ${PCL_LIBRARIES})
find_package(OpenGL)
if(NOT OPENGL_FOUND)
message(FATAL_ERROR "\n\n OpenGL package is missing!\n\n")
endif()
list(APPEND DEPENDENCIES ${OPENGL_LIBRARIES})
if(WIN32)
list(APPEND DEPENDENCIES glfw3)
else()
find_package(glfw3 REQUIRED)
list(APPEND DEPENDENCIES glfw)
endif()
add_executable(rs-pcl rs-pcl.cpp)
set_property(TARGET rs-pcl PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-pcl ${DEPENDENCIES})
if(NOT glfw3_DIR)
target_include_directories(rs-pcl PUBLIC "${CMAKE_SOURCE_DIR}/third-party/glfw/include")
# target_link_directories(rs-pcl PUBLIC "${CMAKE_BINARY_DIR}/third-party/glfw/src/${CMAKE_BUILD_TYPE}")
endif()
set_target_properties (rs-pcl PROPERTIES
FOLDER "Examples/PCL"
)
foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MT")
endforeach(flag_var)
install(
TARGETS
rs-pcl
RUNTIME DESTINATION
${CMAKE_INSTALL_PREFIX}/bin
)
cmake .&&make