Python-OpenCV 安装

OpenCV 各个系统安装流程。

OSX 系统

安装依赖包

1
2
3
brew install cmake pkg-config
brew install jpeg libpng libtiff openexr
brew install eigen tbb

安装 OpenCV

1
2
brew tap homebrew/science
brew install opencv

设置路径

Python-OpenCV

1
2
mkdir -p /Users/YI/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/YI/Library/Python/2.7/lib/python/site-packages/homebrew.pth

建立软连接

1
2
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

设置编译路径

1
export OpenCV_DIR=/usr/local/Cellar/opencv@3/3.4.5

XCODE 编译配置:

1
2
3
Header Search Paths -> /usr/local/include/
Library Search Paths -> /usr/local/lib
并把 lib 下的库复制到项目中

Raspberry 系统

1
2
3
4
sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv

CentOS 系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum install cmake
yum install python-devel numpy
yum install gcc gcc-c++
yum install gtk2-devel
yum install libdc1394-devel
yum install libv4l-devel
yum install ffmpeg-devel
yum install gstreamer-plugins-base-devel
yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel
scp -r XXX/opencv-2.4.13 root@IP/home/libs/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j32
sudo make install
cp /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages

编译带上 FFMPEG:

1
2
export PKG_CONFIG_PATH=/usr/local/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_TIFF=ON -D BUILD_EXAMPLES=ON -D BUILD_NEW_PYTHON_SUPPORT=ON --withffmpeg ..

报错解决

问题:

1
2
3
make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
修改 …/modules/highgui/src/cap_ffmpeg_impl.hpp 文件
//修改前
#ifdef WIN32
#define HAVE_FFMPEG_SWSCALE 1
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#else

//修改后
#if 1
#define HAVE_FFMPEG_SWSCALE 1
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#else

Ubuntu 系统

1
2
sudo apt-get install libcv-dev or libopencv-devpy
sudo apt-get install python-opencv or sudo pip3 install opencv-python

XML 资源文件

XML 资源文件