How can i visualize point cloud with pcl?
Hi, I'm trying to use pcl (Point Cloud Library) with ros, and I've done working on tutorial. However when I try to visualize point cloud, I got error saying "undefined reference to `pcl::visualization::CloudViewer::CloudViewer()' " even though my code did work without ros environment.
Here is my code. I just want to visualize my points with pcl::visualization::CloudViewer
command. How can i fix it? Thanks in advance.
# include <ros/ros.h>
# include <pcl_ros/point_cloud.h>
# include <pcl/point_types.h>
# include <boost/foreach.hpp>
#include <pcl/visualization/cloud_viewer.h>
typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;
void callback(const PointCloud::ConstPtr& msg){
printf("Cloud: width= %d, height= %d \n", msg->width, msg->height);
BOOST_FOREACH(const pcl::PointXYZRGB& pt, msg->points)
printf("\t(%f, %f, %f)\n", pt.x, pt.y, pt.z);
// create viewer
pcl::visualization::CloudViewer viewer("PointCloudViewer");
viewer.showCloud(msg);
}
int main(int argc, char** argv) {
ros::init (argc, argv, "show_pcl");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe<PointCloud>("/camera/depth/color/points",1,callback);
ros::spin();
}
Asked by KRN.S on 2023-04-18 23:51:58 UTC
Answers
Can you check if the path of the libraries you added is in the correct location on your computer? I think the problem comes from there.
Asked by furkan_ on 2023-04-19 03:58:40 UTC
Comments
Thanks for your response! It was solved.
Asked by KRN.S on 2023-04-20 22:02:03 UTC
We cannot use pcl visualization with pcl_ros. Instead, we must use pcl library itself.
To do that, you need to add pcl library to your CMakeLists.txt as like below.
cmake_minimum_required(VERSION 3.0.2)
project(image_process)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
sensor_msgs
pcl_conversions
pcl_ros
roscpp
)
## find PCL package
find_package(PCL 1.8 REQUIRED)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## include PCL library
include_directories(
include
${PCL_INCLUDE_DIRS}
)
# ## Declare a C++ executable
# ## With catkin_make all packages are built within a single CMake context
# ## The recommended prefix ensures that target names across packages don't collide
add_executable(show_pcl
src/show_pcl.cpp
)
add_dependencies(show_pcl ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(show_pcl
${PCL_LIBRARIES}
${catkin_LIBRARIES}
)
You need both PCL_LIBRARIES
and catkin_LIBRARIES
in target_link_libraries
if you want to use subscribe or publish ros topic.
Asked by KRN.S on 2023-04-20 22:09:50 UTC
Comments