[PYTHON] Steps to install the latest version of OpenCV on OS X El Capitan without Homebrew

Introduction

Here are the steps to compile and install the source code to try out the latest version of OpenCV on your Mac. Although it says El Capitan, it seems that it will work with other versions because we have confirmed the operation with El Capitan.

First of all, please install Xcode as a preparation.

Install CMake

Download and install the dmg file from the following site.

https://cmake.org/download/

By the way, the file I downloaded was "cmake-3.6.2-Darwin-x86_64.dmg".

CMake command line settings

Execute the following command so that CMake installed by GUI can be used on the command line.

python


$ sudo ln -s "/Applications/CMake.app/Contents/bin/ccmake" /usr/local/bin/ccmake
$ sudo ln -s "/Applications/CMake.app/Contents/bin/cmake" /usr/local/bin/cmake
$ sudo ln -s "/Applications/CMake.app/Contents/bin/cmake-gui" /usr/local/bin/cmake-gui
$ sudo ln -s "/Applications/CMake.app/Contents/bin/cmakexbuild" /usr/local/bin/cmakexbuild
$ sudo ln -s "/Applications/CMake.app/Contents/bin/cpack" /usr/local/bin/cpack
$ sudo ln -s "/Applications/CMake.app/Contents/bin/ctest" /usr/local/bin/ctest

Download OpenCV

Download the latest version from GitHub. By the way, opencv_contrib is an additional library, but it makes little sense without it, so please think it is essential (^ _ ^;)

python


$ cd ~
$ git clone https://github.com/Itseez/opencv.git
$ git clone https://github.com/Itseez/opencv_contrib.git

OpenCV installation

Compile and install with the following command.

python


$ cd opencv
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_EXECUTABLE=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/bin/python \
-D PYTHON3_INCLUDE_DIR=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/include/python3.5m \
-D PYTHON3_LIBRARY=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/libpython3.5m.dylib \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
..
$ make -j4
$ sudo make install

By the way, Python3 was installed by following the steps on the following page. The above command is for Anaconda version 3-4.1.1, so if the version is different, edit it according to the version you are using. In addition, the part marked [username] also needs to be edited according to the environment of the Mac on which it is installed.

http://qiita.com/mix_dvd/items/d915752215db67919c06

Installation of additional libraries

When I try to run the sample code from Python, I get an error saying that the library fails to load, so I borrow a compiled library from another program and use it.

python


$ cd ~/
$ git clone https://github.com/open-ephys-GUI-binaries/osx-yosemite.git
$ sudo cp osx-yosemite/libhdf5* /usr/local/lib

[error indication]

python


>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so, 2): Library not loaded: libhdf5_cpp.11.dylib
  Referenced from: /usr/local/lib/libopencv_hdf.3.1.dylib
  Reason: image not found
>>> 

Shell script

Run the following shell script and the installation should be complete ... Please change the user name and the version of each application as appropriate.

install_opencv.sh


#CMake installation
curl -OL https://cmake.org/files/v3.7/cmake-3.7.0-rc1-Darwin-x86_64.dmg
hdiutil mount ./cmake-3.7.0-rc1-Darwin-x86_64.dmg
sudo cp -r /Volumes/cmake-3.7.0-rc1-Darwin-x86_64/CMake.app /Applications
umount /Volumes/cmake-3.7.0-rc1-Darwin-x86_64
#CMake settings
sudo ln -s "/Applications/CMake.app/Contents/bin/ccmake" /usr/local/bin/ccmake
sudo ln -s "/Applications/CMake.app/Contents/bin/cmake" /usr/local/bin/cmake
sudo ln -s "/Applications/CMake.app/Contents/bin/cmake-gui" /usr/local/bin/cmake-gui
sudo ln -s "/Applications/CMake.app/Contents/bin/cmakexbuild" /usr/local/bin/cmakexbuild
sudo ln -s "/Applications/CMake.app/Contents/bin/cpack" /usr/local/bin/cpack
sudo ln -s "/Applications/CMake.app/Contents/bin/ctest" /usr/local/bin/ctest
#OpenCV download
cd ~
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
#OpenCV installation
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_EXECUTABLE=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/bin/python \
-D PYTHON3_INCLUDE_DIR=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/include/python3.5m \
-D PYTHON3_LIBRARY=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/libpython3.5m.dylib \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/numpy/core/include \
-D PYTHON3_PACKAGES_PATH=/Users/[username]/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
..
make -j4
sudo make install
#Install additional libraries
git clone https://github.com/open-ephys-GUI-binaries/osx-yosemite.git
sudo cp osx-yosemite/libhdf5* /usr/local/lib

Recommended Posts

Steps to install the latest version of OpenCV on OS X El Capitan without Homebrew
Install the latest version of Apache httpd 2.4 from source on Cent OS 8
Install the latest version of CMake on Ubuntu 18.04.4 LTS
Steps to install the latest Python on your Mac
How to install Theano on Mac OS X with homebrew
Install the latest version of Git on your Linux server
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
Install NumPy, SciPy, Matplotlib with Homebrew on OS X (as of November 2015)
Use the latest version of PyCharm on Ubuntu
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Very easy to install SciPy on Mac OS X
How to install caffe on OS X with macports
Install PyQt5 with homebrew on Mac OS X Marvericks (10.9.2)
[Is it explosive !?] Setup for using the GPU version of Tensorflow on OS X
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
Put the latest version of Python on linux (Debian) on Chromebook
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
How to update the python version of Cloud Shell on GCP
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
Steps to install MySQL 8.0 on CentOS 8.1
Steps to install matplotlib on Mac
Steps to install VirtualBox on CentOS
Install python3 on Mac (El Capitan)
How to install OpenCV on Mac
Change the Python version of Homebrew
Install mitmproxy on Mac OS X
Steps to install python3 on mac
Install pgmagick on Mac OS X 10.9
Steps to get KeePassX key on OS X with one command line
Build an environment with pyenv, pyenv-virtualenv, jupyter on OS X El Capitan
Steps to install Pinebook Pro's default OS (Manjaro KDE Edition) on eMMC
How to check the version of Django
What to do if you installed pyenv on Mac OS using Homebrew but the python version doesn't switch
Steps to install Python environment on Ubuntu
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
R environment construction with Jupyter (formerly IPython notebook) (on OS X El Capitan 10.11.3)
[2020 version] How to install Python3 on AWS EC2
How to install OpenCV on Jetson Nano Python
Install the latest Cuda + CuDNN on Ubuntu 18.04 @ Spring 2020
[Python] How to install OpenCV on Anaconda [Windows]
When import tkinter is not possible on Mac OS X 10.11.3 (El Capitan) + pyenv + Python 3.5.1.