Building OpenCV with CMake on Windows

OpenCV (Open Source Computer Vision Library) is a powerful open source library of computer vision algorithms. It is widely used by many technologies, such as image acquiring (e.g. Webcam capture), image processing (e.g. noise reduction), image detection (e.g. face detection), image recognition (e.g. OCR), and so on. Since all OpenCV source code is on GitHub, let’s get the copy and build the source code ourselves for fun.

Prerequisites for Building OpenCV on Windows

CMake-GUI Configuration

To facilitate the building work, we just need to launch CMake-GUI. Specify the source code path and the target build directory.


Click Configure, and specify the generator for building. I’m using Visual Studio 2013.


After that, you will see a bunch of checked options.


You are free to select any build modules you like, and then press Generate to generate build files.

Building OpenCV Source Code with Visual Studio

Open OpenCV.sln with Visual Studio.


Now you just need to click Build Solution. Wait for a while, and then you will see the final results:


How to Build OpenCV with Command Line Tool

If you prefer building source code with a command line tool, you can learn how to write scripts from opencv\platforms\scripts. Here is the script for building OpenCV source code with default configurations:

set PATH=%PATH%;F:\CMake\bin\
mkdir OpenCVBuild
cd OpenCVBuild
cmake F:\git\opencv\opencv
cmake --build .


How to Build OpenCV for Android

OpenCV contains toolchains for diverse platforms.


Let’s take Android for example. To build OpenCV for Android on Windows, we need to download ninja. And then we can use following Windows batch scripts:

set PATH=%PATH%;F:\CMake\bin\;F:\zip\ninja-win\
mkdir OpenCV4AndroidBuild
cd OpenCV4AndroidBuild
cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=F:\git\opencv\opencv\platforms\android\android.toolchain.cmake -DANDROID_NDK=F:\android-ndk-r10 -DCMAKE_BUILD_TYPE=Release F:\git\opencv\opencv
cmake --build .


If you are interested in OpenCV, try to build the source code yourself.