我的安装过程主要按照https://www.cnblogs.com/txg198955/p/5990295.html,并根据自己的实际情况对命令和步骤进行了相应更改。
一、卸载以前版本:在Build(make路径)下打开终端,执行下面的命令
1. $ sudo make uninstall
2. $ cd ..
3. $ sudo rm -r build
4. $sudo rm -r /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*
5.sudo apt-get --purge remove opencv-doc opencv-data python-opencv
二:安装新版本
1、安装依赖项
$ sudo sh dependencies.sh
$ sudo sh opencv3_3_1.sh
$sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler protobuf-c-compiler apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags cython ipython
$ sudo apt-get install cmake cmake-qt-gui
2、使用cmake-gui安装,解压opencv3.4.0,并进入目录,建立build目录并进去build目录打开终端,并输入:$ cmake-gui
3. 在source 目录里添加:文件目录如(../../../opencv-3.4.0),在build目录添加build目录(如:../../../opencv-3.4.0/build),其他项目默认
4. configure
5. generate
6. 进入build目录 $ make -j8
7.在build目录下 $ sudo make install
8. 写入opencv路径:sudo sh -c 'echo "usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
9. sudo ldconfig
三、安装测试
1.创建工作目录
$ mkdir ~/opencv-lena
$ cd ~/opencv-lena
$ gedit DisplayImage.cpp
2. 编译如下代码
#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;
}
3. 创建CMake文件
$ gedit CMakeLists.txt
写入如下内容
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
4. 编译
$ cd ~/opencv-lena
$ cmake .
$ make
5. 执行
此时opencv-lena文件夹中已经产生了可执行文件DisplayImage,下载lena.jpg放在opencv-lena下,运行
$ ./DisplayImage lena.jpg