I have trawled through all of Google's and Stack Overflow's search results in order to install stupid OpenCV on my Mac and all the methods have been completely useless.
I started trying to do this with OS X 10.8 and its XCode version and it didn't work. Now I'm on 10.9 and XCode 5.0.1.
I have tried mainly 2 methods:1) Downloading the .dmg file from OCV website and doing the CMAKE routine. When I get to the make -j8 step, at around 36% it gives me errors such as:
How to Install OpenCV on Mac OS with PYTHON. In this post, we will provide step by step instructions for installing OpenCV 3.3.0 (C++ and Python) on MacOS and OSX. If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda. In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. The first step towards reading a video file is to create a VideoCapture object.
-make[1]: * [modules/imgproc/CMakeFiles/opencv_perf_imgproc.dir/all] Error 2
-Documents/Libraries/opencv-2.4.6.1/modules/ts/include/opencv2/ts/ts_perf.hpp:480:12: note: expanded from macro 'CV_PERF_TEST_MAIN_INTERNALS' while (++argc >= (--argc,-1)) {VA_ARGS; break;} /this ugly cons... ^ ~~1 error generated.make[2]: ** [modules/imgproc/CMakeFiles/opencv_perf_imgproc.dir/perf/perf_main.cpp.o] Error 1
2) Using Homebrew at the end of which, even if I added the python path, when I do 'import cv' it gives me a 'no module named cv' error
I tried Macports as well a while ago so I don't remember the steps, but it didn't work.
It goes without saying that I've tried different versions of OCV and all have given me issues at the make step.
Any other solutions?
Rupert Cobbe-WarburtonRupert Cobbe-Warburton2 Answers
There is a great tutorial located here. It highlights that the version you are trying to use 'is not compatible'. I am thinking that you should try installing the version stated in those instructions and see how you go.
The following instructions are taken directly from that link:
INSTALLING CMAKE
First you need to download the .dmg file under Binary Distribution of CMake here. At the time of I installed this, the file name is cmake-2.8.11.2-Darwin64-universal.dmg.
Upon completion of the installation you will be prompted whether to put CMake in /usr/bin, select Yes and finish installation
To ensure that CMake has been successfully installed, type cmake -version in Terminal
INSTALLING OPENCV
First, download the tar file of OpenCV 2.4.3 stable version here and DO NOT use the latest version. I struggled with the installation because I used OpenCV 2.4.6.1 and it's not yet compatible.
Extract the tar file in a folder and go to that folder through the terminal, e.g. cd Downloads/OpenCV-2.4.3
Now, each of the following line is to be executed in Terminal and must be executed successfully (no error)
mkdir buildcd buildcmake -G 'Unix Makefiles' ..make -j8sudo make install
You are all set to use OpenCV!
Steps for those who prefer to build opencv from source
I prefer to use cmake-gui instead of entirely doing this from command line, since that gives you more idea about the options available for opencv configuration and bring you in better position if you want to customize opencv build tomorrow ( like enabling java wrapper or OpenCL etc ).
- Download source either from opencv.org(1) or opencv githubrepo(2)
- Start cmake-gui.
- Set
source pathto downloaded opencv directory andbuild pathtoyour choice and click Configure button. - Specify generator as unix makefiles and pressContinue
- Choose compilers and click Done button.
- An options page with errors might be listed.
- Hovering mouse over any option gives a tooltip about the option. Edit option(s) as you feel fit. E.g.
CMAKE_INSTALL_PREFIX- change opencv install pathBUILD_SHARED_LIBS- build static or shared library. etc
Then click Configure again. If there are still errors, edit the option(s) causing the error and click Configure button. Repeat the edit / Configure cycle until there are no more errors. Once there are no more errors, proceed to step (8).
A minimal sample config of the WITH tab below. Feel free to play with options. This might cause build problems initially. But your understanding of build, will be better.
- Click Generate to generate make files.
- Type
make( in terminal at build folder ) - Type
make install
Not the answer you're looking for? Browse other questions tagged opencvcmake or ask your own question.
This tutorial provides step-by-step instructions to install OpenCV 4 (with Python bindings) on your macOS machine.
OpenCV 4 was released on November 20th, 2018.
I originally wrote this blog post when the alpha version was released, and it has now been updated on November 30th, 2018 to support the official release.
OpenCV 4 comes with new features, particularly in the DNN module for deep learning.
To learn how to install OpenCV 4 on macOS, just keep reading.
Looking for the source code to this post?
Jump right to the downloads section.
Install OpenCV 4 on macOS
In this blog post we’re going to install OpenCV 4 on macOS. OpenCV 4 is packed with new features, many of which are deep-learning focused.
Note: If you landed on the wrong install tutorial (perhaps you want to install OpenCV on Ubuntu or your Raspberry Pi), then you should visit my OpenCV installation guides page. I post links to all my OpenCV install tutorials there.
First, we’ll install Xcode and set up Homebrew.
From there, we’ll establish Python virtual environments.
Then we’ll compile OpenCV 4 from source. Compiling from source allows us to have full control over the compile and build as well as to install the full OpenCV 4 build. I’ll cover alternative methods (pip and Homebrew) in future installation guides (neither of these methods can be covered until OpenCV 4 is officially released).
Finally, we’ll test out our OpenCV 4 install and get our feet wet with a real OpenCV project.
Let’s begin!
Step #1: Install Xcode
First we need to install Xcode.
To install Xcode, fire up the Apple App Store, find the Xcode app, and install. You’ll need to wait patiently as this figure indicates:
Figure 1: Xcode is a dependency for Homebrew and therefore OpenCV 4 on macOS. To install Xcode, launch the App Store, find Xcode, and run the installation.
After Xcode has installed we need to accept a license agreement. Launch a terminal and enter the following command:
To accept the license, simply scroll down and accept it.
Once you’ve accepted the license agreement, let’s install Apple Command Line Tools. This is required, so that you’ll have make , gcc , clang , etc. You can install the tools via:
Figure 2: Installing Apple Command Line Tools on macOS.
Click the “Install” button and wait about 5 minutes for the installation to complete.
Step #2: Install Homebrew
For this step we’re going to install the Mac community package manager, Homebrew.
Homebrew runs on Ruby which is a popular programming language. When you’re ready, copy the entire command below to install Homebrew:
$/usr/bin/ruby-e'$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)' |
Note: Make sure you use the <=> button in the code block above to expand the code block before you copy and paste. If you don’t expand the code block you may end up copying pasting part of the command, which will ultimately end with an error installing Homebrew.
Homebrew commands are shortened to brew .
Let’s update the Homebrew definitions:
And now let’s edit our Mac’s bash profile. This script is run each time you launch a terminal. For simplicity, I suggest the nano text editor. If you’re more comfortable with vim or emacs, then go for it.
Edit your bash profile with nano using the following command:
Once you’re actively editing the file, append the following lines to the end to update your PATH :
2 | export PATH=/usr/local/bin:$PATH |
From there, save the profile. If you’re using nano, you’ll see the shortcut keys at the bottom of the window which demonstrate how to save (write) and exit.
Once you’re back in bash, source your bash profile:
Step #3: Install OpenCV prerequisites using Homebrew
In this section we’ll ensure that Python 3.6 is installed. We’ll also install prerequisites for building OpenCV from source.
Install Python 3.6
It is extremely important to use Python 3.6. By default High Sierra and Mojave are coming with Python 3.7 now. It sounds good, but Python 3.7 is unsupported by Keras/TensorFlow (both are used often on this blog) and thus are not a good choice for OpenCV either.
These commands will install Python 3.6.5_1:
2 4 | Python3.6.5(default,Jun172018,12:13:06) [GCC4.2.1Compatible Apple LLVM9.1.0(clang-902.0.39.2)]on darwin Type'help','copyright','credits'or'license'formore information. |
Great! I can see that we have Python 3.6.5 installed now.
Let’s verify one more thing:
2 | /usr/local/bin/python3 |
If you see /usr/local/bin/python3 you are using the Homebrew Python (which is what we desire). If you see /usr/bin/python3 then you are using the system Python and you likely need to fix your bash profile and/or source it.
Take the time now to verify you are using the Homebrew version of Python and not the system version.
Install other prerequisites
OpenCV requires a few prerequisites to be installed before we compile it. These packages are related to either (1) tools used to build and compile, (2) libraries used for image I/O operations (i.e., loading various image file formats from disk such as JPEG, PNG, TIFF, etc.) or (3) optimization libraries.
To install these prerequisites for OpenCV on macOS execute the following commands:
2 4 | $echo-e'n# virtualenv and virtualenvwrapper'>>~/.bash_profile $echo'export WORKON_HOME=$HOME/.virtualenvs'>>~/.bash_profile $echo'export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3'>>~/.bash_profile $echo'source /usr/local/bin/virtualenvwrapper.sh'>>~/.bash_profile |
Then source the file:
You’ll see several lines printed in your terminal indicating that virtualenvwrapper is setup.
The virtualenvwrapper tool provides us a number of terminal commands:
- mkvirtualenv<env_name><options> : Used to “make a virtual environment”
- rmvirtualenv<env_name> : Destroys a virtual environment
- workon<env_name> : Activates a virtual environment
- deactivate : Deactivates the current virtual environment
- You’ll want to read the docs for more information.
Let’s take advantage of the first command to create a Python virtual environment for OpenCV:
2 4 6 8 10 12 | Running virtualenv with interpreter/usr/local/bin/python3 Using base prefix'/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6' Newpython executable in/Users/admin/.virtualenvs/cv/bin/python3.6 Also creating executable in/Users/admin/.virtualenvs/cv/bin/python done. virtualenvwrapper.user_scriptscreating/Users/admin/.virtualenvs/cv/bin/predeactivate virtualenvwrapper.user_scriptscreating/Users/admin/.virtualenvs/cv/bin/postdeactivate virtualenvwrapper.user_scriptscreating/Users/admin/.virtualenvs/cv/bin/preactivate virtualenvwrapper.user_scriptscreating/Users/admin/.virtualenvs/cv/bin/postactivate virtualenvwrapper.user_scriptscreating/Users/admin/.virtualenvs/cv/bin/get_env_details |
Notice that cv is the name of our environment and that I am creating a Python 3 (controlled by the -ppython3 switch) environment.
Important: Take note in the output that Python 3.6 is being utilized for the environment as well (highlighted).
You may name your environment differently if you’d like. I actually like to name my environments like so:
- py3cv4
- py3cv3
- py2cv2
- etc.
Here my py3cv4 virtual environment can be used for Python 3 + OpenCV 4. My py3cv3 virtual environment is used for Python 3 and OpenCV 3. And my py2cv2 environment can be used to test legacy Python 2.7 + OpenCV 2.4 code. These virtual environment names are easy to remember and allow me to switch between OpenCV + Python versions (nearly) seamlessly.
Next, let’s install NumPy while we’re inside the environment.
Chances are, the environment is already active (indicated by (cv) preceding your bash prompt). Just in case, let’s workon (activate) the environment:

Figure 4: We are “in” the cv virtual environment as is denoted by (cv) before the bash prompt. This is necessary prior to installing packages and compiling OpenCV 4.
Each time you wish to use the environment or install packages into it you should use the workon command.
Now that our environment is activated, we can install NumPy:
Step #5: Compile OpenCV 4 for macOS
Compiling from source gives you the most control over your build as opposed to package managers such as pip, Homebrew, and Anaconda.
Package managers are convenient for sure and I’ll cover them in a future installation tutorial, I just want to give you fair warning — while they appear easy on the surface, you won’t be able to get the latest revision of OpenCV and in some cases it won’t work well with virtual environments. You also might be missing features.
I still compile from source all the time and you should definitely learn how if you’re serious about working with OpenCV.
Download OpenCV 4
Let’s download OpenCV.
First, navigate to our home folder and download both opencv and opencv_contrib. The contrib repo contains extra modules and functions which we frequently use here on the PyImageSearch blog.You should be installing the OpenCV library with the additional contrib modules as well.
When you’re ready, just follow along to download both the opencv and opencv_contrib code:
2 4 6 8 10 12 | $cmake-DCMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -DPYTHON3_LIBRARY=`python-c'import subprocess ; import sys ; s = subprocess.check_output('python-config --configdir', shell=True).decode('utf-8').strip() ; (M, m) = sys.version_info[:2] ; print('{}/libpython{}.{}.dylib'.format(s, M, m))'` -DPYTHON3_INCLUDE_DIR=`python-c'import distutils.sysconfig as s; print(s.get_python_inc())'` -DBUILD_opencv_python2=OFF -DINSTALL_PYTHON_EXAMPLES=ON -DOPENCV_ENABLE_NONFREE=ON |
Update 2018-11-30: I added a CMake compile flag to enable nonfree algorithms ( OPENCV_ENABLE_NONFREE=ON ). This is required for OpenCV 4 if you want access to patented algorithms for educational purposes.
Note: For the above CMake command, I spent considerable time creating, testing, and refactoring it. It is effectively self-configuring with no work required on your part. I’m confident that it will save you time and frustration if you use it exactly as it appears. Make sure you click the “<=>” button in the toolbar of the code block above to expand the code block. This will enable you to copy and paste the entire command (it’s pretty lengthy).
Once CMake has finished, scroll up (ever so slightly) until you see the following information in your terminal:
Figure 5: Inspecting CMake output while installing OpenCV 4 on macOS is important. Be sure that your cv virtual environment is being used for Python and NumPy.
Your output should look very similar to mine. What you’re looking for is to ensure that your Python 3 interpreter and NumPy in the virtual environment will both be used. If you don’t see this, then you likely executed CMake without being “in” the cv virtual environment (or whatever you named your Python virtual environment). If that’s the case, no worries — just delete the build directory, activate the virtual environment with the workon command, and re-run CMake.
Next, scroll up a little further to check that your output matches mine where I’ve highlighted:
Figure 6: Verify that the “Non-free algorithms” will be installed in OpenCV 4.
Provided that your CMake output is good to go you can kick off the compilation via:
Note: The -j4 argument is optional and will instruct make to take advantage of 4 CPU cores. You can adjust the numerical value or leave the argument off completely. It doesn’t happen often, but sometimes race conditions may prevent the compile from finishing — at which point you should execute make without the flag.
Take a break while the compilation continues, but don’t forget to come back and create symbolic links.
Opencv 3.1 Download
When make is finished, you should see this:
Figure 6: Compiling OpenCV 4 on macOS has reached 100%. We’re now ready to link OpenCV 4 into our cv environment.
If you’ve reached 100%, then there is one additional command to install OpenCV 4 prior to Step #6:
Step #6: Sym-link OpenCV 4 on macOS to your virtual environment site-packages
Now let’s create what is called a “symbolic link”. We need a link from our cv virtual environment site-packagesto our system site-packages where OpenCV was installed. Effectively, OpenCV will then be “linked” into the virtual environment allowing you to import it into Python scripts as well as the command line interpreter.
Before we make a symbolic link to link OpenCV 4 into our Python virtual environment, let’s determine our Python version:
2 | $python--version |
Using the Python version, we can easily navigate to the correct site-packages directory next (although I do recommend tab-completion in the terminal).
Update 2018-12-20: The following paths have been updated. Previous versions of OpenCV installed the bindings in a different location ( /usr/local/lib/python3.6/site-packages ), so be sure to take a look at the paths below carefully.
At this point, your Python 3 bindings for OpenCV should reside in the following folder:
2 4 6 | $python >>>cv2.__version__ >>>exit() |
Figure 7: To test that OpenCV 4 is installed on macOS, fire up Python in your virtual environment and execute cv2.__version__ after importing cv2.
Provided you see no errors and OpenCV 4 is installed, you’re good to go!
Run your first example with your fresh install of OpenCV 4!
If you’re breaking into the field of computer vision with OpenCV, my guess is that you want to run something cool with OpenCV.
Let’s start by building a simple “document scanner” with OpenCV.
I’m not going to review all the code here — you should check out the original post if you’re interested or when you have more time.
To get started, simply scroll to the “Downloads” and grab the source code + images.
From there just enter the following commands to prepare: