Installing OpenCV (Windows)

OpenCV (Open-Source Computer Vision Library: http://opencv.org) is an open-source library that includes several hundreds of computer vision algorithms.

There are two ways you can get OpenCV installed on your Windows machine.

Way #1: Pre-Compiled Package

You can download and install pre-compiled packages to quickly start your journey to OpenCV. But the pre-built package will not contain certain parts of OpenCV, like OpenVINO support, FFMPEG support or IntelTBB like that. You can learn more from the links below.

A package manager also be an excellent choice particularly when you want to intergrade OpenCV into your existing project.

Way #2: Compiling (MSys2)

MSYS2 isn’t “one tool to rule them all” but tries to focus on what it’s good at. It provides a native build environment, based on open-source software, and makes you feel right at home when you are already comfortable with Linux. There are good reasons to use multiple different environments and tools for different tasks on Windows.

So MSys2 makes your terminal on Windows work like Linux (Arch more specifically), but it can build native software for window, while WSL can only do it through cross compile which makes things more complicated.

Installing MSys2

Ther are plenty of articles that guide you to get MSys2 installed.

Aside from the basic MSys2 installation, you also need to install other build tools to compile OpenCV from source code.

pacman -S --needed base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmake mingw-w64-x86_64-qt6

Download latest OpenCV source code

Launch “MSYS2 MinGW x64” from the Start menu or Windows Terminal.

Get the latest OpenCV version number using GitHub API and assign it to a shell variable for later use.

vernum=$(wget -qO- -t1 -T2 "https://api.github.com/repos/opencv/opencv/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g;s/v//g')

Then, download and unzip OpenCV source code from GitHub. In order not to mess the working directory up, it is recommended to create a new folder dedicated to compilation.

mkdir opencv_build && cd opencv_build
wget https://github.com/opencv/opencv/archive/refs/tags/$vernum.zip
unzip $vernum.zip && rm $vernum.zip

wget https://github.com/opencv/opencv_contrib/archive/refs/tags/$vernum.zip
unzip $vernum.zip && rm $vernum.zip

cd opencv-$vernum

Compile

Launch cmake-gui from “MSYS2 MinGW x64“‘s bash.

cmake-gui

Then click “Browse Source…” to select the OpenCV source code folder, click “Browse Build…” to select the build folder one created just now.

The download path should be in here if you install MSYS2 at C:\msys64.

C:\msys64\home\[your user name]\

After that, click “Configure“. Note that the configuring process needs a stable internet connection.

Select compiler as below.

You should see this after a succeed Configure.

Then enter “extra” in the search box, press enter.

Click the button at the right of the textbox, select the modules folder in opencv-contrib source code folder.

Then click “Generate“. If this finished with no errors, Close the cmake-gui window and go back to terminal.

Make sure you are under build folder created and cmake-gui worked just now, run make -j12 (This number should smaller than your CPU threads).

make -j12

If this finished with no errors too, run make install.

make install

After that, the functional cmake files and lib file will be in “install” folder under “build” folder.

Leave a Reply