Getting Started With OpenCV in C/C++ in Windows
How to configure OpenCV for C/C++ in Windows?
Installing OpenCV is very hard task because of various things like build error and Compiler Error. So in this tutorial, I will write in brief about how can one install OpenCV for C++ in Windows machine.
Getting Systems Ready!
- First check your system if it is 64-bit or 32-bit because we will need separate files for each.
- Download VS-Code for your system. The reason for using VS Code is because of its features. It provides extensions like C/C++, Cmake, Intellsense and it even makes our things easier as it builds our code with just single click.
- Download and install Visual Studio 2019 (not the VS Code) is also installed and during the installation, make sure to download feature
Desktop Development with C++
. - Download and Install Cmake from https://cmake.org/download/. Cmake is a build tool which will work on backend to make a executable file for us. If we want to build any file without using VS code then we must use Cmake from CMD.
- Download and Install mingw (32-bit or 64-bit according to your system) from https://sourceforge.net/projects/mingw/files/Installer/, it provides us the compiler. Also remember the installation path.
- Now lets add mingw in system variable so that the path could be accessible from anywhere.
- For me the paths are just under
C
drive.C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0
- If looked closely.
....\bin
folder contains compilerg++
,c++
and lots of other tools.
- Download and Extract OpenCV from https://sourceforge.net/projects/opencvlibrary/files/4.5.1/opencv-4.5.1-vc14_vc15.exe/download to
C
drive. The sole purpose of choosing C drive is because of simplicity.- Again we have to add the build path (both
lib
andbin
) into the system variable.C:\OpenCV\opencv\build\x64\vc15\lib
C:\OpenCV\opencv\build\x64\vc15\bin
- If looked carefully inside
C"OpenCV\opencv\include\opencv2
we can find lots of*.h
C/C++ header files there. Which will allow us to use OpenCV.
- Again we have to add the build path (both
Configure Build Operations
- Start by making a new project on VS Code. i.e. start on new folder.
- Find a command
Cmake Configure
. By hittingctrl+shift+p
. - Choose your Visual Studio version.
- A warning will be shown on bottom right, click on create.
- Give your project a name (like in this case opencv-test).
- Pick to make executable.
- Now
CMakeLists.txt
,main.cpp
andbuild
folder will be generated by CMake. - On
CMakeLists.txt
we have to give some build information and informations about OpenCV directories.- We have to tell that, find the required package OpenCV and include the directory too.
- Then add target_link_libraries, i.e. link the libraries to our project.
-
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0) project(opencv-test VERSION 0.1.0) include(CTest) enable_testing() find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRs}) add_executable(opencv-test main.cpp) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) include(CPack)
- If everything goes okay, then after saving a file, on the output console, it will be shown. If no error is shown then we can proceed.
Create and Run first OpenCV program in C++.
- Head over to the
main.cpp
. Copy below and replace the codes.#include <iostream> //handeling input/output #include <stdio.h> // standard i/o? #include <opencv2/opencv.hpp> // main opencv #include <opencv2/highgui/highgui.hpp> // opencv gui #include "opencv2/imgproc.hpp" //image processing #include "opencv2/imgcodecs.hpp" //image codecs i.e. png, jpeg etc using namespace cv; using namespace std; Mat image; int main() { // get full path of any image near your directory! string impath="F:\\Desktop\\guiconcept.png"; image = imread(impath, 1); imshow("Image", image); cv::waitKey(0); return 0; }
- Make sure to change the
impath
variable! - Now press on play button on the bottom, it will take few seconds to build it then if everything is okay, it will show our image.
Why not read more?
- Gesture Based Visually Writing System Using OpenCV and Python
- Gesture Based Visually Writing System: Adding Visual User Interface
- Gesture Based Visually Writing System: Adding Virtual Animationn, New Mode and New VUI
- Gesture Based Visually Writing System: Add Slider, More Colors and Optimized OOP code
- Gesture Based Visually Writing System: A Web App
- Contour Based Game: Break The Bricks
- Linear Regression from Scratch
- Writing Popular ML Optimizers from Scratch
- Feed Forward Neural Network from Scratch
- Convolutional Neural Networks from Scratch
- Writing a Simple Image Processing Class from Scratch
- Deploying a RASA Chatbot on Android using Unity3d
- Naive Bayes for text classifications: Scratch to Framework
- Simple OCR for Devanagari Handwritten Text
Comments