初次接触Ubuntu系统,摸索了两天把环境搭建好,来记录一波~~~
下载opencv,此教程使用opencv3.4.6。
下载链接:http://blog.csdn.net/xingchenbingbuyu/article/details/53301987,下载sources版本。
unzip opencv-3.4.6.zip
cd opencv-3.4.6
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
这步在解压缩的文件opencv3.4.6中打开终端,进行如下操作:
mkdir build//新建的编译文件夹
cd build
cmake ..//搭建环境
sudo make
sudo make install
至此,opencv的编译过程就结束了,要想使用opencv就需要在vscode中配置了,将和pcl一起配置。
下载PCL并解压,命令和opencv雷同。
安装pcl前需要安装一堆依赖库,这个很麻烦,网上教程也挺多,在这里直接复制一个链接参考。
需要注意,vtk依赖库的版本问题,可以用以下命令去搜索libvtk的最新版本(pcl-1.9使用的vtk6.3版本):
sudo apt-cache search libvtk
mkdir build
cmake-gui//缺少什么依赖库补什么库
cd build
make -j10//10表示线程数,这个过程需要耐心等待...
sudo make install
至此,pcl的过程也就结束了!
注:ubuntu安装pcl过程中遇到的问题:
(1)缺少PCAP文件
问题:Could Not find PCAP(missing:PCAP_LIBRARIES PCAP_INCLUDE_DIRS
Could Not find GLEW(missing:GLEW_INCLUDE_DIR GLEW_LIBRARY
解决:
sudo ./configure
sudo make
sudo make install
接下来,将opencv和pcl配置到vscode中,这里配置文件一起设置。
为了方便在VScode里编译C++代码,可以将类似g++ -g main.cpp等g++命令写入VScode的任务系统,具体操作:
按ctrl+shift+p打开命令行,输入Tasks:Run task,然后回车,出现如下提示:
No task to run found. configure tasks...
再回车,
Create tasks.json file from template
然后选择:
Others Example to run an arbitrary external command.
生成默认的tasks.json文件,要改为:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command":"g++",
"args": [
"-g",
"${workspaceFolder}/${fileBasename}",
"-l","/usr/local/include/pcl-1.9",
"-l","/usr/include/eigen3",
"-l","/usr/include/vtk-6.3",
"-l","/usr/include/qhull",
"-l","/usr/include/flann",
"-l","/usr/include/boost",
"-l","/usr/local/lib",
"-l","pcl_io",
"-l","pcl_visualliazation",
"-l","pcl_common",
"-l","vtkFiltering",
"-l","vtkCommon",
"-l","vtkRendering",
"-l","vtkGraphics",
"-l","/usr/include/x86_64-linux-gnu",
"-l","boost_system",
"-o",
"${workspaceFolder}/main.out"
],
"group":{
"kind": "build",
"isDefault": true
}
},
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g","${file}","-o","${fileBasenameNoExtension}.out"]
}
]
}
选中文件夹/cpp文件,按Ctrl+shift+D,点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C++(GDB/LLDB)回车!将自动生成launch.json文件,做如下修改:
"program": "enter program name, for example ${workspaceFolder}/a.out",
改为:
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
launch.json文件最终为:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
并将其执行。
按ctrl+shift+p,输入edit,选择C/C++:edit configuration(json),会生成c_cpp_properties.json,
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}",
"/usr/local/include/pcl-1.9",
"/usr/include",
"/usr/include/vtk-6.3",
"/usr/include/qhull",
"/usr/include/flann",
"/usr/include/boost",
"/usr/include/eigen3",
"/usr/include/eigen3/Eigen/",
"/usr/include/x86_64-linux-gnu/sys"
],
"defines":[],
"browse": {
"path": [
"/usr/include",
"/usr/local/include/pcl-1.9"
]
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
cmake_minimum_required(VERSION 3.0)
project(lvbo-demo)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)
set(THIRD_INCLUDE_DIRS
${OpenCV_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
${PCL_LIBRARIES_DIRS}
)
set(THIRD_PARTY_LIBS
${OpenCV_LIBS}
${PCL_LIBS}
)
add_executable(main main.cpp)
target_link_libraries(main ${THIRD_PARTY_LIBS})
target_link_libraries(main ${PCL_LIBRARIES})
add_executable(test1 test.cpp)
target_link_libraries(test1 ${THIRD_PARTY_LIBS})
安装opencv测试程序
#include
#include
using namespace cv;
using namespace std;
int main()
{
cout << "----------读摄像机内外参数----------"<< endl;
Mat img=imread("/home/em/文档/VSCode2015/demo/imageSets/1.jpg");
cv::imshow("image",img);
cv::waitKey();
return 0;
}
安装PCL测试程序
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv) {
cout << "Test PCL !!!" << endl;
pcl::PointCloud::Ptr point_cloud_ptr (new pcl::PointCloud);
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(r) << 16 |
static_cast(g) << 8 | static_cast(b));
point.rgb = *reinterpret_cast(&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;
}