1. 安装xcode

打开appstore下载xcode

2. 安装homebrew

运行如下命令
$:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$:brew update

3. 安装python

#####################################
#安装python到/usr/local/Cellar下面
#####################################
$: brew install python

#####################################
#配置脚本,以使用新安装的python
#####################################
$:touch ~/.bash_profile
$:vi ~/.bash_profile
输入如下内容:
--------------------------------------------------
# Homebrew
export PATH=/usr/local/bin:$PATH
--------------------------------------------------
########################################
#运行脚本,激活新安装的python
########################################
$: source ~/.bash_profile

########################################
#检查python是否配置成功
########################################
$: which python
/usr/local/bin/python

4.安装python虚拟环境

$: pip install virtualenv virtualenvwrapper

################################################
#为了使用虚拟环境方便,配置脚本
################################################
$: vi ~/.bash_profile
输入如下内容:
------------------------------------------------
source /usr/local/bin/virtualenvwrapper.sh
------------------------------------------------

################################
#重新加载配置
################################
$: source ~/.bash_profile

###############################
#创建python虚拟环境
###############################
$:mkvirtualenv mypython

5.安装numpy

$: pip install numpy

6.安装opencv所需包

$: brew install cmake pkg-config
$: brew install jpeg libpng libtiff openexr
$: brew install eigen tbb

7.下载opencv

$: cd ~/
$: mkdir project
$: cd project
$: git clone https://github.com/Itseez/opencv.git
$: cd opencv
$: git checkout 3.0.0
$:
$: cd ~/project
$: git clone https://github.com/Itseez/opencv_contrib
$: cd opencv_contrib
$: git checkout 3.0.0

8.编译opencv

$: cd ~/project/opencv
$: mkdir build
$: cd build
######################################################
#配置opencv仅支持python2
#######################################################
$: cmake -D CMAKE_BUILD_TYPE=RELEASE \
         -D CMAKE_INSTALL_PREFIX=/usr/local \
         -D PYTHON2_PACKAGES_PATH=~/.virtualenvs/mypthon/lib/python2.7/site-packages \
         -D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin \
         -D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers \
         -D INSTALL_C_EXAMPLES=ON \
         -D INSTALL_PYTHON_EXAMPLES=ON \
         -D BUILD_EXAMPLES=ON \
         -D OPENCV_EXTRA_MODULES_PATH=~/project/opencv_contrib/modules ..

#####################################################
#若是python3
#####################################################
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_PACKAGES_PATH=~/.virtualenvs/py35/lib/python3.5/site-packages -D PYTHON3_LIBRARY=/Library/Frameworks/Python.framework/Versions/3.5/bin -D PYTHON3_INCLUDE_DIR=/Library/Frameworks/Python.framework/Versions/3.5/Headers -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/project/opencv_contrib/modules ..

#####################################################
#当然也可以同时支持python2和python3,只需要在cmake选项中把
#PYTHON2_*和PYTHON3_*的选项加上即可
#####################################################
#可能出现如下错误:
----------------------------
    for file: [/Users/dkos/project/opencv/3rdparty/ippicv/downloads/macosx-9662fe0694a67e59491a0dcc82fa26e0/ippicv_macosx_20141027.tgz]
      expected hash: [9662fe0694a67e59491a0dcc82fa26e0]
        actual hash: [d41d8cd98f00b204e9800998ecf8427e]
             status: [22;"HTTP response code said error"]

Call Stack (most recent call first):
  3rdparty/ippicv/downloader.cmake:108 (_icv_downloader)
  cmake/OpenCVFindIPP.cmake:235 (include)
  cmake/OpenCVFindLibsPerf.cmake:12 (include)
  CMakeLists.txt:526 (include)


CMake Error at 3rdparty/ippicv/downloader.cmake:75 (message):
  ICV: Failed to download ICV package: ippicv_macosx_20141027.tgz.
  Status=22;"HTTP response code said error"
Call Stack (most recent call first):
  3rdparty/ippicv/downloader.cmake:108 (_icv_downloader)
  cmake/OpenCVFindIPP.cmake:235 (include)
  cmake/OpenCVFindLibsPerf.cmake:12 (include)
  CMakeLists.txt:526 (include)


-- Configuring incomplete, errors occurred!
See also "/Users/dkos/project/opencv/build/CMakeFiles/CMakeOutput.log".
See also "/Users/dkos/project/opencv/build/CMakeFiles/CMakeError.log".
----------------------------

########################################
#手动下载ippicv包
########################################
$: wget http://www.mirrorservice.org/sites/dl.sourceforge.net/pub/sourceforge/o/op/opencvlibrary/3rdparty/ippicv/ippicv_macosx_20141027.tgz

############################################
#拷贝到对应的目录
############################################
$:cp ippicv_macosx_20141027.tgz ~/project/opencv/3rdparty/ippicv/downloads/macosx-9662fe0694a67e59491a0dcc82fa26e0/

###########################################
#重新执行
###########################################
$: cmake -D CMAKE_BUILD_TYPE=RELEASE \
         -D CMAKE_INSTALL_PREFIX=/usr/local \
         -D PYTHON2_PACKAGES_PATH=~/.virtualenvs/mypthon/lib/python2.7/site-packages \
         -D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin \
         -D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers \
         -D INSTALL_C_EXAMPLES=ON \
         -D INSTALL_PYTHON_EXAMPLES=ON \
         -D BUILD_EXAMPLES=ON \
         -D OPENCV_EXTRA_MODULES_PATH=~/project/opencv_contrib/modules ..

############################################
#若安装成功,内容大体如下:
############################################
-- General configuration for OpenCV 3.0.0 =====================================
--   Version control:               3.0.0
-- 
--   Platform:
--     Host:                        Darwin 15.5.0 x86_64
--     CMake:                       3.5.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 7.3.0.7030031)
--     C++ flags (Release):         -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     Precompiled headers:         NO
--     Extra dependencies:          -framework OpenCL -framework Cocoa -framework QTKit -framework QuartzCore -framework AppKit
--     3rdparty dependencies:       libjpeg libwebp libpng libtiff libjasper IlmImf zlib ippicv
-- 
--   OpenCV modules:
--     To be built:                 hal core flann imgproc ml photo reg surface_matching video face imgcodecs shape videoio highgui objdetect optflow superres tracking ts ximgproc xobjdetect xphoto adas bgsegm bioinspired features2d latentsvm line_descriptor saliency text calib3d ccalib datasets rgbd videostab xfeatures2d stitching python2
--     Disabled:                    world contrib_world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3 viz cvv matlab
-- 
--   GUI: 
--     QT:                          NO
--     Cocoa:                       YES
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        build (ver 1.2.8)
--     JPEG:                        build (ver 90)
--     WEBP:                        build (ver 0.3.1)
--     PNG:                         build (ver 1.5.12)
--     TIFF:                        build (ver 42 - 4.0.2)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--     GDAL:                        NO
-- 
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      NO
--       codec:                     NO
--       format:                    NO
--       util:                      NO
--       swscale:                   NO
--       resample:                  NO
--       gentoo-style:              NO
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     QuickTime:                   NO
--     QTKit:                       YES
--     V4L/V4L2:                    NO/NO
--     XIMEA:                       NO
--     gPhoto2:                     NO
-- 
--   Other third-party libraries:
--     Use IPP:                     8.2.1 [8.2.1]
--          at:                     /Users/dkos/project/opencv/3rdparty/ippicv/unpack/ippicv_osx
--     Use IPP Async:               NO
--     Use Eigen:                   YES (ver 3.2.8)
--     Use TBB:                     NO
--     Use OpenMP:                  NO
--     Use GCD                      YES
--     Use Concurrency              NO
--     Use C=:                      NO
--     Use pthreads for parallel for:
--                                  NO
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
-- 
--   OpenCL:
--     Version:                     static
--     libraries:                   -framework OpenCL
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO
-- 
--   Python 2:
--     Interpreter:                 /Users/dkos/.virtualenvs/mypthon/bin/python2.7 (ver 2.7.11)
--     Libraries:                   /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin (ver 2.7.11)
--     numpy:                       /Users/dkos/.virtualenvs/mypthon/lib/python2.7/site-packages/numpy/core/include (ver 1.11.0)
--     packages path:               /Users/dkos/.virtualenvs/mypthon/lib/python2.7/site-packages
-- 
--   Python 3:
--     Interpreter:                 NO
-- 
--   Python (for build):            /Users/dkos/.virtualenvs/mypthon/bin/python2.7
-- 
--   Java:
--     ant:                         NO
--     JNI:                         /System/Library/Frameworks/JavaVM.framework/Headers /System/Library/Frameworks/JavaVM.framework/Headers /System/Library/Frameworks/JavaVM.framework/Headers
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:
--     mex:                         NO
-- 
--   Documentation:
--     Doxygen:                     NO
--     PlantUML:                    NO
-- 
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              YES
-- 
--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /Users/dkos/project/opencv/build
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/dkos/project/opencv/build

9. 编译opencv

#########################
#编译
#########################
$: make -j4
[ 98%] Built target example_tapi_camshift
[ 98%] Built target example_tapi_tvl1_optical_flow
[ 98%] Built target example_tapi_hog
[ 98%] Built target example_tapi_squares
[100%] Built target example_tapi_bgfg_segm
[100%] Built target example_tapi_pyrlk_optical_flow
[100%] Built target example_tapi_clahe

#############################
#安装
#############################
$: sudo make install
$: cd .virtualenvs/mypthon/lib/python2.7/site-packages/
$: ls -la cv2.so
-rwxr-xr-x  1 root  staff  2021204  5 28 04:08 cv2.so

10. 测试

这里写图片描述

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐