Ubuntu18.04编译OpenCV3.4.6+PCL1.9库及用vscode配置OpenCV+PCL

初次接触Ubuntu系统,摸索了两天把环境搭建好,来记录一波~~~

文章目录

    • 1.OpenCV3.4.6的编译与安装
          • 1.1 解压zip包
          • 1.2 安装依赖库和cmake
          • 1.3 执行cmake
          • 1.4 执行make命令
          • 1.5 执行install命令
    • 2.PCL1.9的编译与安装
          • 2.1 安装pcl依赖库
          • 2.2 执行cmake
    • 3.vscode配置OpenCV+PCL
          • 3.1 tasks.json文件---生成out文件
          • 3.2 launch.json文件---执行out文件
          • 3.3 c_cpp_properties.json
    • 4.环境测试
          • 4.1 CMakeLists.txt
          • 4.2 测试代码

1.OpenCV3.4.6的编译与安装

下载opencv,此教程使用opencv3.4.6。
下载链接:http://blog.csdn.net/xingchenbingbuyu/article/details/53301987,下载sources版本。

1.1 解压zip包
unzip opencv-3.4.6.zip
cd opencv-3.4.6
1.2 安装依赖库和cmake
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
1.3 执行cmake

这步在解压缩的文件opencv3.4.6中打开终端,进行如下操作:

mkdir build//新建的编译文件夹
cd build
cmake ..//搭建环境
1.4 执行make命令
sudo make
1.5 执行install命令
sudo make install

至此,opencv的编译过程就结束了,要想使用opencv就需要在vscode中配置了,将和pcl一起配置。

2.PCL1.9的编译与安装

下载PCL并解压,命令和opencv雷同。

2.1 安装pcl依赖库

安装pcl前需要安装一堆依赖库,这个很麻烦,网上教程也挺多,在这里直接复制一个链接参考。
需要注意,vtk依赖库的版本问题,可以用以下命令去搜索libvtk的最新版本(pcl-1.9使用的vtk6.3版本):

sudo apt-cache search libvtk
2.2 执行cmake
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

3.vscode配置OpenCV+PCL

接下来,将opencv和pcl配置到vscode中,这里配置文件一起设置。

3.1 tasks.json文件—生成out文件

为了方便在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"]
        }
    ]
}
3.2 launch.json文件—执行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
                }
            ]
        }
    ]
}

并将其执行。

3.3 c_cpp_properties.json

按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
}

4.环境测试

4.1 CMakeLists.txt
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})
4.2 测试代码

安装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;
}

你可能感兴趣的:(OpenCV,PCL,ubuntu)