1. 程式人生 > >linux安裝OpenCV以及windows安裝numpy、cv2等python2.7模塊

linux安裝OpenCV以及windows安裝numpy、cv2等python2.7模塊

efi .com build 文件夾 什麽 read /etc ++ vlib

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-3.3.1.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

linux安裝OpenCV以及windows安裝numpy、cv2等python2.7模塊