OpenCV的安装和vscode的配置

2024-01-07 20:01:33

在图像处理领域,OpenCV的使用是必不可少的,这里介绍一下OpenCV的安装及其在vscode中的配置

1.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 add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"

	sudo apt update

	sudo apt install libjasper1 libjasper-dev

这个我自己用是有效果的
第二种:

	sudo add-apt-repository "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe"

	sudo apt update

	sudo apt install libjasper1 libjasper-dev

第三种:不用管,其实这个依赖对后续的使用影响不大,甚至可能有些淘汰了

(2)下载OpenCV源码

网站放在这里了:https://opencv.org/releases/,下载对应版本的source就OK了

(3)安装

先解压文件,这里我解压在/home目录下了

进入OpenCV文件夹,创建build目录

	cd opencv4.5.5

	mkdir build

	cd ./build

之后是cmake步骤

	cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local .. 

再执行make指令

	sudo make -j6  #数字取决于你电脑的进程数,数字越大,make越快

	sudo make install

(4)环境配置

我们之前安装的时候 OPENCV_GENERATE_PKGCONFIG=ON 所以在/usr/local/lib/pkgconfig下生成了opencv4.pc文件,里面记录了OpenCV头文件、库文件的路径。这个文件注意一下,因为最后你如果出错,很有可能缺少了这个文件

	sudo gedit /etc/bash.bashrc
	#文件末尾添加以下内容 并保存

	PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

	export PKG_CONFIG_PATH
	#更新

	sudo updatedb

	source /etc/bash.bashrc
	#打开下列文件

	sudo gedit /etc/ld.so.conf.d/opencv.conf 

	 

	# 添加lib路径,在末尾保存退出

	/usr/local/lib

	 

	# 更新

	sudo ldconfig
	#终端输入以下两命令,显示正常则安装成功

	pkg-config --modversion opencv4 #查看版本号

	pkg-config --libs opencv4 #查看libs库

2.vscode的配置

先保证自己C++相关插件下载好
实现一个软连接

	cd   /usr/local/include/

	sudo  ln  -s   opencv4/opencv2   opencv2

配置以下三个文件
c_cpp_properties.json

	{

	    "configurations": [

	        {

	            "name": "Linux",

	            "includePath": [

	                "${workspaceFolder}/**",

	                "/usr/local/include/",

	                "/usr/include/"

	            ],

	            "defines": [],

	            "compilerPath": "/usr/bin/gcc",

	            "cStandard": "c17",

	            "cppStandard": "gnu++14",

	            "intelliSenseMode": "linux-gcc-x64"

	        }

	    ],

	    "version": 4

	}

launch.json

	{

	 

	    "version": "0.2.0",

	    "configurations": [

	        {

	            "name": "g++ - Build and debug active file",

	            "type": "cppdbg",

	            "request": "launch",

	            "program": "${fileDirname}/${fileBasenameNoExtension}",  //程序文件路径

	            "args": [],  //程序运行需传入的参数

	            "stopAtEntry": false,

	            "cwd": "${fileDirname}",

	            "environment": [],

	            "externalConsole": true,   //运行时是否显示控制台窗口

	            "MIMode": "gdb",

	            "setupCommands": [

	                {

	                    "description": "Enable pretty-printing for gdb",

	                    "text": "-enable-pretty-printing",

	                    "ignoreFailures": true

	                }

	            ],

	            "preLaunchTask": "C/C++: g++ build active file",

	            "miDebuggerPath": "/usr/bin/gdb"

	        }

	    ]

	}

tasks.json

	{

	    "tasks": [

	        {

	            "type": "cppbuild",

	            "label": "C/C++: g++ build active file",  /* 与launch.json文件里的preLaunchTask的内容保持一致 */

	            "command": "/usr/bin/g++",

	            "args": [

	                "-std=c++11",

	                "-g",

	                //"${file}",   /* 编译单个文件 */

	                "${fileDirname}/*.cpp",  /* 编译多个文件 */

	                "-o",

	                "${fileDirname}/${fileBasenameNoExtension}",  /* 输出文件路径 */

	 

	                /* 项目所需的头文件路径 */

	                "-I","${workspaceFolder}/",

	                "-I","/usr/local/include/",

	                "-I","/usr/local/include/opencv4/",

	                "-I","/usr/local/include/opencv4/opencv2",

	 

	                /* 项目所需的库文件路径 */

	                "-L", "/usr/local/lib",

	 

	                /* OpenCV的lib库 */

	                "/usr/local/lib/libopencv_*",

	 

	            ],

	            "options": {

	                "cwd": "${fileDirname}"

	            },

	            "problemMatcher": [

	                "$gcc"

	            ],

	            "group": {

	                "kind": "build",

	                "isDefault": true

	            },

	            "detail": "Task generated by Debugger."

	        }

	    ],

	    "version": "2.0.0"

	}

	 

就ok了

文章来源:https://blog.csdn.net/2301_81940605/article/details/135422454
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。