本文讲述在Linux上使用配置Python调用OpenCV的开发环境
环境:CentOS 6.5 +Python2.7.8+OpenCV2.4.7
0.安装OpenCV环境要求(Installation in Linux)
OpenCV.org的要求:
1.安装gcc
- GCC 4.4.x or later()
- CMake 2.8.7 or higher
- Git(如果直接下载了源码,这个没必要)
- GTK+2.x or higher, including headers (libgtk2.0-dev)
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
- [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
实际测试的时候发现,GCC=4.4.7还是会出问题,所以本人用的是GCC-4.8.3,更新GCC版本请参照http://blog.****.net/dyllove98/article/details/8917485
安装gcc:yum install gcc
验证版本:gcc -v 或者 gcc --version,版本必须>4.4.x
这样安装的GCC版本是4.4的,所以首先升级为4.8,升级方法请看
centos 6 升级gcc,这个方法是靠谱的
安装gcc-c++:yum install gcc-c++
2.安装cmake (因为源里面的cmake版本不对,所以直接源码安装):
下载cmake-3.0.0.tar.gz,编译安装
tar -zxvf cmake-3.0.0.tar.gz cd cmake-3.0.0 ./configure make -j8 make install
3.安装gtk:yum install gtk*
安装pkg-config
gtk*安装完了以后会带有pkg-config
yum info pkgconfig 查看是否已经安装
4.安装Python:下载Python-2.7.8.tgz
tar -zxvf Python-2.7.8.tgz cd Python-2.7.8 ./configure make -j8 make install安装python-dev:
yum install python-dev*
安装numpy:
tar -zxvf numpy-1.8.1.tar.gz
cd numpy-1.8.1
python setup.py install
numpy 和 python安装不成功的话后面opencv make的时候不会生成cv.so,所以这一步要确定numpy安装正确
5.编译安装opencv
cmake -D WITH_IPP=OFF -D WITH_OPENCL=OFF WITH_GTK_2_X=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../../opencv-masterv
make -j8
make install
WITH_IPP 是否开启IPP支持
WITH_OPENCL 是否开启OpenCL支持
WITH_GTK_2_X 图形界面相关的支持,如果使用opencv用到了显示相关的,这个必须开启
参数都在CMakeLists.txt中有介绍,还有很多参数可以设置,参数都有默认值,可以根据需要自己设置
6.c++ 调用测试
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdio.h> using namespace cv; int main( int argc, char** argv ){ Mat image; image = imread( argv[1]); if( argc != 2 || !image.data ){ printf("没有图片\n"); return -1; } namedWindow( "显示图片", CV_WINDOW_AUTOSIZE ); imshow( "显示图片", image ); waitKey(0); return 0; }
编译:g++ test.cpp -lopencv_imgproc -lopencv_highgui -lopencv_core
能显示图片说明c调用的方法ok了
7.python调用测试
进入python命令行,import cv2,如果没有提示说明安装python中也可以使用opencv了
如果出错看看 /usr/local/lib/python2.7/site-packages 下是否存在 cv2.so,如果不存在,找找opencv build时的原因吧
由于我们要用到sift算法,所以先测试下sift能不能用
运行 opencv-master/samples/python2/目录下的 find_obj.py
python find_obj.py --feature=sift imagea.jpg imageb.jpg
8.可能遇到的问题:
1./usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15‘ not found
(http://blog.sina.com.cn/s/blog_a39910330101jads.html)
(http://blog.sina.com.cn/s/blog_62d4d5900101a0z4.html)
3.OpenCV: “libopencv_core.so.2.3: cannot open shared object file: No such file or directory”
opencv的so库目录没加到 ld.so.conf,所以
echo "/usr/local/lib/">>/etc/ld.so.conf
ldconfig
/usr/bin/ld: note: ‘lrint@@GLIBC_2.1‘ is defined in DSO /lib/libm.so.6 so try adding it to the linker command line
/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
http://*.com/questions/14676018/install-opencv-and-compile-c-program-using-gcc
编译时加上 -lm参数
5.Python2.7.3的No module named zlib错误
自己编译安装zlib或者./configure --with-zlib
6.Linking CXX shared library ../../lib/libopencv_imgproc.so /usr/bin/ld: /opt/intel/ipp/lib/ia32/libippcv_l.a(ippcv00242as.o): relocation R_386_GOTOFF against undefined symbol `ippJumpIndexForMergedLibs‘ can not be used when making a shared object/usr/bin/ld: final link failed: Bad valuecollect2: ld return 1
make[2]: * [lib/libopencv_imgproc.so.2.4.3] error 1
make[1]:[modules/imgproc/CMakeFiles/opencv_imgproc.dir/all] error 2
make: ** [all] error 2
https://software.intel.com/en-us/forums/topic/335961
http://answers.opencv.org/question/3871/opencv243-build-with-the-intel-ipp-failed/
如果出现这个错误,就是因为IPP的原因,一般都不用IPP,所以可以编译的时候直接禁用 WITH_IPP = OFF设置