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
#include <iostream>
#include <opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
/* read image */
String filename1 = "image.jpg"; // class
Mat img = imread(filename1); //Mat class
Mat img_gray = imread("image.jpg", 0); // read in grayscale
/* write image */
String filename2 = "writeTest.jpg"; // C++ class/syntax (String, cout, cin)
imwrite(filename2, img);
Â
/* display image */
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", img);
namedWindow("image_gray", WINDOW_AUTOSIZE);
imshow("image_gray", img_gray);
waitKey(0);
}
C++ for OpenCV
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 that can be adapted to multiple platforms. You can see basic C++ tutorials in following site,
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
Then, compile and run the program.
//#include "TU_DLIP.h"
#include "../../../Include/TU_DLIP.h"
int main()
{
// =============================
// Exercise 1: Define Function
// =============================
int val1 = 11;
int val2 = 22;
int out = sum(val1, val2);
std::cout << out << std::endl;
// ====================================
// Exercise 2: Create a Class 'MyNum'
// ====================================
MyNum mynum(10, 20);
mynum.print();
}
#ifndef _TU_DLIP_H // same as "#if !define _TU_DLIP_H" (or #pragma once)
#define _TU_DLIP_H
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2)
{
return val1 + val2;
}
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
// Declare Constructor, function(sum, print), variable(val1, val2)
class MyNum
{
// Add code here
};
#endif // !_TU_DLIP_H
#include "TU_DLIP.h"
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2)
{
return val1 + val2;
}
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
// Constructor: x1 -> val1, x2 -> val2
MyNum::MyNum(int x1, int x2)
{
// Add code here
}
int MyNum::sum(void)
{
// Add code here
}
void MyNum::print(void)
{
// Add code here
}
Solution
#ifndef _TU_DLIP_H // same as "#if !define _TU_DLIP_H" (or #pragma once)
#define _TU_DLIP_H
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2);
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
class MyNum
{
public:
MyNum(int x1, int x2);
int val1;
int val2;
int sum(void);
void print(void);
};
#endif // !_TU_DLIP_H
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
#ifndef _TU_DLIP_H // same as "#if !define _TU_DLIP_H" (or #pragma once)
#define _TU_DLIP_H
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2);
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
class MyNum
{
public:
MyNum(int x1, int x2);
int val1;
int val2;
int sum(void);
void print(void);
};
// ======================================================
// Exercise 3: Create two Class "MyNum" in proj_A, proj_B
// ======================================================
namespace proj_A
{
// Add code here
}
namespace proj_B
{
// Add code here
}
#endif // !_TU_DLIP_H
#include "TU_DLIP.h"
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2)
{
return val1 + val2;
}
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
MyNum::MyNum(int x1, int x2)
{
val1 = x1;
val2 = x2;
}
int MyNum::sum(void)
{
return val1 + val2;
}
void MyNum::print(void)
{
std::cout << "MyNum.val1 : " << val1 << std::endl;
std::cout << "MyNum.val2 : " << val2 << std::endl;
std::cout << "Sum : " << sum() << std::endl;
}
// ======================================================
// Exercise 3: Create two Class "MyNum" in proj_A, proj_B
// ======================================================
proj_A::MyNum::MyNum(int x1, int x2, int x3)
{
// Add code here
}
int proj_A::MyNum::sum(void)
{
// Add code here
}
void proj_A::MyNum::print(void)
{
// Add code here
}
proj_B::MyNum::MyNum(int x1, int x2, int x3)
{
// Add code here
}
int proj_B::MyNum::sum(void)
{
// Add code here
}
void proj_B::MyNum::print(void)
{
// Add code here
}
#ifndef _TU_DLIP_H // same as "#if !define _TU_DLIP_H" (or #pragma once)
#define _TU_DLIP_H
#include <iostream>
// =============================
// Exercise 1: Define Function
// =============================
int sum(int val1, int val2);
// ====================================
// Exercise 2: Create a Class "MyNum"
// ====================================
class MyNum
{
public:
MyNum(int x1, int x2);
int val1;
int val2;
int sum(void);
void print(void);
};
// ======================================================
// Exercise 3: Create two Class "MyNum" in proj_A, proj_B
// ======================================================
namespace proj_A
{
class MyNum
{
public:
MyNum(int x1, int x2, int x3);
int val1;
int val2;
int val3;
int sum(void);
void print(void);
};
}
namespace proj_B
{
class MyNum
{
public:
MyNum(int x1, int x2, int x3);
int val1;
int val2;
int val3;
int sum(void);
void print(void);
};
}
#endif // !_TU_DLIP_H
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.
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