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,

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

  1. C++ (general image processing)

  2. 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\Tutorial

  • C:\Users\yourID\source\repos\DLIP\Include

  • C:\Users\yourID\source\repos\DLIP\Assignment

  • C:\Users\yourID\source\repos\DLIP\LAB

  • C:\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

  1. Create a new C++ project in Visual Studio Community

    • Project Name: DLIP_Tutorial_C++_Ex1

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  2. Create a new C+ source file

    • File Name: DLIP_Tutorial_C++_Ex1.cpp

  3. Create new header files

    • File Names: TU_DLIP.h, TU_DLIP.cpp

    • Lib Folder: C:\Users\yourID\source\repos\DLIP\Include\

  4. Declare the sum function in the header file(TU_DLIP.h) as

  1. Define the sum function in the header source file(TU_DLIP.cpp) as

  1. Include the header file in the main source code of DLIP_Tutorial_C++_Ex1.cpp.

  2. Modify the source main code to print the sum value of any two numbers

  3. 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'

  1. Create a new C++ project in Visual Studio Community

  • Project Name: DLIP_Tutorial_C++_Ex2

  • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  1. Create a new C+ source file

  • File Name: DLIP_Tutorial_C++_Ex2.cpp

  1. Modify the header file TU_DLIP.h and TU_DLIP.cpp to 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

  1. 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.

  1. Create a new C++ project in Visual Studio Community

  • Project Name: DLIP_Tutorial_C++_Ex3

  • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  1. Create a new C+ source file

  • File Name: DLIP_Tutorial_C++_Ex3.cpp

  1. Modify the header file TU_DLIP.h and TU_DLIP.cpp to declare two class members named as MyNum in proj\_A and proj\_B.

  2. Use namespace to identify two classes clearly

    • First MyNum class: namespace name proj_A

    • Second MyNum class: namespace name proj_B

  3. 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 values val1, val2, val3

    • val1, val2, val3: member variable of integer type

    • sum(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

Overloading exercise


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 = &var

  • Access the value at the address available in the pointer variable int value = *ptr

Example

Exercise

Do the exercises in

Pointer Exercises


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

Reference Exercises

Last updated

Was this helpful?