一、安装Opencv
第一步:下载opencv
下载地址:https://github.com/opencv/opencv
第二步:解压安装
unzip opencv-4.6.0zip
?第三步:安装编译文件
sudo apt-get install cmake
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
第四步:开始编译
# Create build directory
mkdir -p build && cd build
# Configure
cmake ../opencv-4.x
# Build
cmake --build .
第五步:安装
sudo make install
默认 安装地址:/user/local/lib
第六步:配置环境变量
sudo gedit /etc/ld.so.conf
在文件中加上一行
?接着:
sudo ldconfig
修改bash.bashrc文件 命令行:
sudo gedit /etc/bash.bashrc
在文件末尾加入
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
生效:
source /etc/bash.bashrc
测试:
pkg-config opencv –modversion
好像每报错就可以了,应该会出现版本号的。
使用以下官方的程序:
?在这个安装包下open in Terminal
cd samples
cd cpp
cd example_cmake
cmake .
make
./opencv_example
?
?测试成功!
二、下面配置VSCODE
第一步:在桌面新建一个CV2文件夹
第二步:在VSCODE中打开它,并新建一个测试程序
代码:
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");
imshow("Origin",srcImage);
waitKey(0);
return 0;
}
?随便找个图,命名为1.jpg放在CV2文件夹内。
第三步:开始配置launch.json
?选择第一个,按照下面的程序修改,也可以直接copy:
{
// 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) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
第四步:开始配置tasks.json
按F5运行刚才的程序
?
复制下面的程序:
{
// 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",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.main.out",
"-I", "/usr/local/include",
"-I", "/usr/local/include/opencv4",
"-I", "/usr/local/include/opencv4/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"
],
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
?也可能需要查一下opencv目录,这里可能需要再安装一个plocate:sudo apt install plocate
第五步:开始配置c_cpp_properties.json
Ctrl+Shift+P找到Edit Configurations(JSON)
把下面的程序复制过去:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/cpp",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
按下F5:
?顺利!
|