linux安装OpenCV以及windows安装numpy、cv2等python2.7模块

时间:2022-10-07 21:27:32

OpenCV(Open Source Computer Vision Library) 是一个基于BSD许可(开源)发行的跨平台计算机视觉库,它具有C ++,C,Python和Java接口,可以运行在Linux、Windows、Mac OS和Android操作系统上。OpenCV的设计是为了提高计算效率,并将重点放在实时应用程序上, 用优化的C / C ++编写,库可以利用多核处理,实现了图像处理和计算机视觉方面的很多通用算法。

#-----------------------------------------------------------------------------------------------------------

0x00 前言

OpenCV在linux的安装方法可以按照 https://github.com/jayrambhia/Install-OpenCV 来安装。

但有时候因为网速或者别的原因,总之我就觉得里面好繁琐,我从上述项目里提炼出关键的语句,运行即可安装成功。

本次只制作ubuntu下的安装方法,其他系统的安装你们可以参照上述项目。

 

0x01 准备工作

下载opencv源码包(现在最新版本为3.3.1):

https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.3.1/opencv-3.3.1.zip/download

更新下ubuntu:

#change to root
sudo -s
#update
apt-get update

准备下文件dependencies.sh

#!/bin/bash

echo "--- Removing any pre-installed ffmpeg and x264"
sudo apt-get -qq remove ffmpeg x264 libx264-dev

function install_dependency {
    echo "--- Installing dependency: $1"
    sudo apt-get -y install $1
}

install_dependency libopencv-dev
install_dependency build-essential
install_dependency checkinstall
install_dependency cmake
install_dependency pkg-config
install_dependency yasm
install_dependency libtiff5-dev
install_dependency libjpeg-dev
install_dependency libjasper-dev
install_dependency libavcodec-dev
install_dependency libavformat-dev
install_dependency libswscale-dev
install_dependency libdc1394-22-dev
install_dependency libxine2-dev
install_dependency libgstreamer0.10-dev
install_dependency libgstreamer-plugins-base0.10-dev
install_dependency libv4l-dev
install_dependency python-dev
install_dependency python-numpy
install_dependency libtbb-dev
install_dependency libqt5x11extras5
install_dependency libqt5opengl5
install_dependency libqt5opengl5-dev
install_dependency libgtk2.0-dev
install_dependency libfaac-dev
install_dependency libmp3lame-dev
install_dependency libopencore-amrnb-dev
install_dependency libopencore-amrwb-dev
install_dependency libtheora-dev
install_dependency libvorbis-dev
install_dependency libxvidcore-dev
install_dependency x264
install_dependency v4l-utils
#install_dependency ffmpeg
install_dependency unzip

准备下install.sh

#!/bin/bash

version=3.3.1
downloadfile=opencv-${version}.zip
dldir=OpenCV

source dependencies.sh

mkdir -p $dldir
mv $downloadfile $dldir/$downloadfile
cd $dldir
unzip $downloadfile
cd opencv-$version
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j 4
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV" $version "ready to be used"

 

0x02 安装OpenCV

只要opencv-3.3.1.zip、dependencies.sh、install.sh在同一个文件夹下,就可以进行安装:

chmod +x dependencies.sh
chmod +x install.sh
source install.sh

这安装过程会相对漫长。

 

0x03 编译OpenCV程序

例如要编译的文件为pcat.cpp

g++ -ggdb `pkg-config –-cflags opencv` `pkg-config –-libs opencv` pcat.cpp –o pcat

 

0x04 windows安装numpy、cv2等python2.7模块

对于windows,我暂时只需要cv2这个python2.7模块,而这个cv2模块依赖numpy模块

numpy的安装

https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

现在numpy最新版本是1.13.3,python的32位版本就下载numpy-1.13.3+mkl-cp27-cp27m-win32.whl,64位版本就下载numpy-1.13.3+mkl-cp27-cp27m-win_amd64.whl

pip install numpy-xxx.whl

cv2的安装

https://opencv.org/releases.html

现在OpenCV最新的版本是3.3.1,下载 https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.3.1/opencv-3.3.1-vc14.exe/download

这个opencv-3.3.1-vc14.exe点击后会自动解压,包含sources和build文件夹,\opencv\sources\samples可供学习。

打开\opencv\build\python\2.7,有x86和x64两个版本,关键看你的python是什么版本,复制相对应版本的文件夹下面的cv2.pyd

复制到python2.7的安装目录\Lib\site-packages里面即可

之后可以打开python验证是否成功

import cv2