系统:Centos 6.5
从github或者opencv的官网下载opencv库;
github下载链接:https://github.com/Itseez/opencv.git
1、安装依赖包(很重要)
yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils swig python libtool nasm
如果不安装上述依赖包,到最后调用opencv时会出错,如下:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow,...................
此时只能将编译好的opencv删除再重新编译,即将/usr/local/lib和/usr/local/include/中的有关的删除
mv /usr/local/lib/libopencv*
mv -fr /usr/local/include/opencv
mv -fr /usr/local/include/opencv2
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow,...................
此时只能将编译好的opencv删除再重新编译,即将/usr/local/lib和/usr/local/include/中的有关的删除
mv /usr/local/lib/libopencv*
mv -fr /usr/local/include/opencv
mv -fr /usr/local/include/opencv2
然后再重新编译opencv.
2.yum -y install cmake 来安装cmake
3.进入opencv目录,然后执行:
cmake CMakeLists.txt
4. make && make install
5. 添加路径
如果不添加路径到环境变量当运行测试程序时会出错如下:
./drawing: error while loading shared libraries: libopencv_calib3d.so.2.4: cannot open shared object file: No such file or directory
解决方法:
(1)用ln将需要的so文件链接到/usr/lib或者/lib这两个默认的目录下边
ln -s /usr/local/lib/*.so /usr/lib
sudo ldconfig
(2)修改LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
测试代码:
如果不添加路径到环境变量当运行测试程序时会出错如下:
./drawing: error while loading shared libraries: libopencv_calib3d.so.2.4: cannot open shared object file: No such file or directory
解决方法:
(1)用ln将需要的so文件链接到/usr/lib或者/lib这两个默认的目录下边
ln -s /usr/local/lib/*.so /usr/lib
sudo ldconfig
(2)修改LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
sudo ldconfig
5、测试: 测试代码:
编写Makefile文件:
Makefile的编写有两种方式:
第一种就是加$(LIBS), $(LABS),$(CFG);
第二种是只有$(ORG)。
第二种是最原始的编译方式,也就是直接加载动态库。第一种是通过pkg-config文件来加载的,我们可以在/usr/local/lib/pkgconfig/目录下找到一个opencv.pc文件,如果没有这个文件的话,需要:
cp /usr/local/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig
然后:
export PKG_CONFIG_PATH=$PKG_CONFGI_PATH:/usr/local/lib/pkgconfig
不然会报如下错误:
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
/tmp/ccUa2HAA.o: In function `main' :
code.cpp:(.text+0x1b): undefined reference to `cvLoadImage'
code.cpp:(.text+0x3c): undefined reference to `cvNamedWindow'
code.cpp:(.text+0x4d): undefined reference to `cvShowImage'
code.cpp:(.text+0x57): undefined reference to `cvWaitKey'
code.cpp:(.text+0x61): undefined reference to `cvDestroyWindow'
code.cpp:(.text+0x6d): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
make: *** [drawing] Error 1
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
/tmp/ccUa2HAA.o: In function `main' :
code.cpp:(.text+0x1b): undefined reference to `cvLoadImage'
code.cpp:(.text+0x3c): undefined reference to `cvNamedWindow'
code.cpp:(.text+0x4d): undefined reference to `cvShowImage'
code.cpp:(.text+0x57): undefined reference to `cvWaitKey'
code.cpp:(.text+0x61): undefined reference to `cvDestroyWindow'
code.cpp:(.text+0x6d): undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
make: *** [drawing] Error 1
仔细查看opencv.pc文件:
其实也就是指定动态库的目录,所以我们也可以直接像第二种方式一样指定库文件的目录就可以编译了。
最后,我们就可以看到如下的效果了: