Professional Documents
Culture Documents
NRP : 05111940000015
---
# Tutorial OpenCV
To compile the source code of this tutorial OpenCV, I use this syntax:
```
.\(name file)
```
* `g++` is a GNU c++ compiler invocation command, which is used for preprocessing, compilation,
assembly and linking of source code to generate an executable file.
* `-o (name file)`, its mean the output from the input
---
## A. Basic Drawing
#### Requirement:
FUNCTION
STRUCTURE
* Use **cv::Point**
```
Point pt;
pt.x = 10;
pt.y = 8;
```
* Use **cv::Scalar**
As most of OpenCV operates on maximum 4 channel images, so Scalar is a simple class which is
actually a 4-element vector (cv::Vec). The type Scalar is widely used in OpenCV for passing pixel values.
In this tutorial, **Scalar** extensively to represent BGR color values (3 parameters). The order of color
is BGR (Blue, Green, Red)
```
Scalar( a, b, c )
```
#### Design:
1. Create two images and two windows to display the an atom and a rook.
> NB : `CV_8UC3`
> * CV_ <bit depth> {U | S | F} C (<number of channels>) single array channel where U, S, and F
stands for **unsigned**, **signed**, and **float**.
> * So,
> `CV_8UC3` is array of 3 channels with each element having an unsigned integer 8-bit data type to
access RGB (BGR) data
3. Display the stuff (an Atom and a rook image) with `imshow` and `moveWIndow`. To access OpenCV
Function `imshow` and `moveWindow`, I need `highgui.hpp` File Reference
4. Use Waitkey (delay) to hold the window, when delay≤0 (its mean infinitely) or delay n positive (its
mean for n miliseconds)
```
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#define w 400
//Function headers
//(1) Create two images and two windows to display the an atom and a rook.
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyPolygon( rook_image );
//(2.b.2) Creating a yellow rectangle for the bottom floor of the rook
rectangle( rook_image,
FILLED, //filled
waitKey( 0 );
return(0);
//Function Declaration
int thickness = 2;
int lineType = 8;
ellipse( img,
angle,
0,
360,
thickness,
lineType );
circle( img,
center,
w/32,
FILLED,
LINE_8 );
Point rook_points[1][20];
int npt[] = { 20 };
fillPoly( img,
ppt,
npt,
1,
lineType );
int thickness = 2;
line( img,
start,
end,
thickness,
lineType );
```
## B. Smoothing Image
#### Requirement:
FUNCTION
In this tutorial, I use 4 OpenCV function to apply diverse linear filters to smooth images :
* **blur()**
* **GaussianBlur()**
* **medianBlur()**
* **bilateralFilter()**
* There are many reasons for smoothing. In this tutorial, its will focus on smoothing in order to reduce
noise
* The most common type of filters are linear in which an output pixel's value (i.e. g(i,j)) is determined as
a weighted sum of input pixel values (i.e. f(i+k,j+l)):
* $$h(k,l)$$ is called the kernel, which is the coefficients of the filter. It helps to visualize a filter as a
window of coefficients sliding across the image.
KIND OF FILTERS
1. Normalized Box Filters: Filter that each output pixel is the mean of its kernel neighbors
2. Gaussian Blur
3. Median Filter
4. Bilateral Filter
#### Design:
##### [LOADS AN IMAGE, APPLIES 4 DIFFERENT KIND OF FILTERS, AND SHOW THE FILTERED IMAGE]
*NB: sequentially*
1. Make a window to display image named "Smoothing Demo" with OpenCV Function `namedWindow`
with `WINDOW_AUTOSIZE` flag.
> NB:
> `WINDOW_AUTOSIZE`, its mean the user cannot resize the window, the size is constrainted by the
image displayed.
> `namedWindow` is to creates a window that can be used as a placeholder for images and trackbars
2. Loads the source image by using OpenCV function `imread` with `IMREAD_COLOR` flag. So, I need
`imgcodecs.hpp` File Reference
> NB:
> `IMGREAD_COLOR`, If its set, its will always convert image to the 3 channel BGR color image.
```
Size ksize, //Size of the kernel to be used ( of width w pixels and height h pixels)
Point anchor = Point(-1,-1), //The anchor point (the pixel evaluated) is located.
//(-1, -1)-> that the anchor point is the center of the kernel
```
More detail
[here](https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801
b0b2e440fce37)
```
OutputArray dst,
Size ksize,
```
NB: if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from
ksize.
```
OutputArray dst,
Size ksize,
double sigmaX,
double sigmaY = 0,
```
OutputArray dst,
int d,
double sigmaX,
double sigmaY = 0,
```
6. Put caption "Done" to inform that the proccess was done with openCV function `putText`(It is defined
in `imgproc.hpp` File Reference)
> NB:
> `FONT_HERSHEY_COMPLEX` is a normal size serif font, which is included as Enumeration Type
Documentation
```
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
int DELAY_BLUR = 100; //to give a delay between one filters to the others through 0.1 s
//Prototype function
if (src.empty())
return EXIT_FAILURE;
return 0;
dst = src.clone();
return 0;
return 0;
return 0;
return 0;
return 0;
}
return 0;
return 0;
return 0;
return 0;
}
}
display_caption( "Done!" );
return 0;
// Function Definition
return display_dst(DELAY_CAPTION);
return 0;
```
#### Result
The code opens an image (in this case lena.jpg) and display it under the effects of the 4 filters explained.
#### Requirement:
FUNCTION
In this tutorial, I use 2 OpenCV function to to apply erotion and dilation images :
* `cv::erode`
* `cv::dilate`
EXPLANATION
The most basic morphological operations are: Erosion and Dilation. They have a wide array of uses, i.e. :
* Removing noise
#### Design:
1. Load an image (can be BGR or grayscale) use `CommandLineParser` and by using OpenCV function
`imread` with `IMREAD_COLOR` flag. So, I need `imgcodecs.hpp` File Reference
> NB:
> `IMGREAD_COLOR`, If its set, its will always convert image to the 3 channel BGR color image.
2. Create two windows (one for dilation output, the other for erosion) with OpenCV Function
`namedWindow` with `WINDOW_AUTOSIZE` flag.
> NB:
> `WINDOW_AUTOSIZE`, its mean the user cannot resize the window, the size is constrainted by the
image displayed.
> `namedWindow` is to creates a window that can be used as a placeholder for images and trackbars
* The second trackbar "Kernel size" return erosion_size or dilation_size for the corresponding
operation.
This step will use `createTrackbar`. So, i need `highgui.hpp` File Reference
4. Defaukt start with 0, 0. Every time move any slider, the user's function Erosion or Dilation will be
called and it will update the output image based on the current trackbar values.
```
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
if( src.empty() )
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
//(2) Create two windows (one for dilation output, the other for erosion)
&erosion_elem, max_elem,
Erosion );
&erosion_size, max_kernel_size,
Erosion );
&dilation_elem, max_elem,
Dilation );
&dilation_size, max_kernel_size,
Dilation );
//(4) Default start
Erosion( 0, 0 );
Dilation( 0, 0 );
waitKey(0);
return 0;
//Function Definition
int erosion_type = 0;
//![kernel]
int dilation_type = 0;
//![kernel]
```