note.nkmk.me

HomebrewでMacにOpenCV3をインストールしてPython3で使う

Date: 2017-02-18 / tags: Python, OpenCV, 画像処理, Mac
スポンサーリンク

HomebrewでOpenCV3をインストール

以下の環境で動作確認。

  • macOS: 10.12.3
  • Python: 3.6.0
  • Homebrew: 1.1.10

OpenCVは標準では入っていないため、homebrew/scienceのリポジトリをtapする。

$ brew tap homebrew/science

オプションを確認する。

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

RequiredRecommendedOptionalの使いそうなものをインストール。

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

オプションを付けてOpenCV3をインストール。ダウンロードも合わせると結構時間がかかる(40〜50分ぐらい?)ので気長に待つ。

$ brew install opencv3 --with-contrib --with-examples --with-ffmpeg --with-jasper --with-opengl --with-python3 --with-qt5 --with-tbb --without-numpy

無事にエラーなくインストールされると、以下のようなメッセージが表示される。

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"' >> ~/.bash_profile

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
  mkdir -p /Users/xxx/Library/Python/2.7/lib/python/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/xxx/Library/Python/2.7/lib/python/site-packages/homebrew.pth
==> Summary
/usr/local/Cellar/opencv3/3.2.0: 887 files, 100M, built in 23 minutes 14 seconds

Python3のsite-packagesにコピー

Homebrewのインストール後のメッセージでは、python2.7site-packagesに云々と書いてあるが、Python3で使う場合、/usr/local/Cellar/opencv3/3.2.0/lib/python3.6/site-packages/cv2.cpython-36m-darwin.soを、/usr/local/lib/python3.6/site-packagesにコピーする。

以下のバージョンチェックのスクリプトで、所望のバージョンが表示されればOK。

#!/usr/bin/env python3

import sys
import cv2

print(sys.version)
# 3.6.0 ← Pythonのバージョン
print(cv2.__version__)
# 3.2.0 ← OpenCVのバージョン
スポンサーリンク
シェア
このエントリーをはてなブックマークに追加

関連カテゴリー

関連記事