1.下载vscode
选择deb下载,cd到下载目录进行安装
sudo dpkg -i XXX.deb
2、安装OpenCV
1)OpenCV依赖库的安装
2)OpenCV源码下载
3)OpenCV编译
安装OpenCV需要安装很多的依赖库,这时候可以边安装依赖库,一边下载OpenCV源码。
1)依赖库安装:
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
$ sudo apt-get install ffmpeg
2)OpenCV源码下载:
这里需要下载opencv和opencv_contrib(后者会在cmake配置的时候用到),这是因为opencv3以后SIFT和SURF之类的属性被移到了contrib中.
如果不用到以上特殊接口,只需下载opencv,以下关于opencv_contrib的操作可以忽略。
$ wget https://github.com/opencv/opencv/archive/3.2.0.zip
# 从github上直接下载或者clone也可
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip
3)OpenCV编译:
$ cd opencv-3.2.0
$ mkdir build #创建build文件夹用于编译保存
$ cd build
如果没有下载contrib:
$ sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
如果下载安装contrib版本:
$ sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
../opencv_contrib/modules:这里填写自己下载的contrib中modules的地址
找到该文件,终端打开,pwd即可显示绝对路径.
记住最后是一个空格+两个点!!!
cmake过程中由于网络原因很多库无法下载导致cmke出错,因此可以ctrl+C中断cmake,把所需要的文件替换到对应文件夹后,再重新cmake,这样就会成功!
下面是所需要的文件,readme中有详细介绍!
https://github.com/CoderEugene/opencv3.2_CMake
编译之后执行以下过程实现安装:
$ sudo make -j4 #make -j4表示开4个线程来进行编译,看自己电脑性能
$ sudo make install
到这里opencv3.2.0就安装成功了!
终端输入,出现opencv版本号就说明安装成功!
pkg-config --modversion opencv
3.VScode的配置
先下载若干插件
C/C++
CMake Tools
也可以下载汉化插件 Chinese (Simplidied) Language Pack for Visual Studio Code,这个安装后需要重启.
vscode是打开文件夹的,新建一个文件夹例如叫test,里面存一张叫做1.jpg的图片.
在vscode中打开test文件夹,新建一个.cpp文件叫做1.cpp.
里面随便写点内容
#include<iostream>
#include<opencv2/opencv.hpp>
int main()
{
Mat src=imread('1.jpg',1);
imshow("1",src);
waitKey(0);
return 0;
}
为了能用C++编写opencv,需要改三个文件.
launch.json, task.json, c_cpp_properties.json
输入快捷键ctrl+shift+D, 点击设置图标,弹出的选择中选C++(GDB/LLDB),会自动创建项目的launch.json文件。然后更改launch.json中的配置,主要是修改program字段,改为可执行文件所在位置。
{
// 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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
//"cwd": "${workspaceFolder}",
"cwd": "${fileDirname}",//debug可以用相对路径
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
快捷键ctrl+shift+P调出命令面板,并输入C/C++:Edit Configuration(JSON),编辑文件"includePath"选项,加入OpenCV的头文件,通常是"/usr/local/include",如下图所示(别忘记前面家逗号(,),后面不加逗号(,))
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
快捷键ctrl+shift+P调出命令面板,并输入Tasks:Configure Task,默认选择第一项,开始编辑task.json文件,
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.o",
"`pkg-config", "--cflags", "--libs", "opencv`",
"-I", "/usr/local/include",
"-I", "/usr/local/include/opencv",
"-I", "/usr/local/include/opencv2",
"-L", "/usr/local/lib",
"-l", "opencv_core",
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
这三个文件之间复制过去就行.
另外如果安装code-runner这个插件进行debug,需要改一下
左下角的设置->扩展->找到code-runner->找到Exector Map
对setting.json进行修改
{
"workbench.iconTheme": "vscode-icons",
"workbench.colorTheme": "One Monokai",
"explorer.confirmDelete": false,
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt `pkg-config opencv --libs --cflags opencv` && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runhaskell",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script"
}
}
这样就可以在右上角进行愉快的编译了,但是不能断点.
两个方式debug:
附我用的插件
这样就可以愉快使用VScode/C++/opencv3.2.0/code-runner啦!!!
如果有问题,评论区见~