1、安装准备
# 安装最低要求(Ubuntu 18.04 作为参考)
sudo apt update && sudo apt install -y cmake g++ wget unzip
#安装依赖
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
2、下载opencv
# mkdir OpencvRoute && cd OpencvRoute
# wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
# unzip opencv.zip
# mv opencv-4.x opencv
# cd opencv
# makdir build
# cd build
3、cmake
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
4、编译
sudo make -j8
可以使用sudo make -j 8 、sudo make -j4 、sudo make
我这里就使用sudo make -j 8了
5、安装
sudo make install
6、配置环境
用gedit或者vim打开/etc/ld.so.conf
在文件中加上一行 /usr/loacal/lib
其中/user/loacal是opencv安装路径也就是makefile中指定的安装路
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
至此opencv的安装就完成了
如果出现 找不到opencv.pc的问题
手动创建opencv.pc:
cd /usr/local/lib
sudo mkdir pkgconfig && cd pkgconfig
sudo gedit opencv.pc
然后添加:
prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.4.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
写个程序测试下:
首先建立一个文件夹OpenCV_Code,下面新建一个cpp文件,名为test.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
再此文件夹下继续新建一个文件,名为 CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( test )
find_package( OpenCV REQUIRED )
add_executable( test test.cpp )
target_link_libraries( test ${OpenCV_LIBS} )
$ cmake .
$ make
此时OpenCV_Code文件夹中已经产生了可执行文件test,下载Lina.jpg放在OpenCV_Code下,运行
./test Lina.jpg