去官网下载opencv,在本教程中选用的时opencv3.4.6,其他版本的配置方法异曲同工。
下载链接http://opencv.org/releases.html,选择sources版本
进入下载的路径,解压下载下来的zip包
cd XXX
unzip opencv-3.4.6.zip
sudo apt-get update
sudo apt-get install vim
sudo apt-get install g++
sudo apt-get install gcc
sudo apt-get install cmake
sudo apt-get install build-essential
sudo apt-get install libgtk2.0-dev
sudo apt-get install libavcodec-dev
sudo apt-get install libavformat-dev
sudo apt-get install libswscale-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install gfortran
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
sudo apt-get install libjasper1 libjasper-dev
cd XXX/opencv-3.4.6
mkdir Release
cd Release
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
如果以下错误:
qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory
执行以下指令:
sudo apt-get install qt5-default
sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
sudo ldconfig
sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存,执行如下命令使得配置生效
source /etc/bash.bashrc
sudo updatedb
cd到opencv-3.4.1/samples/cpp/example_cmake目录下
我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下
cd ..
cd samples
cd cpp
cd example_cmake
cmake .
make
./opencv_example
参考:
ubuntu16.04安装opencv3.4.1教程
Ubuntu16.04安装OpenCV3.4.2
先下好gcc、g++
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install g++
下载VScode:
图形推荐(一键式操作,第一推荐):在ubuntu18.04版本安装vscode
在线umake:Ubuntu 16.04 安装VSCode
本地deb(不会再有问题,第二推荐):
在官网里(https://code.visualstudio.com/Download)里下载Linux的.deb格式
然后安装
cd 文件存在的位置
sudo dpkg -i xxx.deb
下面分为安opencv和不安的情况
由于VScode是以文件夹的形式管理工程的,因此我们首先新建一个文件夹
然后通过VScode打开此文件夹:File→Open Folder
新建main.cpp文件(Ctrl+N)并输入程序:
#include
using namespace std;
int main()
{
cout<<"Hello World\n";
return 0;
}
保存Ctrl+S,记住要写后缀(.cpp),不然
Ubuntu下VScode编译报错:File not recongnized不可识别的文件格式
点击左侧的Debug按钮选择添加配置(Add configuration),然后选择C++(GDB/LLDB),然后选个Default,将自动生成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) 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"
}
]
}
Ctrl+Shift+P打开面板
输入Task:Configure Default Build Task
(有的要输入Edit,再输入Task:Configure Default Build Task)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.main.out",
],// 编译命令参数
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
Ctrl+Shift+P
输入Edit
选择Task:Configure Default Build Task
{
// 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/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"
],
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Ctrl+Shift+P
输入Edit
选择C/C++: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
}
Try.cpp
#include
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");
imshow("Origin",srcImage);
waitKey(0);
return 0;
}
1.jpg
效果
https://blog.csdn.net/weixin_43374723/article/details/84064644#commentBox
https://blog.csdn.net/zzz_xxj/article/details/86568353
Linux 使用cmake构建OpenCV项目
Ubuntu18.04下安装配置VScode以及VS的使用总结