所需安装环境
1、安装编译环境
$ sudo dnf install gcc gcc-c++ ncurses-devel cmake
$ sudo dnf install gtk2 gtk2-devel gtk2-devel-docs
$ sudo dnf install libgnome-devel gnome-devel-docs
$ sudo dnf install zlib glib atk pango gdk-pixbuf2 gdk-pixbuf2-devel
3、安装依赖的包
$ sudo dnf install gtk+ gtk2-devel pkgconfig python-devel python-numpydoc
$ sudo dnf install libavc1394 libavc1394-devel libdc1394 libdc1394-devel jasper-devel
$ sudo dnf install ffmpeg ffmpeg-devel openjpeg-devel libjpeg-devel libpng-devel libtiff-devel
4、最后,就是OpenCV的安装包了,去官网下载即可,传送门: http://opencv.org/
使用cmake生成用于编译安装的makefile文件
解压包后按顺序执行以下命令
$ cd ~/opencv-3.1.0/
$ cmake -DINSTALL_CREATE_DISTRIB=ON
$ make
$ sudo make install
opencv会默认安装在/usr/local/目录文件下,若要改变安装位置有选项“-D CMAKE_INSTALL_PREFIX=/usr/local",修改后面的参数即可。
其中命令"cmake -DINSTALL_CREATE_DISTRIB=ON"是必须要加上的,否则编译以后目标文件链接库的时候会出现以下错误
/usr/bin/ld: cannot find -lippicv
collect2: error: ld returned 1 exit status
环境配置
1、添加环境变量profile
$ sudo vim /etc/profile在最后添加如下内容
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
保存并退出,并执行以下命令使修改生效
$ sudo source /etc/profile
2、添加另一个环境变量bashrc
$ sudo vim /etc/bashrc在最后添加如下内容
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig保存并退出,并执行以下命令使修改生效
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
$ sudo source /etc/bashrc
$ sudo ldconfig
测试
1、首先检查测试是否安装成功
$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include
$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_world
$ pkg-config --modversion gtk+-2.0
2.24.29
#include "opencv/cv.h"
#include "opencv/highgui.h"
int main(int argc, char **argv)
{
IplImage* pImage;
if(argc==2 && (pImage=cvLoadImage(argv[1],1))!=0)
{
cvNamedWindow("Image",1);
cvShowImage("Image",pImage);
cvWaitKey(0);
cvDestroyWindow("Image");
cvReleaseImage(&pImage);
return 0;
}
return -1;
}
直接编译
$ g++ `pkg-config --cflags opencv` -o test test.cpp `pkg-config --libs opencv`
$ g++ `pkg-config --cflags opencv` -c test.cpp
$ g++ `pkg-config --libs opencv` -o test test.o
最后运行
$ ./test Lenna.jpg