Adding OpenCV 4.1.2 to Visual Studio 2019 Project in Windows using Pre-built binaries
Note: This uses an older version (4.1.2) of OpenCV. A newer version (4.2.0) with many new features has been released. Unless you have a very specific reason to stick to version 4.1.2, you should use the 4.2.0 version. Please refer to https://medium.com/@subwaymatch/adding-opencv-4-2-0-to-visual-studio-2019-project-in-windows-using-pre-built-binaries-93a851ed6141 for an updated tutorial on adding the newer version to Visual Studio 2019.
For some reason, the official documentation for OpenCV 4.1.2 has vastly outdated contents on setting up OpenCV for Visual Studio (for C++). So, let’s take a quick dive into how to setup OpenCV 4.1.2 on Visual Studio 2019.
Environment
- Windows 10 (64-bit)
- Visual Studio 2019
Step 1: Download and extract the pre-built library
Downlo a d the latest binary from opencv’s Github repository. The latest version as of October 17, 2019 is 4.1.2 (opencv-4.1.2-vc14_vc15.exe). Release files for 4.1.2 are listed at https://github.com/opencv/opencv/releases/tag/4.1.2.
Run the downloaded .exe file to extract the archive.
For the sake of uniformity, this tutorial will assume that you’ve extracted the contents to c:\ .
Как добавить opencv в visual studio
OpenCV (англ. Open Source Computer Vision Library, библиотека компьютерного зрения с открытым исходным кодом) — библиотека алгоритмов компьютерного зрения, обработки изображений и численных алгоритмов общего назначения с открытым кодом. Реализована на C/C++, также разрабатывается для Python, Java, Ruby, Matlab, Lua и других языков[2]. Может свободно использоваться в академических и коммерческих целях — распространяется в условиях лицензии BSD. [Википедия]
Рассмотрим подготовку и использование этой библиотеки в средах Visual Studio 2014 и 2015. К счастью, в новых сборках OpenCV поставляются уже собранные библиотеки, их и будем использовать.
Шаг 1. Загрузка и установка OpenCV
Первым делом необходимо скачать установочный файл OpenCV (Доступен на официальном сайте проекта: http://opencv.org/*). Нас интересует версия для Windows.
*Сейчас на главной странице версия 2.4.13, версия 3.1.0 доступна на странице opencv.org/downloads.html)
Шаг 2. Настройка переменных среды
Открываем: "Панель управления" -> "Система" -> "Дополнительные параметры системы" ->Дополнительно->"Переменные среды. "
Install and configure OpenCV-4.2.0 in Windows 10 — VC++
Complete guide to install OpenCV-4.2.0 in Windows 10
Apr 19, 2020 · 4 min read
This post will guide you through all the steps for installing and configuring OpenCV-4.2.0 in Windows 10 (64-bit) within 2 different environments:
- OpenCV with Visual Studio 2019, for C++ development
- OpenCV with Anaconda, for Python 3.6.0+ development
I will focus here on OpenCV for Visual Studio with C++, the other tutorial part for python and Anaconda can be found here.
Note: To follow along with the tutorial, I will assume that you already have Visual Studio 2019 installed. If not, please feel free to install these tools before continuing to read further.
OpenCV-4.2.0 for Visual Studio 2019
All the required steps for setting up OpenCV for a Visual C++ development.
Step1: Install the C++ Desktop development Workload
Open your Visual Studio Installer tool and add C++ for desktop development as a workload to your current Visual Studio IDE version. This step is essential since you can not use OpenCV in VS without all the C++ required libraries.
Step 2: Download and Install OpenCV-4.2.0
Do w nload OpenCV 4.2.0 latest stable release (opencv-4.2.0-vc14_vc15.exe) for Windows platform. Go to the official OpenCV website: https://opencv.org/ -> Resources -> Releases and click on the Windows platform. You will be redirected to SourceForge and download will automatically start.
Before running the download .exe file, go to your C:\ folder and add a new folder named opencv-4.2.0. Run the installer and extract the zip file to the opencv-4.2.0 folder newly created.
Step 3: Add OpenCV binaries to your System path
Once OpenCV is correctly installed in your folder, you now have to add the binaries C:\OpenCV-4.2.0\opencv\build\x64\vc15\bin to your system path, so you can have access to OpenCV executables easily through your command line.
Step 4: Configure a Visual Studio project to run OpenCV
Open Visual Studio 2019, choose to create a new project and go for the C++ Console App template.
Once the project created you should have a layout with a solution explorer to the right having one source file and in the window upper ribbon you should see Debug for x86 platforms, meaning that the project will build and run in debug mode targeting x86 windows architectures.
- First, you should change the solution platforms from x86 to x64
- Secondly, you have to change the Project Properties to add the OpenCV libraries
There are a bunch of properties to edit before being able to execute any code:
- Go to Configuration Properties/VC++ Directories to add the include and library directories for OpenCV.
For the Include directory, you have to add the following path: C:\OpenCV-4.2.0\opencv\build\include. Do the same for the Library Directories adding this internal path: C:\OpenCV-4.2.0\opencv\build\x64\vc15\lib.
2. Edit the VC++ project linker with the opencv_world420d.lib OpenCV dynamic library. You will find the DLL (Dynamic Link Library) here: C:\OpenCV-4.2.0\opencv\build\x64\vc15\lib copy the name of the file opencv_world420d.lib and paste it in the dependency box.
Final Step: Test a bit of code
Finally, you can run this demo code in your visual studio IDE to see if it’s all working fine.