• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

Install OpenCV 3.4.4 on macOS (C++ and Python)

Vishwesh Shrimali
December 3, 2018 Leave a Comment
Install OpenCV 3 Tutorial

December 3, 2018 By Leave a Comment

Install OpenCV 3 on macOS

OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows:

  • OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1.
  • A lot of C API from OpenCV 1.x has been removed.
  • Persistence (storing and loading structured data to/from XML, YAML or JSON) in the core module has been completely reimplemented in C++ and lost the C API as well.
  • New module G-API has been added, it acts as an engine for very efficient graph-based image procesing pipelines.
  • dnn module now includes experimental Vulkan backend and supports networks in ONNX format.
  • The popular Kinect Fusion algorithm has been implemented and optimized for CPU and GPU (OpenCL)
    QR code detector and decoder have been added to the objdetect module.
  • Very efficient and yet high-quality DIS dense optical flow algorithm has been moved from opencv_contrib to the video module.

In this post, we will provide a bash script for installing OpenCV-3.4.4 (C++ and Python 3.7) on macOS – High Sierra and Mojave. We will also briefly study the script to understand what’s going in it. Note that this script will install OpenCV in a local directory and not on the entire system. Let’s jump in 🙂

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 and jupyter notebooks along with other dependencies as described in this blog.

1. Install XCode

Install XCode from App Store.

If XCode available on App Store is not compatible with your OS:

  1. Find XCode version compatible to your OS from this table https://en.wikipedia.org/w/index.php?title=Xcode#Version_comparison_table
  2. Go to this webpage https://developer.apple.com/download/more/
    • Login if you have apple developer account else create your account and login.
    • Search for xcode and download the version compatible to your OS.
  3. Install XCode.
  4. After installation open XCode, and accept xcode-build license when it asks.

2. Install OpenCV

Now that XCode has been installed, we will move on to OpenCV installation.

First, let’s install Homebrew.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update

We will also add Homebrew to PATH.

# Add Homebrew path in PATH
echo "# Homebrew" >> ~/.bash_profile
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile

Next we will install the requirements – Python 3, CMake and Qt 5.

brew install python3
brew install cmake
brew install qt5

QT5PATH=/usr/local/Cellar/qt/5.11.2_1

We will also save current working directory in cwd variable and OpenCV version (3.4.4) in cvVersion.

cwd=$(pwd)
cvVersion="3.4.4"

# Clean build directories
rm -rf opencv/build
rm -rf opencv_contrib/build

# Create directory for installation
mkdir installation
mkdir installation/OpenCV-"$cvVersion"

Now, let’s install the Python libraries and create the Python environment.

sudo -H pip3 install -U pip numpy

# Install virtual environment
sudo -H python3 -m pip install virtualenv virtualenvwrapper
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
echo "VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3" >> ~/.bash_profile
echo "# Virtual Environment Wrapper" >> ~/.bash_profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
cd $cwd
source /usr/local/bin/virtualenvwrapper.sh

############ For Python 3 ############
# create virtual environment
mkvirtualenv OpenCV-"$cvVersion"-py3 -p python3
workon OpenCV-"$cvVersion"-py3
 
# now install python libraries within this virtual environment
pip install cmake numpy scipy matplotlib scikit-image scikit-learn ipython dlib
 
# quit virtual environment
deactivate
######################################

If you are solely a Python user, it is easier to use the following and skip the next steps

pip install opencv-contrib-python==3.4.4.19

Next, let’s clone the OpenCV github repositories.

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.4
cd ..
 
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4
cd ..

cd opencv
mkdir build
cd build
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!

Download Code

Finally, we will use CMake to build OpenCV.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \
            -D INSTALL_C_EXAMPLES=ON \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D WITH_TBB=ON \
            -D WITH_V4L=ON \
            -D OPENCV_SKIP_PYTHON_LOADER=ON \
            -D CMAKE_PREFIX_PATH=$QT5PATH \
            -D CMAKE_MODULE_PATH="$QT5PATH"/lib/cmake \
            -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/OpenCV-"$cvVersion"-py3/lib/python3.7/site-packages \
        -D WITH_QT=ON \
        -D WITH_OPENGL=ON \
        -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
        -D BUILD_EXAMPLES=ON ..
        
make -j$(sysctl -n hw.physicalcpu)
make install

cd $cwd

And that’s it! By now you should have OpenCV installed successfully in your system.

Become an expert in Computer Vision, Machine Learning, and AI in 12-weeks! Check out our course

Computer Vision Course

3. Test OpenCV Installation

3.1. OpenCV in Python

To use cv2 module in Python, we will first activate the Python environment.

workon OpenCV-3.4.4-py3

Next, let’s import the module and verify the OpenCV Version installed.

import cv2
cv2.__version__

3.2. OpenCV in C++

To use OpenCV in C++, we can simply use CMakeLists.txt and specify the OpenCV_DIR variable. The format is as follows:

SET(OpenCV_DIR <OpenCV_Home_Dir>/installation/OpenCV-3.4.4/share/OpenCV/)

Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:

SET(OpenCV_DIR /usr/local/Cellar/OpenCV_installation/installation/OpenCV-3.4.4/share/OpenCV)

Once you have made your CMakeLists.txt, follow the steps given below.

mkdir build && cd build
cmake ..
cmake --build . --config Release

This will generate your executable file in build directory.

Hope this script proves to be useful for you :). Stay tuned for more interesting stuff. In case of any queries, feel free to comment below and we will get back to you as soon as possible.

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this post, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now


Tags: dlib Install macos OpenCV3

Filed Under: Install, OpenCV 3, Tutorial

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • RAFT: Optical Flow estimation using Deep Learning
  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy