Opencv Install For Mac Rating: 5,0/5 687 votes

In this tutorial, we'll use the Python 3.8. Start by installing the Command Line Tools for macOS. Easy install OpenCV for python on Mac, Ubuntu, and Raspberry Pi. Installing OpenCV has never been easy and always required a lot of careful usage of the command line to build from source.

Install OpenCV 4.0 on Mac OS

Step 1. Install XCode
1.1. First, we need to install the latest XCode.
Grab the info and download the binary from the below Apple website:
https://developer.apple.com/xcode/

Or, you may download XCode from Apple App Store -> find the XCode app -> install XCode.

Godzilla games for mac. 1.2. After installation completed, open XCode and accept license agreement.


Step 2. Install Homebrew

2.1. Install the Mac community package manager, Homebrew.

Then, update the Homebrew definitions:

2.2. Add Homebrew to PATH
In order to make our work simple, don't forget to add Homebrew to our working PATH at .bash_profile file.


Step 3. Install OpenCV prerequisites using Homebrew

3.1. Install Python 3.6

Verify whether the Python installation OK or not by typing the below command:

3.2. Install CMake and QT (optional)
(This code is only executed if you need CMake and QT for your development)
If you need to work the OpenCV project with CMake and QT, you can execute the below code. If you only need Python, skip this procedure.

Later in this installation, we need to specify the QT path to a variable:

*make sure the above path is available on your environment. Check the availability by making sure the path (ls -l /usr/local/Cellar)

Step 4: Install Python dependencies for OpenCV 4
We will install the Python dependencies for OpenCV 4 in this procedure.

Now the pip is installed, next, we can install virtualenv and virtualenvwrapper, two tools for managing virtual environments. Python virtual environments are a best practice for Python development and recommended to take full advantage of them.

The virtualenvwrapper tool provides various of terminal commands:
-mkvirtualenv : Used to “make a virtual environment”
-rmvirtualenv : Destroys a virtual environment
-workon : Activates a virtual environment
-deactivate : Deactivates the current virtual environment
-Refer to this link for more information.

Now, let’s create a Python virtual environment for OpenCV.
In this command, the virtual environment for Python 3 and OpenCV4 will be defined as py3cv4. You may take your own virtual environment name as you wish.

The command result may look like this:

Next, let’s install NumPy, CMake, and other library while we’re inside the environment.


Step #5: Compile OpenCV 4 for macOS

5.1. Download OpenCV 4
Navigate to our working folder and download both opencv and opencv_contrib.
In this command, we will create opencv and opencv_contrib folder inside home folder.

Navigate back to OpenCV repo and create & enter a build directory.

Now we are ready to execute the CMake.
Make sure to use the workon command before executing the cmake command as shown below.
Notes: I am using py3cv4 as virtual environment. If you are using other name for virtual environment, you should change the below code as your own environment.
This command will take several times. (in my environment it took about 45 minutes ^^;)

Notes:
-OPENCV_ENABLE_NONFREE=ON
This is required for OpenCV 4 if you want access to patented algorithms for educational purposes.
-Once CMake has finished, you will see the following information in the terminal:

*If you need QT in your project, don't forget to add the below command.
The QT5PATH should be defined in the previous step (3.2. Install CMake and QT)

Up to this step, if your CMake output is good to go you can kick off the compilation via:

When the process is finished 100%, the screenshot should be like this:

5.2. Install imutils


Step 6: Test your macOS + OpenCV 4

Lets Run Our First OpenCV Application!
You may clone one of my OpenCV sample from the below GitHub resource.
https://github.com/fahmifahim/opencv

Sample result:

Install opencv mac c++ cmake

Reference:
https://www.learnopencv.com/install-opencv-4-on-macos/
https://www.pyimagesearch.com/2018/08/17/install-opencv-4-on-macos/