首先 tap homebrew/science

brew tap homebrew/science

The tap command allows Homebrew to tap into another repository of formulae. Once you've done this you've expanded your options of installable software.

These additional Git repos (inside usr/local/Library/Taps) describe sets of package formulae that are available for installation.

homebrew/science 中有两个版本的 opencv,分别是 opencvopencv3 。一个是 stable 2.4.13.2 版本的,另一个是 stable 3.2.0 版本的,你可以通过 brew info 命令查看它们的具体信息。

e.g

~/ » brew info opencv3
homebrew/science/opencv3: stable 3.2.0 (bottled), HEAD [keg-only]
Open source computer vision library, version 3
http://opencv.org/
Not installed
From: https://github.com/Homebrew/homebrew-science/blob/master/opencv3.rb
==> Dependencies
Build: cmake ✘, pkg-config ✔
Required: jpeg ✔, libpng ✔, libtiff ✔
Recommended: eigen ✘, openexr ✘, numpy ✘
Optional: ffmpeg ✘, gphoto2 ✘, gstreamer ✘, jasper ✘, libdc1394 ✘, openni ✘, openni2 ✘, qt5 ✘, tbb ✘, vtk ✘
==> Requirements
Optional: cuda ✘, java ✘, python3 ✔
==> Options
--c++11
        Build using C++11 mode
--with-contrib
        Build "extra" contributed modules
--with-cuda
        Build with CUDA v7.0+ support
--with-examples
        Install C and python examples (sources)
--with-ffmpeg
        Build with ffmpeg support
--with-gphoto2
        Build with gphoto2 support
--with-gstreamer
        Build with gstreamer support
--with-jasper
        Build with jasper support
--with-java
        Build with Java support
--with-libdc1394
        Build with libdc1394 support
--with-nonfree
        Enable non-free algorithms
--with-opengl
        Build with OpenGL support (must use --with-qt5)
--with-openni
        Build with openni support
--with-openni2
        Build with openni2 support
--with-python3
        Build with python3 support
--with-qt5
        Build the Qt5 backend to HighGUI
--with-quicktime
        Use QuickTime for Video I/O instead of QTKit
--with-static
        Build static libraries
--with-tbb
        Enable parallel code in OpenCV using Intel TBB
--with-vtk
        Build with vtk support
--without-eigen
        Build without eigen support
--without-numpy
        Use a numpy you've installed yourself instead of a Homebrew-packaged numpy
--without-opencl
        Disable GPU code in OpenCV using OpenCL
--without-openexr
        Build without openexr support
--without-python
        Build without Python support
--without-test
        Build without accuracy & performance tests
--HEAD
        Install HEAD version
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

opencv3 and opencv install many of the same files.

然后,根据 info 命令列出的编译选项,组织适合自己需求的命令(参数放opencv3右边会被忽略):

~/ » brew install --with-examples --with-contrib --with-python3 opencv3

完成安装后,brew 会告诉你

This formula is keg-only, which means it was not symlinked into /usr/local.

opencv3 and opencv install many of the same files.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/opencv3/bin:$PATH"' >> ~/.zshrc

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/opencv3/lib
    CPPFLAGS: -I/usr/local/opt/opencv3/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/opencv3/lib/pkgconfig


If you need Python to find bindings for this keg-only formula, run:
  echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth

根据它所说的,在 ~/.zshrc (或其他sh配置文件中)添加:

export PATH="$PATH:/usr/local/opt/opencv3/bin"
export PYTHONPATH="$PYTHONPATH:/usr/local/opt/opencv3/lib/python3.6/site-packages"

source ~/.zshrc 后,打开 python3 解释器确认是否能够导入 cv2 模块:

Python 3.6.1 (default, Mar 23 2017, 16:49:06)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv'
>>> import cv2
>>> cv2.__version__
'3.2.0'

如果没有错误,那么 congratulation!