官方发布版本:
https://opencv.org/releases/
官方教程地址:
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/tutorials.html
将其解压,我这里还将起移动到home文件夹下面了
如果提醒需要apt-get update,那就先sudo su进入root权限,再sudo apt-get update,然后在执行下面命令:
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
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_SHARED_LIBS=ON \
-D BUILD_STATIC_LIBS=ON \
-D CMAKE_C_COMPILER=/usr/bin/gcc-5 ..
sudo make -j8
-j8的意思代表8线程编译
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
查看版本:
pkg-config --modversion opencv
首先,OpenCV 有在ubuntu上安装的官方文档:https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html
打开终端,安装以下软件包
sudo apt install build-essential
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
第三行中,可能会出现 无法定位软件包libjasper-dev 的错误提示
查阅资料后找到解决方法如下(原文:https://blog.csdn.net/weixin_41053564/article/details/81254410)
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt upgrade
sudo apt install libjasper1 libjasper-dev
其中 libjasper1 是 libjasper-dev 的依赖包
接下来获取 OpenCV 的 source 文件,直接到官网下载:https://opencv.org/releases/
点击对应版本的 Source 即可下载
下载成功后解压,解压到哪个文件夹都可以
进入解压出来的OpenCV-4.1.0文件夹,创建一个新文件夹,我创建的叫 build
进入新文件夹,打开终端,进入这个文件夹
执行命令
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
如果报错,那么去掉 -D后面的空格再尝试一下,即
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
这之中 , /usr/local 是 OpenCV 的安装路径
接下来开始编译
sudo make -j8
编译结束后
sudo make install
这部分参考了 https://blog.csdn.net/sandalphon4869/article/details/94398876#3launchjson_144
首先将OpenCV的库添加到路径,从而可以让系统找到
打开opencv.conf ,打开后很可能是空白
sudo gedit /etc/ld.so.conf.d/opencv.conf
在文末添加
/usr/local/lib
保存后,执行如下命令使得刚才的配置路径生效
sudo ldconfig
接下来配置 bash
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
到这里配置就完成了
转到 opencv-4.1.0/sample/cpp/example_cmake 目录下,打开终端
运行
cmake .
make
./opencv_example
如果摄像头打开,左上角出现 Hello OpenCV 则证明安装成功
sudo apt-get install cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-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 ffmpeg
# 可能需要安装的依赖
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
cmake 过程中需要 ippicv,下载会很慢,所以先准备好, 下载地址:https://www.lanzous.com/i8qmxsh
打开源码中的 opencv/3rdparty/ippicv/ippicv.cmake 文件
找到: “https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/” # 在最后位置
修改为:“file:~/home/serinus/ippicv” # 下载的 ippicv 目录
进入opencv源码目录,创建build 目录,cd build 运行
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv -D WITH_GTK=ON -D WITH_OPENMP=ON ..
解释每个含义:
cmake -D CMAKE_BUILD_TYPE=Release # 选择发布
OPENCV_GENERATE_PKGCONFIG=YES # 创建 PKGCONFIG文件
CMAKE_INSTALL_PREFIX=/usr/local/opencv #安装路径
WITH_GTK=ON # ubuntu 上显示窗口必须加上这个
解决ippicv下载问题:ippicv_2019_lnx_intel64_general_20180723.tgz
解决办法:离线下载后手动安装
第一步,下载ippicv_2019_lnx_intel64_general_20180723.tgz
百度云盘链接: https://pan.baidu.com/s/1eyG7mqKLY6CvUQdh7CbUgA 提取码: 1miu
第二步,修改opencv安装包解压文件夹下’3rdparty/ippicv/ippicv.cmake’配置文件
按以下内容进行修改。
# 将47行的
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/"
# 改为步骤1中手动下载的文件的本地路径(也就是将网络下载的模式改为本地文件下载的模式):
"file:/home/username/" #(仅供参考,根据自己的路径填写)
第三步,重新cmake。
make -j4
sudo make install
# 现在 opencv 已经安装好了
安装好后,include目录在/usr/local/include/opencv4下,库目录在/usr/local/lib下
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
重启系统
reboot
测试安装情况,查看安装版本
kg-config opencv4 --modversion
# or
pkg-config --cflags --libs opencv4
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv4' found
原因:在configrue时,为指定 OPENCV_GENERATE_PKGCONFIG为NO(OFF为关闭),而安装后并未自动生成响应的opencv4.pc文件
网上的许多的教程都未给出该选项
当然,如果你勾选吧,那么,就没有下面4.3的问题啦
sudo gedit /usr/local/lib/pkgconfig/opencv4.pc
添加如下内容
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.2.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
再次pkg-config测试
kg-config --cflags --libs opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
pkg-config 命令介绍:用于获得某一个库/模块的所有编译相关的信息
所有用opencv的其他程序,在编译时,只需要写“pkg-config opencv –libs –cflags”,而不需要自己去找opencv的头文件在哪里,要链接的库在哪里!省时省力!
pkg-config介绍参考
sudo ldconfig 命令介绍: ldconfig是一个动态链接库管理命令,其目的为了让动态链接库为系统所共享。
主要是在默认搜寻目录/lib和/usr/lib以及动态库配置文件/etc/ld.so.conf内所列的目录下,否则需要 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/
搜索出可共享的动态链接库(格式如lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件,
缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表。
linux下的共享库机制采用了类似高速缓存机制,将库信息保存在/etc/ld.so.cache,程序连接的时候首先从这个文件里查找,然后再到ld.so.conf的路径中查找。
为了让动态链接库为系统所共享,需运行动态链接库的管理命令ldconfig,此执行程序存放在/sbin目录下。
ldconfig介绍参考 https://blog.csdn.net/winycg/article/details/80572735
打开vs code,ctrl + shift + p 搜索:c_cpp_properties.json
在 includePath 下加入
"/usr/local/opencv/include/opencv4",
"/usr/local/opencv/include"
#include "opencv2/opencv.hpp"
创建一个文件showEagle.cpp
同级目录下存一张雄鹰的图片eagle.jpg
showEagle.cpp代码如下:
#include
#include
using namespace cv;
int main(int argc, char const *argv[])
{
Mat src = imread("eagle.jpg");
imshow("src", src);
Mat src_gray;
cvtColor(src, src_gray, COLOR_BGR2GRAY); // 注意,有的教程CV_BGR2GRAY,opencv4下会报错
imshow("src_gray", src_gray);
imwrite("eagle_gray.jpg", src_gray);
waitKey(0);
return 0;
开始编译
g++ showEagle.cpp -o showEagle `pkg-config --cflags --libs opencv4`
无限报错报错:error “OpenCV 4.x+ requires enabled C++11 support”
再来一次
g++ -std=c++11 showEagle.cpp -o showEagle `pkg-config --cflags --libs opencv4`