How to Install OpenCV
A step by step guide to Installing OpenCV in Windows, Mac, and Linux
Updated May 3, 2023
Learn more about OpenCV and Computer Vision from this YouTube Channel!
If you’ve been itching to dive into the fantastic world of computer vision, you’ve come to the right place. Today, we’re going to explore how to install OpenCV, a powerful and versatile library that’s the Swiss Army knife of computer vision. Whether you’re using Windows, Mac, or Linux, this guide will get you up and running in no time.
Before we start, it’s essential to mention that this tutorial is meant for people of all experience levels. We’ll be using simple language and engaging examples to keep things easy and enjoyable. So grab a cup of your favorite beverage, and let’s get started!
1. Getting to Know OpenCV
OpenCV, which stands for Open Source Computer Vision Library, is a powerful, open-source computer vision and machine learning library. It contains over 2500 optimized algorithms, written primarily in C and C++, for real-time computer vision applications. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, and much more.
Now that we know a bit about OpenCV let’s see how we can install it on various platforms.
2. Installing OpenCV on Windows
Installing OpenCV on Windows is a breeze. Just follow these simple steps, and you’ll be up and running in no time.
2.1. Install Visual Studio
To start, you’ll need to install Visual Studio, a popular integrated development environment (IDE) for Windows. Download the latest version of Visual Studio Community, which is free for individual developers, open-source projects, academic research, education, and small professional teams.
After downloading the installer, run it and follow the on-screen instructions. Be sure to select the “Desktop development with C++” workload during installation.
2.2. Install CMake
Next, you’ll need to install CMake, a cross-platform build tool that simplifies the installation process. Download the Windows installer and follow the installation wizard. Make sure to add CMake to your system’s PATH during installation.
2.3. Download and Configure OpenCV
Now it’s time to download OpenCV. Head over to the OpenCV GitHub releases page and download the latest source code as a ZIP file. Extract the contents of the ZIP file to a folder on your computer.
Open CMake and set the source code directory to the folder where you extracted OpenCV. Create a new folder named “build” inside the OpenCV folder, and set it as the build directory in CMake.
Click “Configure” and choose “Visual Studio” as the generator. Make sure to select the correct platform (x64 or x86) based on your system. Once the configuration is complete, click “Generate” to create the Visual Studio solution.
2.4. Build and Install OpenCV
Open the generated solution file (OpenCV.sln) in Visual Studio. Right-click on the “INSTALL” project in the Solution Explorer and select “Build”. This will compile and install OpenCV on your system.
Once the build is complete, you’ll find the OpenCV binaries and include files in the “build” folder inside the OpenCV directory. Add the “bin” folder to your system’s PATH, and you’re good to go!
3. Installing OpenCV on macOS
Installing OpenCV on macOS is a cinch, thanks to the Homebrew package manager. If you don’t have Homebrew installed, visit the Homebrew websiteand follow the instructions to install it.
3.1. Install Dependencies
With Homebrew installed, open a terminal window and run the following command to install the necessary dependencies:
brew install cmake pkg-config
3.2. Install OpenCV
Next, you can install OpenCV with just one command:
brew install opencv
This command will download, compile, and install OpenCV for you. It may take a few minutes to complete, so sit back and relax while Homebrew does its magic.
3.3. Configure Your Environment
After the installation is complete, you’ll need to update your system’s DYLD_LIBRARY_PATH to include the OpenCV library path. Add the following line to your ~/.bash_profile or ~/.zshrc file (depending on your shell):
Finally, restart your terminal or run
source ~/.bash_profile (or
source ~/.zshrc) to apply the changes.
Congratulations! You’ve successfully installed OpenCV on macOS.
4. Installing OpenCV on Linux
Installing OpenCV on Linux is just as straightforward as the other platforms. In this tutorial, we’ll focus on Ubuntu, but the process should be similar for other distributions.
4.1. Install Dependencies
First, let’s update the package list and install the necessary dependencies. Open a terminal window and run the following commands:
sudo apt update sudo apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
4.2. Download and Configure OpenCV
Next, navigate to your home directory and clone the OpenCV and OpenCV contrib repositories:
cd ~ git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git
Now, create a build directory inside the opencv folder and navigate to it:
cd opencv mkdir build cd build
Run CMake to configure the build:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON ..
4.3. Build and Install OpenCV
Now you’re ready to build OpenCV. Run the following command to start the compilation process:
This command will use all available cores on your system to speed up the build process. Once the build is complete, install OpenCV with the following command:
sudo make install
Finally, run the following command to update the library cache:
And that’s it! You’ve successfully installed OpenCV on Linux.
You’re now equipped with the knowledge to install OpenCV on Windows, macOS, and Linux. With this powerful library at your disposal, the world of computer vision is your oyster. Whether you’re a seasoned expert or just starting, OpenCV offers a wealth of possibilities to experiment with and learn from.
In summary, here are the key takeaways from this tutorial:
- OpenCV is a powerful, open-source computer vision and machine learning library, containing over 2500 optimized algorithms.
- Installing OpenCV on Windows involves setting up Visual Studio, CMake, and building the library using the generated solution file.
- On macOS, you can quickly install OpenCV using Homebrew and configure your environment to include the library path.
- For Linux (Ubuntu), you need to install the necessary dependencies, clone the OpenCV repositories, and build the library using CMake and make.
Now that you have OpenCV installed on your preferred platform, you can start exploring the fascinating world of computer vision. Whether you want to build face recognition systems, track objects in videos, or develop any other computer vision application, OpenCV is the perfect tool to bring your ideas to life.
This guide is meant to be accessible to beginners, so don’t hesitate to share it with anyone interested in computer vision. The more people we can help get started with OpenCV, the more exciting innovations we can look forward to in the future. Happy coding!