Tutorial: C++ basics
Tutorial: C++ basics for OpenCV
Deep Learning Image Processing. Updated. 2024.2
I. Introduction
The OpenCV Library has >2500 algorithms, extensive documentation, and sample code for real-time computer vision. You can see basic information about OpenCV at the following sites,
Homepage: https://opencv.org
Documentation: https://docs.opencv.org
Source code: https://github.com/opencv
Tutorial: https://docs.opencv.org/master
Books: https://opencv.org/books
In this tutorial, you will learn fundamental concepts of the C++ language to use the OpenCV API. You will learn namespace, class, C++ syntax to use image reading, writing and displaying.
OpenCV Example Code
Image File Read / Write / Display
OpenCV-C++
OpenCV is provided in C++, Python, Java. We will learn how to use OpenCV in
C++ (general image processing)
Python (for Deep learning processing)
For C++, we need to learn
Basic C++ syntax
Class
Overloading, namespace, template
Reference
C++
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. C++ is portable and can be used to develop applications adapted to multiple platforms. You can see basic C++ tutorials on following site,
Project Workspace Setting
Create the lecture workspace as C:\Users\yourID\source\repos\DLIP
e.g.
C:\Users\ykkim\source\repos\DLIP
Then, create sub-directories such as :
C:\Users\yourID\source\repos\DLIP\TutorialC:\Users\yourID\source\repos\DLIP\IncludeC:\Users\yourID\source\repos\DLIP\AssignmentC:\Users\yourID\source\repos\DLIP\LABC:\Users\yourID\source\repos\DLIP\Image
Define and Declare Functions in Header Files
We will learn how to declare and define functions in the header file
Exercise
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_C++_Ex1Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_C++_Ex1.cpp
Create new header files
File Names:
TU_DLIP.h,TU_DLIP.cppLib Folder:
C:\Users\yourID\source\repos\DLIP\Include\
Declare the sum function in the header file(
TU_DLIP.h) as
Define the sum function in the header source file(
TU_DLIP.cpp) as
Include the header file in the main source code of
DLIP_Tutorial_C++_Ex1.cpp.Modify the source main code to print the sum value of any two numbers
Compile and run.
C++ Class
Class is similar to C structure:
Structure: Cannot inclue functions. Only variables
Class: Can include variables, functions definition/declaration, other classes
Class Constructor
Constructor is special method automatically called when an object of a class is created.
Use the same name as the class, followed by parentheses ()
It is always public.
It does not have any return values.
Mat Class in OpenCV
The image data are in forms of 1D, 2D, 3D arrays with values 0~255 or 0~1
OpenCV provides the Mat class for operating multi dimensional images
Example
Printing out information about the source image using OpenCV
Results
Exercise
Create a Class 'MyNum'
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_C++_Ex2Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_C++_Ex2.cpp
Modify the header file
TU_DLIP.handTU_DLIP.cppto declare a class member named as MyNum.
Constructor : MyNum()
Member variables: val1, val2 ** integer type
Member functions: int sum() ** returns the sum of val1 and val2
Member functions: void print() ** prints values of val1, val2, and sum
Then, compile and run the program.
Solution
Namespace
A namespace provides a scope to the identifiers (the names of types, functions, variables, etc) inside it.
Uses :: as scope resolution operator
Use namespace in order to avoid collision using functions with the same name e.g. KimHandong --> Student::KimHandong, TA::KimHandong
std::cout, std::cin, std::endl are also defined in iostream
Exercise
Create another Class 'MyNum'
In this exercise, you create the MyNum class, previously implemented in Exercise 2, with the same class name in different namespaces, proj\_A, and proj\_B.
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_C++_Ex3Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_C++_Ex3.cpp
Modify the header file
TU_DLIP.handTU_DLIP.cppto declare two class members named as MyNum inproj\_Aandproj\_B.Use namespace to identify two classes clearly
First MyNum class: namespace name proj_A
Second MyNum class: namespace name proj_B
Also, declare class member variables for each MyNum class: Constructor / val1 / val2 / val3 / sum / print
Constructor
MyNum(int in1, int in2, int in3): A constructor for specifying valuesval1,val2,val3val1,val2,val3: member variable of integer typesum(void): member function that returns the sum of val1, val2, and val3`print(void): member function that prints val1, val2, val3, and sum
Template
A template can make a variable type(int, float, char..) as a variable. How can you use the same function but with a different number type as the input argument? : add(float A, float B), add(int A, int B) &rarr add(T A, T B) where T=int or T=float
Function Overloading
Functions with the same name (but with different types or number of parameters) can be defined.
Different return type (with everything else the same) is not a function overloading.
Example
cv::Mat can be created in many different ways. Use the up or down the keyboard to see what the options are.
Function Overloading Reference
Exercise
Do the exercises in
Default Parameter
Default parameter in OpenCV
Pointer
A pointer is a variable whose value is the address of another variable.
What are Pointers?
A pointer is a variable whose value is the address of another variable. i.e. direct address of the memory locations Pointers are the basis for data structures.
Define a pointer variable
int *ptr;Assign the address of a variable to a pointer
ptr = &varAccess the value at the address available in the pointer variable
int value = *ptr
Example
Exercise
Do the exercises in
Reference
A reference variable is a "reference" to an existing variable, and it is created with the & operator.
Reference can replace the use of pointer *
The disadvantage of reference is "We do not know if a function is call-by-value or call-by-reference"
Exercise
Do the exercise in
Last updated
Was this helpful?