Ubuntu 配置OpenCV C++

如果需要卸载之前版本make uninstallcd ..rm -rf buildrm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/Open...

如果需要卸载之前版本

make uninstall
cd ..
rm -rf build
rm -rf /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
unzip opencv-3.4.0.zip

step0 依赖项

$ 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 libatlas-base-dev gfortran          # 优化opencv功能
$ sudo apt-get install ffmpeg

step1 编译

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make

step2 安装

make install

step3 配置OpenCV环境

还需要为程序指定openvc的头文件位置,这里使用pkg-config命令来完成。

1、进入/etc/profile配置文件,在文件末尾追加添加以下命令:

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

添加完之后使用命令行更新配置文件

source profile

2、查看版本号,验证是否安装成功

pkg-config --modversion opencv

测试程序 test.cpp

  1 #include<opencv2/opencv.hpp>
  2 
  3 using namespace cv;
  4 
  5 int main()
  6 {
  7     Mat src;
  8     src = imread("1.jpg");
  9     imshow("img_text",src);
 10     waitKey(6000);
 11     return 0;
 12 }

编写一个CMakeLists.txt,内容如下:

  1 cmake_minimum_required(VERSION 2.8)
  2 project( test )
  3 find_package( OpenCV REQUIRED )
  4 add_executable( test test.cpp )
  5 target_link_libraries( test ${OpenCV_LIBS} )                                          

 

 

 

本文标题为:Ubuntu 配置OpenCV C++