Cleaning up the doc/tutorials folder in preparation to add thew new structure.
@ -1,265 +0,0 @@
|
||||
.. _Drawing_1:
|
||||
|
||||
Basic Drawing
|
||||
****************
|
||||
|
||||
Goals
|
||||
======
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Use :point:`Point <>` to define 2D points in an image.
|
||||
* Use :scalar:`Scalar <>` and why it is useful
|
||||
* Draw a **line** by using the OpenCV function :line:`line <>`
|
||||
* Draw an **ellipse** by using the OpenCV function :ellipse:`ellipse <>`
|
||||
* Draw a **rectangle** by using the OpenCV function :rectangle:`rectangle <>`
|
||||
* Draw a **circle** by using the OpenCV function :circle:`circle <>`
|
||||
* Draw a **filled polygon** by using the OpenCV function :fill_poly:`fillPoly <>`
|
||||
|
||||
OpenCV Theory
|
||||
===============
|
||||
|
||||
For this tutorial, we will heavily use two structures: :point:`Point <>` and :scalar:`Scalar <>`:
|
||||
|
||||
Point
|
||||
-------
|
||||
It represents a 2D point, specified by its image coordinates :math:`x` and :math:`y`. We can define it as:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Point pt;
|
||||
pt.x = 10;
|
||||
pt.y = 8;
|
||||
|
||||
or
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Point pt = Point(10, 8);
|
||||
|
||||
Scalar
|
||||
-------
|
||||
* Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel values.
|
||||
* In this tutorial, we will use it extensively to represent RGB color values (3 parameters). It is not necessary to define the last argument if it is not going to be used.
|
||||
* Let's see an example, if we are asked for a color argument and we give:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Scalar( a, b, c )
|
||||
|
||||
We would be defining a RGB color such as: *Red = c*, *Green = b* and *Blue = a*
|
||||
|
||||
|
||||
Code
|
||||
=====
|
||||
* This code is in your OpenCV sample folder. Otherwise you can grab it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/Basic/Drawing_1.cpp>`_
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
#. Since we plan to draw two examples (an atom and a rook), we have to create 02 images and two windows to display them.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// Windows names
|
||||
char atom_window[] = "Drawing 1: Atom";
|
||||
char rook_window[] = "Drawing 2: Rook";
|
||||
|
||||
/// Create black empty images
|
||||
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
|
||||
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
|
||||
|
||||
#. We created functions to draw different geometric shapes. For instance, to draw the atom we used *MyEllipse* and *MyFilledCircle*:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// 1. Draw a simple atom:
|
||||
|
||||
/// 1.a. Creating ellipses
|
||||
MyEllipse( atom_image, 90 );
|
||||
MyEllipse( atom_image, 0 );
|
||||
MyEllipse( atom_image, 45 );
|
||||
MyEllipse( atom_image, -45 );
|
||||
|
||||
/// 1.b. Creating circles
|
||||
MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );
|
||||
|
||||
#. And to draw the rook we employed *MyLine*, *rectangle* and a *MyPolygon*:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// 2. Draw a rook
|
||||
|
||||
/// 2.a. Create a convex polygon
|
||||
MyPolygon( rook_image );
|
||||
|
||||
/// 2.b. Creating rectangles
|
||||
rectangle( rook_image,
|
||||
Point( 0, 7*w/8.0 ),
|
||||
Point( w, w),
|
||||
Scalar( 0, 255, 255 ),
|
||||
-1,
|
||||
8 );
|
||||
|
||||
/// 2.c. Create a few lines
|
||||
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
|
||||
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
|
||||
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
|
||||
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
|
||||
|
||||
#. Let's check what is inside each of these functions:
|
||||
|
||||
* *MyLine*
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
void MyLine( Mat img, Point start, Point end )
|
||||
{
|
||||
int thickness = 2;
|
||||
int lineType = 8;
|
||||
line( img,
|
||||
start,
|
||||
end,
|
||||
Scalar( 0, 0, 0 ),
|
||||
thickness,
|
||||
lineType );
|
||||
}
|
||||
|
||||
As we can see, *MyLine* just call the function :line:`line <>`, which does the following:
|
||||
|
||||
* Draw a line from Point **start** to Point **end**
|
||||
* The line is displayed in the image **img**
|
||||
* The line color is defined by **Scalar( 0, 0, 0)** which is the RGB value correspondent to **Black**
|
||||
* The line thickness is set to **thickness** (in this case 2)
|
||||
* The line is a 8-connected one (**lineType** = 8)
|
||||
|
||||
* *MyEllipse*
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
void MyEllipse( Mat img, double angle )
|
||||
{
|
||||
int thickness = 2;
|
||||
int lineType = 8;
|
||||
|
||||
ellipse( img,
|
||||
Point( w/2.0, w/2.0 ),
|
||||
Size( w/4.0, w/16.0 ),
|
||||
angle,
|
||||
0,
|
||||
360,
|
||||
Scalar( 255, 0, 0 ),
|
||||
thickness,
|
||||
lineType );
|
||||
}
|
||||
|
||||
From the code above, we can observe that the function :ellipse:`ellipse <>` draws an ellipse such that:
|
||||
|
||||
* The ellipse is displayed in the image **img**
|
||||
* The ellipse center is located in the point **(w/2.0, w/2.0)** and is enclosed in a box of size **(w/4.0, w/16.0)**
|
||||
* The ellipse is rotated **angle** degrees
|
||||
* The ellipse extends an arc between **0** and **360** degrees
|
||||
* The color of the figure will be **Scalar( 255, 255, 0)** which means blue in RGB value.
|
||||
* The ellipse's **thickness** is 2.
|
||||
|
||||
|
||||
* *MyFilledCircle*
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
void MyFilledCircle( Mat img, Point center )
|
||||
{
|
||||
int thickness = -1;
|
||||
int lineType = 8;
|
||||
|
||||
circle( img,
|
||||
center,
|
||||
w/32.0,
|
||||
Scalar( 0, 0, 255 ),
|
||||
thickness,
|
||||
lineType );
|
||||
}
|
||||
|
||||
Similar to the ellipse function, we can observe that *circle* receives as arguments:
|
||||
|
||||
* The image where the circle will be displayed (**img**)
|
||||
* The center of the circle denoted as the Point **center**
|
||||
* The radius of the circle: **w/32.0**
|
||||
* The color of the circle: **Scalar(0, 0, 255)** which means *Red* in RGB
|
||||
* Since **thickness** = -1, the circle will be drawn filled.
|
||||
|
||||
* *MyPolygon*
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
void MyPolygon( Mat img )
|
||||
{
|
||||
int lineType = 8;
|
||||
|
||||
/** Create some points */
|
||||
Point rook_points[1][20];
|
||||
rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
|
||||
rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
|
||||
rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
|
||||
rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );
|
||||
rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );
|
||||
rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );
|
||||
rook_points[0][6] = Point( 3*w/4.0, w/8.0 );
|
||||
rook_points[0][7] = Point( 26*w/40.0, w/8.0 );
|
||||
rook_points[0][8] = Point( 26*w/40.0, w/4.0 );
|
||||
rook_points[0][9] = Point( 22*w/40.0, w/4.0 );
|
||||
rook_points[0][10] = Point( 22*w/40.0, w/8.0 );
|
||||
rook_points[0][11] = Point( 18*w/40.0, w/8.0 );
|
||||
rook_points[0][12] = Point( 18*w/40.0, w/4.0 );
|
||||
rook_points[0][13] = Point( 14*w/40.0, w/4.0 );
|
||||
rook_points[0][14] = Point( 14*w/40.0, w/8.0 );
|
||||
rook_points[0][15] = Point( w/4.0, w/8.0 );
|
||||
rook_points[0][16] = Point( w/4.0, 3*w/8.0 );
|
||||
rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );
|
||||
rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );
|
||||
rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;
|
||||
|
||||
const Point* ppt[1] = { rook_points[0] };
|
||||
int npt[] = { 20 };
|
||||
|
||||
fillPoly( img,
|
||||
ppt,
|
||||
npt,
|
||||
1,
|
||||
Scalar( 255, 255, 255 ),
|
||||
lineType );
|
||||
}
|
||||
|
||||
To draw a filled polygon we use the function :fill_poly:`fillPoly <>`. We note that:
|
||||
|
||||
* The polygon will be drawn on **img**
|
||||
* The vertices of the polygon are the set of points in **ppt**
|
||||
* The total number of vertices to be drawn are **npt**
|
||||
* The number of polygons to be drawn is only **1**
|
||||
* The color of the polygon is defined by **Scalar( 255, 255, 255)**, which is the RGB value for *white*
|
||||
|
||||
* *rectangle*
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
rectangle( rook_image,
|
||||
Point( 0, 7*w/8.0 ),
|
||||
Point( w, w),
|
||||
Scalar( 0, 255, 255 ),
|
||||
-1,
|
||||
8 );
|
||||
|
||||
Finally we have the :rectangle:`rectangle <>` function (we did not create a special function for this guy). We note that:
|
||||
|
||||
* The rectangle will be drawn on **rook_image**
|
||||
* Two opposite vertices of the rectangle are defined by ** Point( 0, 7*w/8.0 )** and **Point( w, w)**
|
||||
* The color of the rectangle is given by **Scalar(0, 255, 255)** which is the RGB value for *yellow*
|
||||
* Since the thickness value is given by **-1**, the rectangle will be filled.
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
Compiling and running your program should give you a result like this:
|
||||
|
||||
.. image:: images/Drawing_1_Tutorial_Result_0.png
|
||||
:alt: Drawing Tutorial 1 - Final Result
|
||||
:align: center
|
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 6.3 KiB |
@ -1,285 +0,0 @@
|
||||
.. _Drawing_2:
|
||||
|
||||
Fancy Drawing!
|
||||
****************
|
||||
|
||||
Goals
|
||||
======
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Use the *Random Number generator class* (:rng:`RNG <>`) and how to get a random number from a uniform distribution.
|
||||
* Display Text on an OpenCV window by using the function :put_text:`putText <>`
|
||||
|
||||
Code
|
||||
=====
|
||||
* In the previous tutorial we drew diverse geometric figures, giving as input parameters such as coordinates (in the form of :point:`Points <>`), color, thickness, etc. You might have noticed that we gave specific values for these arguments.
|
||||
|
||||
* In this tutorial, we intend to use *random* values for the drawing parameters. Also, we intend to populate our image with a big number of geometric figures. Since we will be initializing them in a random fashion, this process will be automatic and made by using *loops*
|
||||
|
||||
* This code is in your OpenCV sample folder. Otherwise you can grab it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/Basic/Drawing_2.cpp>`_
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
#. Let's start by checking out the *main* function. We observe that first thing we do is creating a *Random Number Generator* object (RNG):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
RNG rng( 0xFFFFFFFF );
|
||||
|
||||
RNG implements a random number generator. In this example, *rng* is a RNG element initialized with the value *0xFFFFFFFF*
|
||||
|
||||
#. Then we create a matrix initialized to *zeros* (which means that it will appear as black), specifying its height, width and its type:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// Initialize a matrix filled with zeros
|
||||
Mat image = Mat::zeros( window_height, window_width, CV_8UC3 );
|
||||
|
||||
/// Show it in a window during DELAY ms
|
||||
imshow( window_name, image );
|
||||
|
||||
#. Then we proceed to draw crazy stuff. After taking a look at the code, you can see that it is mainly divided in 8 sections, defined as functions:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// Now, let's draw some lines
|
||||
c = Drawing_Random_Lines(image, window_name, rng);
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Go on drawing, this time nice rectangles
|
||||
c = Drawing_Random_Rectangles(image, window_name, rng);
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Draw some ellipses
|
||||
c = Drawing_Random_Ellipses( image, window_name, rng );
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Now some polylines
|
||||
c = Drawing_Random_Polylines( image, window_name, rng );
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Draw filled polygons
|
||||
c = Drawing_Random_Filled_Polygons( image, window_name, rng );
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Draw circles
|
||||
c = Drawing_Random_Circles( image, window_name, rng );
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Display text in random positions
|
||||
c = Displaying_Random_Text( image, window_name, rng );
|
||||
if( c != 0 ) return 0;
|
||||
|
||||
/// Displaying the big end!
|
||||
c = Displaying_Big_End( image, window_name, rng );
|
||||
|
||||
All of these functions follow the same pattern, so we will analyze only a couple of them, since the same explanation applies for all.
|
||||
|
||||
#. Checking out the function **Drawing_Random_Lines**:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/**
|
||||
* @function Drawing_Random_Lines
|
||||
*/
|
||||
int Drawing_Random_Lines( Mat image, char* window_name, RNG rng )
|
||||
{
|
||||
int lineType = 8;
|
||||
Point pt1, pt2;
|
||||
|
||||
for( int i = 0; i < NUMBER; i++ )
|
||||
{
|
||||
pt1.x = rng.uniform( x_1, x_2 );
|
||||
pt1.y = rng.uniform( y_1, y_2 );
|
||||
pt2.x = rng.uniform( x_1, x_2 );
|
||||
pt2.y = rng.uniform( y_1, y_2 );
|
||||
|
||||
line( image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8 );
|
||||
imshow( window_name, image );
|
||||
if( waitKey( DELAY ) >= 0 )
|
||||
{ return -1; }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
We can observe the following:
|
||||
|
||||
* The *for* loop will repeat **NUMBER** times. Since the function :line:`line <>` is inside this loop, that means that **NUMBER** lines will be generated.
|
||||
* The line extremes are given by *pt1* and *pt2*. For *pt1* we can see that:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
pt1.x = rng.uniform( x_1, x_2 );
|
||||
pt1.y = rng.uniform( y_1, y_2 );
|
||||
|
||||
* We know that **rng** is a *Random number generator* object. In the code above we are calling **rng.uniform(a,b)**. This generates a radombly uniformed distribution between the values **a** and **b** (inclusive in **a**, exclusive in **b**).
|
||||
|
||||
* From the explanation above, we deduce that the extremes *pt1* and *pt2* will be random values, so the lines positions will be quite impredictable, giving a nice visual effect (check out the Result section below).
|
||||
|
||||
* As another observation, we notice that in the :line:`line <>` arguments, for the *color* input we enter:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
randomColor(rng)
|
||||
|
||||
Let's check the function implementation:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
static Scalar randomColor( RNG& rng )
|
||||
{
|
||||
int icolor = (unsigned) rng;
|
||||
return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
|
||||
}
|
||||
|
||||
As we can see, the return value is an *Scalar* with 3 randomly initialized values, which are used as the *R*, *G* and *B* parameters for the line color. Hence, the color of the lines will be random too!
|
||||
|
||||
#. The explanation above applies for the other functions generating circles, ellipses, polygones, etc. The parameters such as *center* and *vertices* are also generated randomly.
|
||||
|
||||
#. Before finishing, we also should take a look at the functions *Display_Random_Text* and *Displaying_Big_End*, since they both have a few interesting features:
|
||||
|
||||
#. **Display_Random_Text:**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
|
||||
{
|
||||
int lineType = 8;
|
||||
|
||||
for ( int i = 1; i < NUMBER; i++ )
|
||||
{
|
||||
Point org;
|
||||
org.x = rng.uniform(x_1, x_2);
|
||||
org.y = rng.uniform(y_1, y_2);
|
||||
|
||||
putText( image, "Testing text rendering", org, rng.uniform(0,8),
|
||||
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
|
||||
|
||||
imshow( window_name, image );
|
||||
if( waitKey(DELAY) >= 0 )
|
||||
{ return -1; }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Everything looks familiar but the expression:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
putText( image, "Testing text rendering", org, rng.uniform(0,8),
|
||||
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
|
||||
|
||||
|
||||
So, what does the function :put_text:`putText <>` do? In our example:
|
||||
|
||||
* Draws the text **"Testing text rendering"** in **image**
|
||||
* The bottom-left corner of the text will be located in the Point **org**
|
||||
* The font type is a random integer value in the range: :math:`[0, 8>`.
|
||||
* The scale of the font is denoted by the expression **rng.uniform(0, 100)x0.05 + 0.1** (meaning its range is: :math:`[0.1, 5.1>`)
|
||||
* The text color is random (denoted by **randomColor(rng)**)
|
||||
* The text thickness ranges between 1 and 10, as specified by **rng.uniform(1,10)**
|
||||
|
||||
As a result, we will get (analagously to the other drawing functions) **NUMBER** texts over our image, in random locations.
|
||||
|
||||
#. **Displaying_Big_End**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
int Displaying_Big_End( Mat image, char* window_name, RNG rng )
|
||||
{
|
||||
Size textsize = getTextSize("OpenCV forever!", CV_FONT_HERSHEY_COMPLEX, 3, 5, 0);
|
||||
Point org((window_width - textsize.width)/2, (window_height - textsize.height)/2);
|
||||
int lineType = 8;
|
||||
|
||||
Mat image2;
|
||||
|
||||
for( int i = 0; i < 255; i += 2 )
|
||||
{
|
||||
image2 = image - Scalar::all(i);
|
||||
putText( image2, "OpenCV forever!", org, CV_FONT_HERSHEY_COMPLEX, 3,
|
||||
Scalar(i, i, 255), 5, lineType );
|
||||
|
||||
imshow( window_name, image2 );
|
||||
if( waitKey(DELAY) >= 0 )
|
||||
{ return -1; }
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Besides the function **getTextSize** (which gets the size of the argument text), the new operation we can observe is inside the *foor* loop:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
image2 = image - Scalar::all(i)
|
||||
|
||||
So, **image2** is the substraction of **image** and **Scalar::all(i)**. In fact, what happens here is that every pixel of **image2** will be the result of substracting every pixel of **image** minus the value of **i** (remember that for each pixel we are considering three values such as R, G and B, so each of them will be affected)
|
||||
|
||||
Also remember that the substraction operation *always* performs internally a **saturate** operation, which means that the result obtained will always be inside the allowed range (no negative and between 0 and 255 for our example).
|
||||
|
||||
|
||||
Result
|
||||
========
|
||||
|
||||
As you just saw in the Code section, the program will sequentially execute diverse drawing functions, which will produce:
|
||||
|
||||
#. First a random set of *NUMBER* lines will appear on screen such as it can be seen in this screenshot:
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_0.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 0
|
||||
:align: center
|
||||
|
||||
#. Then, a new set of figures, these time *rectangles* will follow:
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_1.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 1
|
||||
:align: center
|
||||
|
||||
#. Now some ellipses will appear, each of them with random position, size, thickness and arc length:
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_2.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 2
|
||||
:align: center
|
||||
|
||||
#. Now, *polylines* with 03 segments will appear on screen, again in random configurations.
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_3.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 3
|
||||
:align: center
|
||||
|
||||
#. Filled polygons (in this example triangles) will follow:
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_4.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 4
|
||||
:align: center
|
||||
|
||||
#. The last geometric figure to appear: circles!
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_5.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 5
|
||||
:align: center
|
||||
|
||||
#. Near the end, the text *"Testing Text Rendering"* will appear in a variety of fonts, sizes, colors and positions.
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_6.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 6
|
||||
:align: center
|
||||
|
||||
#. And the big end (which by the way expresses a big truth too):
|
||||
|
||||
.. image:: images/Drawing_2_Tutorial_Result_7.png
|
||||
:height: 300px
|
||||
:alt: Drawing Tutorial 2 - Final Result 7
|
||||
:align: center
|
||||
|
Before Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 254 KiB |
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 65 KiB |
@ -1,162 +0,0 @@
|
||||
.. _Adding_Trackbars:
|
||||
|
||||
Adding a Trackbar to our applications!
|
||||
***************************************
|
||||
|
||||
* In the previous tutorials (about *linear blending* and the *brightness and contrast adjustments*) you might have noted that we needed to give some **input** to our programs, such as :math:`\alpha` and :math:`beta`. We accomplished that by entering this data using the Terminal
|
||||
|
||||
* Well, it is time to use some fancy GUI tools. OpenCV provides some GUI utilities (*highgui.h*) for you. An example of this is a **Trackbar**
|
||||
|
||||
.. image:: images/Adding_Trackbars_Tutorial_Trackbar.png
|
||||
:alt: Trackbar example
|
||||
:align: center
|
||||
|
||||
* In this tutorial we will just modify our two previous programs so that they get the input information from the trackbar.
|
||||
|
||||
|
||||
Goals
|
||||
======
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Add a Trackbar in an OpenCV window by using :create_trackbar:`createTrackbar <>`
|
||||
|
||||
Code
|
||||
=====
|
||||
|
||||
Let's modify the program made in the tutorial :ref:`Adding_Images`. We will let the user enter the :math:`\alpha` value by using the Trackbar.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global Variables
|
||||
const int alpha_slider_max = 100;
|
||||
int alpha_slider;
|
||||
double alpha;
|
||||
double beta;
|
||||
|
||||
/// Matrices to store images
|
||||
Mat src1;
|
||||
Mat src2;
|
||||
Mat dst;
|
||||
|
||||
/**
|
||||
* @function on_trackbar
|
||||
* @brief Callback for trackbar
|
||||
*/
|
||||
void on_trackbar( int, void* )
|
||||
{
|
||||
alpha = (double) alpha_slider/alpha_slider_max ;
|
||||
beta = ( 1.0 - alpha );
|
||||
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
|
||||
imshow( "Linear Blend", dst );
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Read image ( same size, same type )
|
||||
src1 = imread("../../images/LinuxLogo.jpg");
|
||||
src2 = imread("../../images/WindowsLogo.jpg");
|
||||
|
||||
if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
|
||||
if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
|
||||
|
||||
/// Initialize values
|
||||
alpha_slider = 0;
|
||||
|
||||
/// Create Windows
|
||||
namedWindow("Linear Blend", 1);
|
||||
|
||||
/// Create Trackbars
|
||||
char TrackbarName[50];
|
||||
sprintf( TrackbarName, "Alpha x %d", alpha_slider_max );
|
||||
|
||||
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
|
||||
|
||||
/// Show some stuff
|
||||
on_trackbar( alpha_slider, 0 );
|
||||
|
||||
/// Wait until user press some key
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
We only analyze the code that is related to Trackbar:
|
||||
|
||||
#. First, we load 02 images, which are going to be blended.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
src1 = imread("../../images/LinuxLogo.jpg");
|
||||
src2 = imread("../../images/WindowsLogo.jpg");
|
||||
|
||||
#. To create a trackbar, first we have to create the window in which it is going to be located. So:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow("Linear Blend", 1);
|
||||
|
||||
#. Now we can create the Trackbar:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
|
||||
|
||||
Note the following:
|
||||
|
||||
* Our Trackbar has a label **TrackbarName**
|
||||
* The Trackbar is located in the window named **"Linear Blend"**
|
||||
* The Trackbar values will be in the range from :math:`0` to **alpha_slider_max** (the minimum limit is always **zero**).
|
||||
* The numerical value of Trackbar is stored in **alpha_slider**
|
||||
* Whenever the user moves the Trackbar, the callback function **on_trackbar** is called
|
||||
|
||||
#. Finally, we have to define the callback function **on_trackbar**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
void on_trackbar( int, void* )
|
||||
{
|
||||
alpha = (double) alpha_slider/alpha_slider_max ;
|
||||
beta = ( 1.0 - alpha );
|
||||
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
|
||||
imshow( "Linear Blend", dst );
|
||||
}
|
||||
|
||||
Note that:
|
||||
|
||||
* We use the value of **alpha_slider** (integer) to get a double value for **alpha**.
|
||||
* **alpha_slider** is updated each time the trackbar is displaced by the user.
|
||||
* We define *src1*, *src2*, *dist*, *alpha*, *alpha_slider* and *beta* as global variables, so they can be used everywhere.
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
* Our program produces the following output:
|
||||
|
||||
.. image:: images/Adding_Trackbars_Tutorial_Result_0.png
|
||||
:alt: Adding Trackbars - Windows Linux
|
||||
:align: center
|
||||
|
||||
* As a manner of practice, you can also add 02 trackbars for the program made in :ref:`Basic_Linear_Transform`. One trackbar to set :math:`\alpha` and another for :math:`\beta`. The output might look like:
|
||||
|
||||
.. image:: images/Adding_Trackbars_Tutorial_Result_1.png
|
||||
:alt: Adding Trackbars - Lena
|
||||
:height: 500px
|
||||
:align: center
|
||||
|
||||
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 212 KiB |
Before Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 884 KiB |
Before Width: | Height: | Size: 550 KiB |
Before Width: | Height: | Size: 299 KiB |
Before Width: | Height: | Size: 2.7 KiB |
@ -1,119 +0,0 @@
|
||||
.. _Adding_Images:
|
||||
|
||||
Adding (blending) two images using OpenCV
|
||||
*******************************************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* What is *linear blending* and why it is useful.
|
||||
* Add two images using :add_weighted:`addWeighted <>`
|
||||
|
||||
Cool Theory
|
||||
=================
|
||||
|
||||
.. note::
|
||||
|
||||
The explanation below belongs to the book `Computer Vision: Algorithms and Applications <http://szeliski.org/Book/>`_ by Richard Szeliski
|
||||
|
||||
From our previous tutorial, we know already a bit of *Pixel operators*. An interesting dyadic (two-input) operator is the *linear blend operator*:
|
||||
|
||||
.. math::
|
||||
|
||||
g(x) = (1 - \alpha)f_{0}(x) + \alpha f_{1}(x)
|
||||
|
||||
By varying :math:`\alpha` from :math:`0 \rightarrow 1` this operator can be used to perform a temporal *cross-disolve* between two images or videos, as seen in slide shows and film production (cool, eh?)
|
||||
|
||||
Code
|
||||
=====
|
||||
|
||||
As usual, after the not-so-lengthy explanation, let's go to the code. Here it is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
double alpha = 0.5; double beta; double input;
|
||||
|
||||
Mat src1, src2, dst;
|
||||
|
||||
/// Ask the user enter alpha
|
||||
std::cout<<" Simple Linear Blender "<<std::endl;
|
||||
std::cout<<"-----------------------"<<std::endl;
|
||||
std::cout<<"* Enter alpha [0-1]: ";
|
||||
std::cin>>input;
|
||||
|
||||
/// We use the alpha provided by the user iff it is between 0 and 1
|
||||
if( alpha >= 0 && alpha <= 1 )
|
||||
{ alpha = input; }
|
||||
|
||||
/// Read image ( same size, same type )
|
||||
src1 = imread("../../images/LinuxLogo.jpg");
|
||||
src2 = imread("../../images/WindowsLogo.jpg");
|
||||
|
||||
if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
|
||||
if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
|
||||
|
||||
/// Create Windows
|
||||
namedWindow("Linear Blend", 1);
|
||||
|
||||
beta = ( 1.0 - alpha );
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
|
||||
imshow( "Linear Blend", dst );
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
#. Since we are going to perform:
|
||||
|
||||
.. math::
|
||||
|
||||
g(x) = (1 - \alpha)f_{0}(x) + \alpha f_{1}(x)
|
||||
|
||||
We need two source images (:math:`f_{0}(x)` and :math:`f_{1}(x)`). So, we load them in the usual way:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
src1 = imread("../../images/LinuxLogo.jpg");
|
||||
src2 = imread("../../images/WindowsLogo.jpg");
|
||||
|
||||
.. warning::
|
||||
|
||||
Since we are *adding* *src1* and *src2*, they both have to be of the same size (width and height) and type.
|
||||
|
||||
#. Now we need to generate the :math:`g(x)` image. For this, the function :add_weighted:`addWeighted <>` comes quite handy:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
beta = ( 1.0 - alpha );
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
|
||||
since :add_weighted:`addWeighted <>` produces:
|
||||
|
||||
.. math::
|
||||
|
||||
dst = \alpha \cdot src1 + \beta \cdot src2 + \gamma
|
||||
|
||||
In this case, :math:`\gamma` is the argument :math:`0.0` in the code above.
|
||||
|
||||
#. Create windows, show the images and wait for the user to end the program.
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
.. image:: images/Adding_Images_Tutorial_Result_0.png
|
||||
:alt: Blending Images Tutorial - Final Result
|
||||
:align: center
|
Before Width: | Height: | Size: 64 KiB |
@ -1,198 +0,0 @@
|
||||
.. _Basic_Linear_Transform:
|
||||
|
||||
Changing the contrast and brightness of an image!
|
||||
***************************************************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Access pixel values
|
||||
|
||||
* Initialize a matrix with zeros
|
||||
|
||||
* Learn what :saturate_cast:`saturate_cast <>` does and why it is useful
|
||||
|
||||
* Get some cool info about pixel transformations
|
||||
|
||||
Cool Theory
|
||||
=================
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book `Computer Vision: Algorithms and Applications <http://szeliski.org/Book/>`_ by Richard Szeliski
|
||||
|
||||
Image Processing
|
||||
--------------------
|
||||
|
||||
* A general image processing operator is a function that takes one or more input images and produces an output image.
|
||||
|
||||
* Image transforms can be seen as:
|
||||
|
||||
* Point operators (pixel transforms)
|
||||
* Neighborhood (area-based) operators
|
||||
|
||||
|
||||
Pixel Transforms
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
||||
* In this kind of image processing transform, each output pixel's value depends on only the corresponding input pixel value (plus, potentially, some globally collected information or parameters).
|
||||
|
||||
* Examples of such operators include *brightness and contrast adjustments* as well as color correction and transformations.
|
||||
|
||||
Brightness and contrast adjustments
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Two commonly used point processes are *multiplication* and *addition* with a constant:
|
||||
|
||||
.. math::
|
||||
|
||||
g(x) = \alpha f(x) + \beta
|
||||
|
||||
* The parameters :math:`\alpha > 0` and :math:`\beta` are often called the *gain* and *bias* parameters; sometimes these parameters are said to control *contrast* and *brightness* respectively.
|
||||
|
||||
* You can think of :math:`f(x)` as the source image pixels and :math:`g(x)` as the output image pixels. Then, more conveniently we can write the expression as:
|
||||
|
||||
.. math::
|
||||
|
||||
g(i,j) = \alpha \cdot f(i,j) + \beta
|
||||
where :math:`i` and :math:`j` indicates that the pixel is located in the *i-th* row and *j-th* column.
|
||||
|
||||
Code
|
||||
=====
|
||||
|
||||
* The following code performs the operation :math:`g(i,j) = \alpha \cdot f(i,j) + \beta`
|
||||
* Here it is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
double alpha; /**< Simple contrast control */
|
||||
int beta; /**< Simple brightness control */
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Read image given by user
|
||||
Mat image = imread( argv[1] );
|
||||
Mat new_image = Mat::zeros( image.size(), image.type() );
|
||||
|
||||
/// Initialize values
|
||||
std::cout<<" Basic Linear Transforms "<<std::endl;
|
||||
std::cout<<"-------------------------"<<std::endl;
|
||||
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
|
||||
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;
|
||||
|
||||
/// Do the operation new_image(i,j) = alpha*image(i,j) + beta
|
||||
for( int y = 0; y < image.rows; y++ )
|
||||
{ for( int x = 0; x < image.cols; x++ )
|
||||
{ for( int c = 0; c < 3; c++ )
|
||||
{
|
||||
new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Windows
|
||||
namedWindow("Original Image", 1);
|
||||
namedWindow("New Image", 1);
|
||||
|
||||
/// Show stuff
|
||||
imshow("Original Image", image);
|
||||
imshow("New Image", new_image);
|
||||
|
||||
/// Wait until user press some key
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
#. We begin by creating parameters to save :math:`\alpha` and :math:`\beta` to be entered by the user:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
double alpha;
|
||||
int beta;
|
||||
|
||||
|
||||
#. We load an image using :imread:`imread <>` and save it in a Mat object:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Mat image = imread( argv[1] );
|
||||
|
||||
#. Now, since we will make some transformations to this image, we need a new Mat object to store it. Also, we want this to have the following features:
|
||||
|
||||
* Initial pixel values equal to zero
|
||||
* Same size and type as the original image
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Mat new_image = Mat::zeros( image.size(), image.type() );
|
||||
|
||||
We observe that :mat_zeros:`Mat::zeros <>` returns a Matlab-style zero initializer based on *image.size()* and *image.type()*
|
||||
|
||||
#. Now, to perform the operation :math:`g(i,j) = \alpha \cdot f(i,j) + \beta` we will access to each pixel in image. Since we are operating with RGB images, we will have three values per pixel (R, G and B), so we will also access them separately. Here is the piece of code:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for( int y = 0; y < image.rows; y++ )
|
||||
{ for( int x = 0; x < image.cols; x++ )
|
||||
{ for( int c = 0; c < 3; c++ )
|
||||
{ new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta ); }
|
||||
}
|
||||
}
|
||||
|
||||
Notice the following:
|
||||
|
||||
* To access each pixel in the images we are using this syntax: *image.at<Vec3b>(y,x)[c]* where *y* is the row, *x* is the column and *c* is R, G or B (0, 1 or 2).
|
||||
|
||||
* Since the operation :math:`\alpha \cdot p(i,j) + \beta` can give values out of range or not integers (if :math:`\alpha` is float), we use :saturate_cast:`saturate_cast <>` to make sure the values are valid.
|
||||
|
||||
|
||||
#. Finally, we create windows and show the images, the usual way.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow("Original Image", 1);
|
||||
namedWindow("New Image", 1);
|
||||
|
||||
imshow("Original Image", image);
|
||||
imshow("New Image", new_image);
|
||||
|
||||
waitKey(0);
|
||||
|
||||
.. note::
|
||||
|
||||
Instead of using the **for** loops to access each pixel, we could have simply used this command:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
image.convertTo(new_image, -1, alpha, beta);
|
||||
|
||||
where :convert_to:`convertTo <>` would effectively perform *new_image = a*image + beta*. However, we wanted to show you how to access each pixel. In any case, both methods give the same result.
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
* Running our code and using :math:`\alpha = 2.2` and :math:`\beta = 50`
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ ./BasicLinearTransforms lena.png
|
||||
Basic Linear Transforms
|
||||
-------------------------
|
||||
* Enter the alpha value [1.0-3.0]: 2.2
|
||||
* Enter the beta value [0-100]: 50
|
||||
|
||||
* We get this:
|
||||
|
||||
.. image:: images/Basic_Linear_Transform_Tutorial_Result_0.png
|
||||
:height: 400px
|
||||
:alt: Basic Linear Transform - Final Result
|
||||
:align: center
|
Before Width: | Height: | Size: 860 KiB |
Before Width: | Height: | Size: 472 KiB |
Before Width: | Height: | Size: 251 KiB |
@ -1,38 +0,0 @@
|
||||
.. _FloodFill:
|
||||
|
||||
How to fill an image
|
||||
**********************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Use the :flood_fill:`FloodFill <>` OpenCV function
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/Image_Processing/Morphology_1.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
|
@ -1,279 +0,0 @@
|
||||
.. _Morphology_1:
|
||||
|
||||
Eroding and Dilating
|
||||
**********************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Apply two very common morphology operators: Dilation and Erosion. For this purpose, you will use the following OpenCV functions:
|
||||
|
||||
* :erode:`erode <>`
|
||||
* :dilate:`dilate <>`
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
Morphological Operations
|
||||
--------------------------
|
||||
|
||||
* In short: A set of operations that process images based on shapes. Morphological operations apply a *structuring element* to an input image and generate an output image.
|
||||
|
||||
* The most basic morphological operations are two: Erosion and Dilation. They have a wide array of uses, i.e. :
|
||||
|
||||
* Removing noise
|
||||
|
||||
* Isolation of individual elements and joining disparate elements in an image.
|
||||
|
||||
* Finding of intensity bumps or holes in an image
|
||||
|
||||
* We will explain dilation and erosion briefly, using the following image as an example:
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Theory_Original_Image.png
|
||||
:alt: Original image
|
||||
:height: 100px
|
||||
:align: center
|
||||
|
||||
Dilation
|
||||
^^^^^^^^^
|
||||
|
||||
* This operations consists of convoluting an image :math:`A` with some kernel (:math:`B`), which can have any shape or size, usually a square or circle.
|
||||
|
||||
* The kernel :math:`B` has a defined *anchor point*, usually being the center of the kernel.
|
||||
|
||||
* As the kernel :math:`B` is scanned over the image, we compute the maximal pixel value overlapped by :math:`B` and replace the image pixel in the anchor point position with that maximal value. As you can deduce, this maximizing operation causes bright regions within an image to "grow" (therefore the name *dilation*). Take as an example the image above. Applying dilation we can get:
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Theory_Dilation.png
|
||||
:alt: Dilation result - Theory example
|
||||
:height: 100px
|
||||
:align: center
|
||||
The background (bright) dilates around the black regions of the letter.
|
||||
|
||||
Erosion
|
||||
^^^^^^^^
|
||||
|
||||
* This operation is the sister of dilation. What this does is to compute a local minimum over the area of the kernel.
|
||||
|
||||
* As the kernel :math:`B` is scanned over the image, we compute the minimal pixel value overlapped by :math:`B` and replace the image pixel under the anchor point with that minimal value.
|
||||
|
||||
* Analagously to the example for dilation, we can apply the erosion operator to the original image (shown above). You can see in the result below that the bright areas of the image (the background, apparently), get thinner, whereas the dark zones (the "writing"( gets bigger.
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Theory_Erosion.png
|
||||
:alt: Erosion result - Theory example
|
||||
:height: 100px
|
||||
:align: center
|
||||
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "highgui.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, erosion_dst, dilation_dst;
|
||||
|
||||
int erosion_elem = 0;
|
||||
int erosion_size = 0;
|
||||
int dilation_elem = 0;
|
||||
int dilation_size = 0;
|
||||
int const max_elem = 2;
|
||||
int const max_kernel_size = 21;
|
||||
|
||||
/** Function Headers */
|
||||
void Erosion( int, void* );
|
||||
void Dilation( int, void* );
|
||||
|
||||
/** @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
/// Create windows
|
||||
namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE );
|
||||
namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );
|
||||
cvMoveWindow( "Dilation Demo", src.cols, 0 );
|
||||
|
||||
/// Create Erosion Trackbar
|
||||
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
|
||||
&erosion_elem, max_elem,
|
||||
Erosion );
|
||||
|
||||
createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
|
||||
&erosion_size, max_kernel_size,
|
||||
Erosion );
|
||||
|
||||
/// Create Dilation Trackbar
|
||||
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
|
||||
&dilation_elem, max_elem,
|
||||
Dilation );
|
||||
|
||||
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
|
||||
&dilation_size, max_kernel_size,
|
||||
Dilation );
|
||||
|
||||
/// Default start
|
||||
Erosion( 0, 0 );
|
||||
Dilation( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** @function Erosion */
|
||||
void Erosion( int, void* )
|
||||
{
|
||||
int erosion_type;
|
||||
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
|
||||
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
|
||||
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( erosion_type,
|
||||
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
|
||||
Point( erosion_size, erosion_size ) );
|
||||
|
||||
/// Apply the erosion operation
|
||||
erode( src, erosion_dst, element );
|
||||
imshow( "Erosion Demo", erosion_dst );
|
||||
}
|
||||
|
||||
/** @function Dilation */
|
||||
void Dilation( int, void* )
|
||||
{
|
||||
int dilation_type;
|
||||
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
|
||||
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
|
||||
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( dilation_type,
|
||||
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
|
||||
Point( dilation_size, dilation_size ) );
|
||||
/// Apply the dilation operation
|
||||
dilate( src, dilation_dst, element );
|
||||
imshow( "Dilation Demo", dilation_dst );
|
||||
}
|
||||
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
#. Most of the stuff shown is known by you (if you have any doubt, please refer to the tutorials in previous sections). Let's check the general structure of the program:
|
||||
|
||||
* Load an image (can be RGB or grayscale)
|
||||
|
||||
* Create two windows (one for dilation output, the other for erosion)
|
||||
|
||||
* Create a set of 02 Trackbars for each operation:
|
||||
|
||||
* The first trackbar "Element" returns either **erosion_elem** or **dilation_elem**
|
||||
* The second trackbar "Kernel size" return **erosion_size** or **dilation_size** for the corresponding operation.
|
||||
|
||||
* Every time we 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.
|
||||
|
||||
Let's analyze these two functions:
|
||||
|
||||
#. **erosion:**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/** @function Erosion */
|
||||
void Erosion( int, void* )
|
||||
{
|
||||
int erosion_type;
|
||||
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
|
||||
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
|
||||
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( erosion_type,
|
||||
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
|
||||
Point( erosion_size, erosion_size ) );
|
||||
/// Apply the erosion operation
|
||||
erode( src, erosion_dst, element );
|
||||
imshow( "Erosion Demo", erosion_dst );
|
||||
}
|
||||
|
||||
* The function that performs the *erosion* operation is :erode:`erode <>`. As we can see, it receives three arguments:
|
||||
|
||||
* *src*: The source image
|
||||
* *erosion_dst*: The output image
|
||||
* *element*: This is the kernel we will use to perform the operation. If we do not specify, the default is a simple :math:`3x3` matrix. Otherwise, we can specify its shape. For this, we need to use the function :get_structuring_element:`getStructuringElement <>`:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Mat element = getStructuringElement( erosion_type,
|
||||
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
|
||||
Point( erosion_size, erosion_size ) );
|
||||
|
||||
We can choose any of three shapes for our kernel:
|
||||
|
||||
* Rectangular box: MORPH_RECT
|
||||
* Cross: MORPH_CROSS
|
||||
* Ellipse: MORPH_ELLIPSE
|
||||
|
||||
Then, we just have to specify the size of our kernel and the *anchor point*. If not specified, it is assumed to be in the center.
|
||||
|
||||
* That is all. We are ready to perform the erosion of our image.
|
||||
|
||||
.. note::
|
||||
Additionally, there is another parameter that allows you to perform multiple erosions (iterations) at once. We are not using it in this simple tutorial, though. You can check out the Reference for more details.
|
||||
|
||||
|
||||
#. **dilation:**
|
||||
|
||||
The code is below. As you can see, it is completely similar to the snippet of code for **erosion**. Here we also have the option of defining our kernel, its anchor point and the size of the operator to be used.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/** @function Dilation */
|
||||
void Dilation( int, void* )
|
||||
{
|
||||
int dilation_type;
|
||||
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
|
||||
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
|
||||
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
|
||||
|
||||
Mat element = getStructuringElement( dilation_type,
|
||||
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
|
||||
Point( dilation_size, dilation_size ) );
|
||||
/// Apply the dilation operation
|
||||
dilate( src, dilation_dst, element );
|
||||
imshow( "Dilation Demo", dilation_dst );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
* Compile the code above and execute it with an image as argument. For instance, using this image:
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Original_Image.png
|
||||
:alt: Original image
|
||||
:height: 200px
|
||||
:align: center
|
||||
|
||||
We get the results below. Varying the indices in the Trackbars give different output images, naturally. Try them out! You can even try to add a third Trackbar to control the number of iterations.
|
||||
|
||||
.. image:: images/Morphology_1_Tutorial_Cover.png
|
||||
:alt: Dilation and Erosion application
|
||||
:height: 400px
|
||||
:align: center
|
||||
|
Before Width: | Height: | Size: 407 KiB |
Before Width: | Height: | Size: 160 KiB |
Before Width: | Height: | Size: 211 KiB |
Before Width: | Height: | Size: 302 KiB |
Before Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 2.4 KiB |
@ -1,286 +0,0 @@
|
||||
.. _Morphology_2:
|
||||
|
||||
More Morphology Transformations
|
||||
*********************************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Use the OpenCV function :morphology_ex:`morphologyEx <>` to apply Morphological Transformation such as:
|
||||
|
||||
* Opening
|
||||
* Closing
|
||||
* Morphological Gradient
|
||||
* Top Hat
|
||||
* Black Hat
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
In the previous tutorial we covered two basic Morphology operations:
|
||||
|
||||
* Erosion
|
||||
|
||||
* Dilation.
|
||||
|
||||
Based on these two we can effectuate more sophisticated transformations to our images. Here we discuss briefly 05 operations offered by OpenCV:
|
||||
|
||||
Opening
|
||||
---------
|
||||
|
||||
* It is obtained by the erosion of an image followed by a dilation.
|
||||
|
||||
.. math::
|
||||
|
||||
dst = open( src, element) = dilate( erode( src, element ) )
|
||||
|
||||
* Useful for removing small objects (it is assumed that the objects are bright on a dark foreground)
|
||||
|
||||
* For instance, check out the example below. The image at the left is the original and the image at the right is the result after applying the opening transformation. We can observe that the small spaces in the corners of the letter tend to dissapear.
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Theory_Opening.png
|
||||
:height: 150pt
|
||||
:alt: Opening
|
||||
:align: center
|
||||
|
||||
Closing
|
||||
---------
|
||||
|
||||
* It is obtained by the dilation of an image followed by an erosion.
|
||||
|
||||
.. math::
|
||||
|
||||
dst = close( src, element ) = erode( dilate( src, element ) )
|
||||
|
||||
* Useful to remove small holes (dark regions).
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Theory_Closing.png
|
||||
:height: 150pt
|
||||
:alt: Closing example
|
||||
:align: center
|
||||
|
||||
|
||||
Morphological Gradient
|
||||
------------------------
|
||||
|
||||
* It is the difference between the dilation and the erosion of an image.
|
||||
|
||||
.. math::
|
||||
|
||||
dst = morph_{grad}( src, element ) = dilate( src, element ) - erode( src, element )
|
||||
|
||||
* It is useful for finding the outline of an object as can be seen below:
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Theory_Gradient.png
|
||||
:height: 150pt
|
||||
:alt: Gradient
|
||||
:align: center
|
||||
|
||||
|
||||
Top Hat
|
||||
---------
|
||||
|
||||
* It is the difference between an input image and its opening.
|
||||
|
||||
.. math::
|
||||
|
||||
dst = tophat( src, element ) = src - open( src, element )
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Theory_TopHat.png
|
||||
:height: 150pt
|
||||
:alt: Top Hat
|
||||
:align: center
|
||||
|
||||
Black Hat
|
||||
----------
|
||||
|
||||
* It is the difference between the closing and its input image
|
||||
|
||||
.. math::
|
||||
|
||||
dst = blackhat( src, element ) = close( src, element ) - src
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Theory_BlackHat.png
|
||||
:height: 150pt
|
||||
:alt: Black Hat
|
||||
:align: center
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ImgProc/Morphology_2.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, dst;
|
||||
|
||||
int morph_elem = 0;
|
||||
int morph_size = 0;
|
||||
int morph_operator = 0;
|
||||
int const max_operator = 4;
|
||||
int const max_elem = 2;
|
||||
int const max_kernel_size = 21;
|
||||
|
||||
char* window_name = "Morphology Transformations Demo";
|
||||
|
||||
/** Function Headers */
|
||||
void Morphology_Operations( int, void* );
|
||||
|
||||
/** @function main */
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
{ return -1; }
|
||||
|
||||
/// Create window
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
|
||||
/// Create Trackbar to select Morphology operation
|
||||
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations );
|
||||
|
||||
/// Create Trackbar to select kernel type
|
||||
createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
|
||||
&morph_elem, max_elem,
|
||||
Morphology_Operations );
|
||||
|
||||
/// Create Trackbar to choose kernel size
|
||||
createTrackbar( "Kernel size:\n 2n +1", window_name,
|
||||
&morph_size, max_kernel_size,
|
||||
Morphology_Operations );
|
||||
|
||||
/// Default start
|
||||
Morphology_Operations( 0, 0 );
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function Morphology_Operations
|
||||
*/
|
||||
void Morphology_Operations( int, void* )
|
||||
{
|
||||
// Since MORPH_X : 2,3,4,5 and 6
|
||||
int operation = morph_operator + 2;
|
||||
|
||||
Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
|
||||
|
||||
/// Apply the specified morphology operation
|
||||
morphologyEx( src, dst, operation, element );
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
#. Let's check the general structure of the program:
|
||||
|
||||
* Load an image
|
||||
|
||||
* Create a window to display results of the Morphological operations
|
||||
|
||||
* Create 03 Trackbars for the user to enter parameters:
|
||||
|
||||
* The first trackbar **"Operator"** returns the kind of morphology operation to use (**morph_operator**).
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat",
|
||||
window_name, &morph_operator, max_operator,
|
||||
Morphology_Operations );
|
||||
|
||||
|
||||
|
||||
* The second trackbar **"Element"** returns **morph_elem**, which indicates what kind of structure our kernel is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name,
|
||||
&morph_elem, max_elem,
|
||||
Morphology_Operations );
|
||||
|
||||
* The final trackbar **"Kernel Size"** returns the size of the kernel to be used (**morph_size**)
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
createTrackbar( "Kernel size:\n 2n +1", window_name,
|
||||
&morph_size, max_kernel_size,
|
||||
Morphology_Operations );
|
||||
|
||||
|
||||
* Every time we move any slider, the user's function **Morphology_Operations** will be called to effectuate a new morphology operation and it will update the output image based on the current trackbar values.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/**
|
||||
* @function Morphology_Operations
|
||||
*/
|
||||
void Morphology_Operations( int, void* )
|
||||
{
|
||||
// Since MORPH_X : 2,3,4,5 and 6
|
||||
int operation = morph_operator + 2;
|
||||
|
||||
Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
|
||||
|
||||
/// Apply the specified morphology operation
|
||||
morphologyEx( src, dst, operation, element );
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
|
||||
|
||||
We can observe that the key function to perform the morphology transformations is :morphology_ex:`morphologyEx <>`. In this example we use four arguments (leaving the rest as defaults):
|
||||
|
||||
* **src** : Source (input) image
|
||||
* **dst**: Output image
|
||||
* **operation**: The kind of morphology transformation to be performed. Note that we have 5 alternatives:
|
||||
|
||||
* *Opening*: MORPH_OPEN : 2
|
||||
* *Closing*: MORPH_CLOSE: 3
|
||||
* *Gradient*: MORPH_GRADIENT: 4
|
||||
* *Top Hat*: MORPH_TOPHAT: 5
|
||||
* *Black Hat*: MORPH_BLACKHAT: 6
|
||||
|
||||
As you can see the values range from <2-6>, that is why we add (+2) to the values entered by the Trackbar:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
int operation = morph_operator + 2;
|
||||
|
||||
* **element**: The kernel to be used. We use the function :get_structuring_element:`getStructuringElement <>` to define our own structure.
|
||||
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
* After compiling the code above we can execute it giving an image path as an argument. For this tutorial we use as input the image: **baboon.jpg**:
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Original_Image.jpg
|
||||
:height: 200pt
|
||||
:alt: Morphology 2: Original image
|
||||
:align: center
|
||||
|
||||
* And here are two snapshots of the display window. The first picture shows the output after using the operator **Opening** with a cross kernel. The second picture (right side, shows the result of using a **Blackhat** operator with an ellipse kernel.
|
||||
|
||||
.. image:: images/Morphology_2_Tutorial_Cover.png
|
||||
:height: 300pt
|
||||
:alt: Morphology 2: Result sample
|
||||
:align: center
|
||||
|
Before Width: | Height: | Size: 1.1 MiB |
Before Width: | Height: | Size: 176 KiB |
Before Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 5.6 KiB |
Before Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 3.9 KiB |
@ -1,256 +0,0 @@
|
||||
.. _Pyramids:
|
||||
|
||||
Image Pyramids
|
||||
***************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Use the OpenCV functions :pyr_up:`pyrUp <>` and :pyr_down:`pyrDown <>` to downsample or upsample a given image.
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
* Usually we need to convert an image to a size different than its original. For this, there are two possible options:
|
||||
|
||||
* *Upsize* the image (zoom in) or
|
||||
* *Downsize* it (zoom out).
|
||||
|
||||
* Although there is a *geometric transformation* function in OpenCV that -literally- resize an image (:resize:`resize <>`, which we will show in a future tutorial), in this section we analyze first the use of **Image Pyramids**, which are widely applied in a huge range of vision applications.
|
||||
|
||||
Image Pyramid
|
||||
--------------
|
||||
|
||||
* An image pyramid is a collection of images - all arising from a single original image - that are successively downsampled until some desired stopping point is reached.
|
||||
|
||||
* There are two common kinds of image pyramids:
|
||||
|
||||
* **Gaussian pyramid:** Used to downsample images
|
||||
|
||||
* **Laplacian pyramid:** Used to reconstruct an upsampled image from an image lower in the pyramid (with less resolution)
|
||||
|
||||
* In this tutorial we'll use the *Gaussian pyramid*.
|
||||
|
||||
Gaussian Pyramid
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
||||
* Imagine the pyramid as a set of layers in which the higher the layer, the smaller the size.
|
||||
|
||||
.. image:: images/Pyramids_Tutorial_Pyramid_Theory.png
|
||||
:alt: Pyramid figure
|
||||
:align: center
|
||||
|
||||
* Every layer is numbered from bottom to top, so layer :math:`(i+1)` (denoted as :math:`G_{i+1}` is smaller than layer :math:`i` (:math:`G_{i}`).
|
||||
|
||||
* To produce layer :math:`(i+1)` in the Gaussian pyramid, we do the following:
|
||||
|
||||
* Convolve :math:`G_{i}` with a Gaussian kernel:
|
||||
|
||||
.. math::
|
||||
|
||||
\frac{1}{16} \begin{bmatrix} 1 & 4 & 6 & 4 & 1 \\ 4 & 16 & 24 & 16 & 4 \\ 6 & 24 & 36 & 24 & 6 \\ 4 & 16 & 24 & 16 & 4 \\ 1 & 4 & 6 & 4 & 1 \end{bmatrix}
|
||||
|
||||
* Remove every even-numbered row and column.
|
||||
|
||||
* You can easily notice that the resulting image will be exactly one-quarter the area of its predecessor. Iterating this process on the input image :math:`G_{0}` (original image) produces the entire pyramid.
|
||||
|
||||
* The procedure above was useful to downsample an image. What if we want to make it bigger?:
|
||||
|
||||
* First, upsize the image to twice the original in each dimension, wit the new even rows and columns filled with zeros (:math:`0`)
|
||||
|
||||
* Perform a convolution with the same kernel shown above (multiplied by 4) to approximate the values of the "missing pixels"
|
||||
|
||||
* These two procedures (downsampling and upsampling as explained above) are implemented by the OpenCV functions :pyr_up:`pyrUp <>` and :pyr_down:`pyrDown <>`, as we will see in an example with the code below:
|
||||
|
||||
.. note::
|
||||
When we reduce the size of an image, we are actually *losing* information of the image.
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ImgProc/Pyramids.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
Mat src, dst, tmp;
|
||||
char* window_name = "Pyramids Demo";
|
||||
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// General instructions
|
||||
printf( "\n Zoom In-Out demo \n " );
|
||||
printf( "------------------ \n" );
|
||||
printf( " * [u] -> Zoom in \n" );
|
||||
printf( " * [d] -> Zoom out \n" );
|
||||
printf( " * [ESC] -> Close program \n \n" );
|
||||
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.png" );
|
||||
if( !src.data )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
|
||||
/// Create window
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
imshow( window_name, dst );
|
||||
|
||||
/// Loop
|
||||
while( true )
|
||||
{
|
||||
int c;
|
||||
c = waitKey(10);
|
||||
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
if( (char)c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
else if( (char)c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
|
||||
imshow( window_name, dst );
|
||||
tmp = dst;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
#. Let's check the general structure of the program:
|
||||
|
||||
* Load an image (in this case it is defined in the program, the user does not have to enter it as an argument)
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.png" );
|
||||
if( !src.data )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
|
||||
* Create a Mat object to store the result of the operations (*dst*) and one to save temporal results (*tmp*).
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Mat src, dst, tmp;
|
||||
/* ... */
|
||||
tmp = src;
|
||||
dst = tmp;
|
||||
|
||||
|
||||
|
||||
* Create a window to display the result
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
imshow( window_name, dst );
|
||||
|
||||
* Perform an infinite loop waiting for user input.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
while( true )
|
||||
{
|
||||
int c;
|
||||
c = waitKey(10);
|
||||
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
if( (char)c == 'u' )
|
||||
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
|
||||
printf( "** Zoom In: Image x 2 \n" );
|
||||
}
|
||||
else if( (char)c == 'd' )
|
||||
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
|
||||
printf( "** Zoom Out: Image / 2 \n" );
|
||||
}
|
||||
|
||||
imshow( window_name, dst );
|
||||
tmp = dst;
|
||||
}
|
||||
|
||||
|
||||
Our program exits if the user presses *ESC*. Besides, it has two options:
|
||||
|
||||
* **Perform upsampling (after pressing 'u')**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 )
|
||||
|
||||
We use the function :pyr_up:`pyrUp <>` with 03 arguments:
|
||||
|
||||
* *tmp*: The current image, it is initialized with the *src* original image.
|
||||
* *dst*: The destination image (to be shown on screen, supposedly the double of the input image)
|
||||
* *Size( tmp.cols*2, tmp.rows*2 )* : The destination size. Since we are upsampling, :pyr_up:`pyrUp <>` expects a size double than the input image (in this case *tmp*).
|
||||
|
||||
* **Perform downsampling (after pressing 'd')**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 )
|
||||
|
||||
Similarly as with :pyr_up:`pyrUp <>`, we use the function :pyr_down:`pyrDown <>` with 03 arguments:
|
||||
|
||||
* *tmp*: The current image, it is initialized with the *src* original image.
|
||||
* *dst*: The destination image (to be shown on screen, supposedly half the input image)
|
||||
* *Size( tmp.cols/2, tmp.rows/2 )* : The destination size. Since we are upsampling, :pyr_down:`pyrDown <>` expects half the size the input image (in this case *tmp*).
|
||||
|
||||
* Notice that it is important that the input image can be divided by a factor of two (in both dimensions). Otherwise, an error will be shown.
|
||||
|
||||
* Finally, we update the input image **tmp** with the current image displayed, so the subsequent operations are performed on it.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
tmp = dst;
|
||||
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
* After compiling the code above we can test it. The program calls an image **chicky_512.png** that comes in the *tutorial_code/image* folder. Notice that this image is :math:`512 \times 512`, hence a downsample won't generate any error (:math:`512 = 2^{9}`). The original image is shown below:
|
||||
|
||||
.. image:: images/Pyramids_Tutorial_Original_Image.png
|
||||
:alt: Pyramids: Original image
|
||||
:align: center
|
||||
|
||||
* First we apply two successive :pyr_down:`pyrDown <>` operations by pressing 'd'. Our output is:
|
||||
|
||||
.. image:: images/Pyramids_Tutorial_PyrDown_Result.png
|
||||
:alt: Pyramids: PyrDown Result
|
||||
:align: center
|
||||
|
||||
* Note that we should have lost some resolution due to the fact that we are diminishing the size of the image. This is evident after we apply :pyr_up:`pyrUp <>` twice (by pressing 'u'). Our output is now:
|
||||
|
||||
.. image:: images/Pyramids_Tutorial_PyrUp_Result.png
|
||||
:alt: Pyramids: PyrUp Result
|
||||
:align: center
|
||||
|
||||
|
Before Width: | Height: | Size: 290 KiB |
Before Width: | Height: | Size: 574 KiB |
Before Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 237 KiB |
Before Width: | Height: | Size: 30 KiB |
@ -1,298 +0,0 @@
|
||||
.. _Smoothing:
|
||||
|
||||
Smoothing Images
|
||||
******************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Apply diverse linear filters to smooth images using OpenCV functions such as:
|
||||
|
||||
* :blur:`blur <>`
|
||||
* :gaussian_blur:`GaussianBlur <>`
|
||||
* :median_blur:`medianBlur <>`
|
||||
* :bilateral_filter:`bilateralFilter <>`
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book `Computer Vision: Algorithms and Applications <http://szeliski.org/Book/>`_ by Richard Szeliski and to *LearningOpenCV*
|
||||
|
||||
* *Smoothing*, also called *blurring*, is a simple and frequently used image processing operation.
|
||||
|
||||
* There are many reasons for smoothing. In this tutorial we will focus on smoothing in order to reduce noise (other uses will be seen in the following tutorials).
|
||||
|
||||
* To perform a smoothing operation we will apply a *filter* to our image. The most common type of filters are *linear*, in which an output pixel's value (i.e. :math:`g(i,j)`) is determined as a weighted sum of input pixel values (i.e. :math:`f(i+k,j+l)`) :
|
||||
|
||||
.. math::
|
||||
g(i,j) = \sum_{k,l} f(i+k, j+l) h(k,l)
|
||||
|
||||
:math:`h(k,l)` is called the *kernel*, which is nothing more than the coefficients of the filter.
|
||||
|
||||
|
||||
It helps to visualize a *filter* as a window of coefficients sliding across the image.
|
||||
|
||||
* There are many kind of filters, here we will mention the most used:
|
||||
|
||||
Normalized Box Filter
|
||||
-----------------------
|
||||
|
||||
* This filter is the simplest of all! Each output pixel is the *mean* of its kernel neighbors ( all of them contribute with equal weights)
|
||||
|
||||
* Just in case, the kernel is below:
|
||||
|
||||
.. math::
|
||||
|
||||
K = \dfrac{1}{K_{width} \cdot K_{height}} \begin{bmatrix}
|
||||
1 & 1 & 1 & ... & 1 \\
|
||||
1 & 1 & 1 & ... & 1 \\
|
||||
. & . & . & ... & 1 \\
|
||||
. & . & . & ... & 1 \\
|
||||
1 & 1 & 1 & ... & 1
|
||||
\end{bmatrix}
|
||||
|
||||
|
||||
Gaussian Filter
|
||||
---------------
|
||||
* Probably the most useful filter (although not the fastest). Gaussian filtering is done by convolving each point in the input array with a *Gaussian kernel* and then summing them all to produce the output array.
|
||||
|
||||
* Just to make the picture clearer, remember how a 1D Gaussian kernel look like?
|
||||
|
||||
.. image:: images/Smoothing_Tutorial_theory_gaussian_0.jpg
|
||||
:height: 200px
|
||||
:align: center
|
||||
|
||||
Assuming that an image is 1D, you can notice that the pixel located in the middle would have the biggest weight. The weight of its neighbors decreases as the spatial distance between them and the center pixel increases.
|
||||
|
||||
.. note::
|
||||
|
||||
* Remember that a 2D Gaussian can be represented as :
|
||||
|
||||
.. math::
|
||||
|
||||
G_{0}(x, y) = A e^{ \dfrac{ -(x - \mu_{x})^{2} }{ 2\sigma^{2}_{x} } + \dfrac{ -(y - \mu_{y})^{2} }{ 2\sigma^{2}_{y} } }
|
||||
|
||||
where :math:`\mu` is the mean (the peak) and :math:`\sigma` represents the variance (per each of the variables :math:`x` and :math:`y`)
|
||||
|
||||
|
||||
|
||||
Median Filter
|
||||
--------------
|
||||
|
||||
The median filter run through each element of the signal (in this case the image) and replace each pixel with the **median** of its neighboring pixels (located in a square neighborhood around the evaluated pixel).
|
||||
|
||||
|
||||
Bilateral Filter
|
||||
-----------------
|
||||
|
||||
* So far, we have explained some filters which main goal is to *smooth* an input image. However, sometimes the filters do not only dissolve the noise, but also smooth away the *edges*. To avoid this (at certain extent at least), we can use a bilateral filter.
|
||||
|
||||
* In an analogous way as the Gaussian filter, the bilateral filter also considers the neighboring pixels with weights assigned to each of them. These weights have two components, the first of which is the same weighting used by the Gaussian filter. The second component takes into account the difference in intensity between the neighboring pixels and the evaluated one.
|
||||
|
||||
* For a more detailed explanation you can check `this link <http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html>`_
|
||||
|
||||
|
||||
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
This tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ImgProc/Smoothing.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
/// Global Variables
|
||||
int DELAY_CAPTION = 1500;
|
||||
int DELAY_BLUR = 100;
|
||||
int MAX_KERNEL_LENGTH = 31;
|
||||
|
||||
Mat src; Mat dst;
|
||||
char window_name[] = "Filter Demo 1";
|
||||
|
||||
/// Function headers
|
||||
int display_caption( char* caption );
|
||||
int display_dst( int delay );
|
||||
|
||||
/**
|
||||
* function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
|
||||
/// Load the source image
|
||||
src = imread( "../images/lena.png", 1 );
|
||||
|
||||
if( display_caption( "Original Image" ) != 0 ) { return 0; }
|
||||
|
||||
dst = src.clone();
|
||||
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
/// Applying Median blur
|
||||
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
/// Wait until user press a key
|
||||
display_caption( "End: Press a key!" );
|
||||
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int display_caption( char* caption )
|
||||
{
|
||||
dst = Mat::zeros( src.size(), src.type() );
|
||||
putText( dst, caption,
|
||||
Point( src.cols/4, src.rows/2),
|
||||
CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
|
||||
|
||||
imshow( window_name, dst );
|
||||
int c = waitKey( DELAY_CAPTION );
|
||||
if( c >= 0 ) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int display_dst( int delay )
|
||||
{
|
||||
imshow( window_name, dst );
|
||||
int c = waitKey ( delay );
|
||||
if( c >= 0 ) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
#. Let's check the OpenCV functions that involve only the smoothing procedure, since the rest is already known by now.
|
||||
|
||||
#. **Normalized Block Filter:**
|
||||
|
||||
OpenCV offers the function :blur:`blur <>` to perform smoothing with this filter.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
|
||||
We specify 4 arguments (more details, check the Reference):
|
||||
|
||||
* *src*: Source image
|
||||
|
||||
* *dst*: Destination image
|
||||
|
||||
* *Size( w,h )*: Defines the size of the kernel to be used ( of width *w* pixels and height *h* pixels)
|
||||
|
||||
* *Point(-1, -1)*: Indicates where the anchor point (the pixel evaluated) is located with respect to the neighborhood. If there is a negative value, then the center of the kernel is considered the anchor point.
|
||||
|
||||
#. **Gaussian Filter:**
|
||||
|
||||
It is performed by the function :gaussian_blur:`GaussianBlur <>` :
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
Here we use 4 arguments (more details, check the OpenCV reference):
|
||||
|
||||
* *src*: Source image
|
||||
|
||||
* *dst*: Destination image
|
||||
|
||||
* *Size(w, h)*: The size of the kernel to be used (the neighbors to be considered). :math:`w` and :math:`h` have to be odd and positive numbers otherwise thi size will be calculated using the :math:`\sigma_{x}` and :math:`\sigma_{y}` arguments.
|
||||
|
||||
* :math:`\sigma_{x}`: The standard deviation in x. Writing :math:`0` implies that :math:`\sigma_{x}` is calculated using kernel size.
|
||||
|
||||
* :math:`\sigma_{y}`: The standard deviation in y. Writing :math:`0` implies that :math:`\sigma_{y}` is calculated using kernel size.
|
||||
|
||||
|
||||
#. **Median Filter:**
|
||||
|
||||
This filter is provided by the :median_blur:`medianBlur <>` function:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
We use three arguments:
|
||||
|
||||
* *src*: Source image
|
||||
|
||||
* *dst*: Destination image, must be the same type as *src*
|
||||
|
||||
* *i*: Size of the kernel (only one because we use a square window). Must be odd.
|
||||
|
||||
|
||||
#. **Bilateral Filter**
|
||||
|
||||
Provided by OpenCV function :bilateral_filter:`bilateralFilter <>`
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
|
||||
We use 5 arguments:
|
||||
|
||||
* *src*: Source image
|
||||
|
||||
* *dst*: Destination image
|
||||
|
||||
* *d*: The diameter of each pixel neighborhood.
|
||||
|
||||
* :math:`\sigma_{Color}`: Standard deviation in the color space.
|
||||
|
||||
* :math:`\sigma_{Space}`: Standard deviation in the coordinate space (in pixel terms)
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
* The code opens an image (in this case *lena.png*) and display it under the effects of the 4 filters explained.
|
||||
|
||||
* Here is a snapshot of the image smoothed using *medianBlur*:
|
||||
|
||||
.. image:: images/Smoothing_Tutorial_Result_Median_Filter.png
|
||||
:alt: Smoothing with a median filter
|
||||
:align: center
|
Before Width: | Height: | Size: 755 KiB |
Before Width: | Height: | Size: 205 KiB |
Before Width: | Height: | Size: 4.4 KiB |
@ -1,320 +0,0 @@
|
||||
.. _Basic_Threshold:
|
||||
|
||||
Basic Thresholding Operations
|
||||
*******************************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Perform basic thresholding operations using OpenCV function :threshold:`threshold <>`
|
||||
|
||||
|
||||
Cool Theory
|
||||
============
|
||||
|
||||
.. note::
|
||||
The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
|
||||
|
||||
What is Thresholding?
|
||||
-----------------------
|
||||
|
||||
* The simplest segmentation method
|
||||
|
||||
* Application example: Separate out regions of an image corresponding to objects which we want to analyze. This separation is based on the variation of intensity between the object pixels and the background pixels.
|
||||
|
||||
* To differentiate the pixels we are interested in from the rest (which will eventually be rejected), we perform a comparison of each pixel intensity value with respect to a *threshold* (determined according to the problem to solve).
|
||||
|
||||
* Once we have separated properly the important pixels, we can set them with a determined value to identify them (i.e. we can assign them a value of :math:`0` (black), :math:`255` (white) or any value that suits your needs).
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Example.png
|
||||
:alt: Threshold simple example
|
||||
:height: 150pt
|
||||
:align: center
|
||||
|
||||
Types of Thresholding
|
||||
-----------------------
|
||||
|
||||
* OpenCV offers the function :threshold:`threshold <>` to perform thresholding operations.
|
||||
|
||||
* We can effectuate :math:`5` types of Thresholding operations with this function. We will explain them in the following subsections.
|
||||
|
||||
* To illustrate how these thresholding processes work, let's consider that we have a source image with pixels with intensity values :math:`src(x,y)`. The plot below depicts this. The horizontal blue line represents the threshold :math:`thresh` (fixed).
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Base_Figure.png
|
||||
:alt: Threshold Binary
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
Threshold Binary
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
||||
* This thresholding operation can be expressed as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{dst} (x,y) = \fork{\texttt{maxVal}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
|
||||
|
||||
* So, if the intensity of the pixel :math:`src(x,y)` is higher than :math:`thresh`, then the new pixel intensity is set to a :math:`MaxVal`. Otherwise, the pixels are set to :math:`0`.
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Binary.png
|
||||
:alt: Threshold Binary
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
|
||||
Threshold Binary, Inverted
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
* This thresholding operation can be expressed as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{dst} (x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{maxVal}}{otherwise}
|
||||
|
||||
* If the intensity of the pixel :math:`src(x,y)` is higher than :math:`thresh`, then the new pixel intensity is set to a :math:`0`. Otherwise, it is set to :math:`MaxVal`.
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Binary_Inverted.png
|
||||
:alt: Threshold Binary Inverted
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
Truncate
|
||||
^^^^^^^^^
|
||||
|
||||
* This thresholding operation can be expressed as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
|
||||
|
||||
* The maximum intensity value for the pixels is :math:`thresh`, if :math:`src(x,y)` is greater, then its value is *truncated*. See figure below:
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Truncate.png
|
||||
:alt: Threshold Truncate
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
|
||||
|
||||
Threshold to Zero
|
||||
^^^^^^^^^^^^^^^^^^
|
||||
|
||||
* This operation can be expressed as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
|
||||
|
||||
* If :math:`src(x,y)` is lower than :math:`thresh`, the new pixel value will be set to :math:`0`.
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Zero.png
|
||||
:alt: Threshold Zero
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
|
||||
Threshold to Zero, Inverted
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
* This operation can be expressed as:
|
||||
|
||||
.. math::
|
||||
|
||||
\texttt{dst} (x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
|
||||
|
||||
* If :math:`src(x,y)` is greater than :math:`thresh`, the new pixel value will be set to :math:`0`.
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Theory_Zero_Inverted.png
|
||||
:alt: Threshold Zero Inverted
|
||||
:height: 100pt
|
||||
:align: center
|
||||
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
The tutorial code's is shown lines below. You can also download it from `here <https://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/tutorial_code/ImgProc/Threshold.cpp>`_
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/// Global variables
|
||||
|
||||
int threshold_value = 0;
|
||||
int threshold_type = 3;;
|
||||
int const max_value = 255;
|
||||
int const max_type = 4;
|
||||
int const max_BINARY_value = 255;
|
||||
|
||||
Mat src, src_gray, dst;
|
||||
char* window_name = "Threshold Demo";
|
||||
|
||||
char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
|
||||
char* trackbar_value = "Value";
|
||||
|
||||
/// Function headers
|
||||
void Threshold_Demo( int, void* );
|
||||
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert the image to Gray
|
||||
cvtColor( src, src_gray, CV_RGB2GRAY );
|
||||
|
||||
/// Create a window to display results
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
|
||||
/// Create Trackbar to choose type of Threshold
|
||||
createTrackbar( trackbar_type,
|
||||
window_name, &threshold_type,
|
||||
max_type, Threshold_Demo );
|
||||
|
||||
createTrackbar( trackbar_value,
|
||||
window_name, &threshold_value,
|
||||
max_value, Threshold_Demo );
|
||||
|
||||
/// Call the function to initialize
|
||||
Threshold_Demo( 0, 0 );
|
||||
|
||||
/// Wait until user finishes program
|
||||
while(true)
|
||||
{
|
||||
int c;
|
||||
c = waitKey( 20 );
|
||||
if( (char)c == 27 )
|
||||
{ break; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @function Threshold_Demo
|
||||
*/
|
||||
void Threshold_Demo( int, void* )
|
||||
{
|
||||
/* 0: Binary
|
||||
1: Binary Inverted
|
||||
2: Threshold Truncated
|
||||
3: Threshold to Zero
|
||||
4: Threshold to Zero Inverted
|
||||
*/
|
||||
|
||||
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
|
||||
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Explanation
|
||||
=============
|
||||
|
||||
|
||||
#. Let's check the general structure of the program:
|
||||
|
||||
* Load an image. If it is RGB we convert it to Grayscale. For this, remember that we can use the function :cvt_color:`cvtColor <>`:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
/// Convert the image to Gray
|
||||
cvtColor( src, src_gray, CV_RGB2GRAY );
|
||||
|
||||
|
||||
* Create a window to display the result
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
|
||||
|
||||
* Create :math:`2` trackbars for the user to enter user input:
|
||||
|
||||
* **Type of thresholding**: Binary, To Zero, etc...
|
||||
* **Threshold value**
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
createTrackbar( trackbar_type,
|
||||
window_name, &threshold_type,
|
||||
max_type, Threshold_Demo );
|
||||
|
||||
createTrackbar( trackbar_value,
|
||||
window_name, &threshold_value,
|
||||
max_value, Threshold_Demo );
|
||||
|
||||
* Wait until the user enters the threshold value, the type of thresholding (or until the program exits)
|
||||
|
||||
* Whenever the user changes the value of any of the Trackbars, the function *Threshold_Demo* is called:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
/**
|
||||
* @function Threshold_Demo
|
||||
*/
|
||||
void Threshold_Demo( int, void* )
|
||||
{
|
||||
/* 0: Binary
|
||||
1: Binary Inverted
|
||||
2: Threshold Truncated
|
||||
3: Threshold to Zero
|
||||
4: Threshold to Zero Inverted
|
||||
*/
|
||||
|
||||
threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
|
||||
|
||||
imshow( window_name, dst );
|
||||
}
|
||||
|
||||
As you can see, the function :threshold:`threshold <>` is invoked. We give :math:`5` parameters:
|
||||
|
||||
* *src_gray*: Our input image
|
||||
* *dst*: Destination (output) image
|
||||
* *threshold_value*: The :math:`thresh` value with respect to which the thresholding operation is made
|
||||
* *max_BINARY_value*: The value used with the Binary thresholding operations (to set the chosen pixels)
|
||||
* *threshold_type*: One of the :math:`5` thresholding operations. They are listed in the comment section of the function above.
|
||||
|
||||
|
||||
|
||||
Results
|
||||
========
|
||||
|
||||
#. After compiling this program, run it giving a path to an image as argument. For instance, for an input image as:
|
||||
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Original_Image.png
|
||||
:alt: Threshold Original Image
|
||||
:height: 200pt
|
||||
:align: center
|
||||
|
||||
#. First, we try to threshold our image with a *binary threhold inverted*. We expect that the pixels brighter than the :math:`thresh` will turn dark, which is what actually happens, as we can see in the snapshot below (notice from the original image, that the doggie's tongue and eyes are particularly bright in comparison with the image, this is reflected in the output image).
|
||||
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Result_Binary_Inverted.png
|
||||
:alt: Threshold Result Binary Inverted
|
||||
:height: 200pt
|
||||
:align: center
|
||||
|
||||
|
||||
#. Now we try with the *threshold to zero*. With this, we expect that the darkest pixels (below the threshold) will become completely black, whereas the pixels with value greater than the threshold will keep its original value. This is verified by the following snapshot of the output image:
|
||||
|
||||
.. image:: images/Threshold_Tutorial_Result_Zero.png
|
||||
:alt: Threshold Result Zero
|
||||
:height: 200pt
|
||||
:align: center
|
||||
|
||||
|
Before Width: | Height: | Size: 281 KiB |
Before Width: | Height: | Size: 574 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 160 KiB |
Before Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 785 B |
Before Width: | Height: | Size: 837 B |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.5 KiB |
@ -1,112 +0,0 @@
|
||||
.. _Display_Image:
|
||||
|
||||
Display an Image
|
||||
*****************
|
||||
|
||||
Goal
|
||||
=====
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Load an image using :imread:`imread <>`
|
||||
* Create a named window (using :named_window:`namedWindow <>`)
|
||||
* Display an image in an OpenCV window (using :imshow:`imshow <>`)
|
||||
|
||||
Code
|
||||
=====
|
||||
|
||||
Here it is:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat image;
|
||||
image = imread( argv[1], 1 );
|
||||
|
||||
if( argc != 2 || !image.data )
|
||||
{
|
||||
printf( "No image data \n" );
|
||||
return -1;
|
||||
}
|
||||
|
||||
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
|
||||
imshow( "Display Image", image );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
#. .. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
These are OpenCV headers:
|
||||
|
||||
* *cv.h* : Main OpenCV functions
|
||||
* *highgui.h* : Graphical User Interface (GUI) functions
|
||||
|
||||
Now, let's analyze the *main* function:
|
||||
|
||||
#. .. code-block:: cpp
|
||||
|
||||
Mat image;
|
||||
|
||||
We create a Mat object to store the data of the image to load.
|
||||
|
||||
#. .. code-block:: cpp
|
||||
|
||||
image = imread( argv[1], 1 );
|
||||
|
||||
Here, we called the function :imread:`imread <>` which basically loads the image specified by the first argument (in this case *argv[1]*). The second argument is by default.
|
||||
|
||||
#. After checking that the image data was loaded correctly, we want to display our image, so we create a window:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
|
||||
|
||||
|
||||
:named_window:`namedWindow <>` receives as arguments the window name ("Display Image") and an additional argument that defines windows properties. In this case **CV_WINDOW_AUTOSIZE** indicates that the window will adopt the size of the image to be displayed.
|
||||
|
||||
#. Finally, it is time to show the image, for this we use :imshow:`imshow <>`
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
imshow( "Display Image", image )
|
||||
|
||||
#. Finally, we want our window to be displayed until the user presses a key (otherwise the program would end far too quickly):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
waitKey(0);
|
||||
|
||||
We use the :wait_key:`waitKey <>` function, which allow us to wait for a keystroke during a number of milliseconds (determined by the argument). If the argument is zero, then it will wait indefinitely.
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
* Compile your code and then run the executable giving a image path as argument:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
./DisplayImage HappyFish.jpg
|
||||
|
||||
* You should get a nice window as the one shown below:
|
||||
|
||||
.. image:: images/Display_Image_Tutorial_Result.png
|
||||
:alt: Display Image Tutorial - Final Result
|
||||
:align: center
|
Before Width: | Height: | Size: 62 KiB |
@ -1,243 +0,0 @@
|
||||
.. _Linux_Eclipse_Usage:
|
||||
|
||||
Using OpenCV with Eclipse (plugin CDT)
|
||||
****************************************
|
||||
|
||||
.. note::
|
||||
For me at least, this works, is simple and quick. Suggestions are welcome
|
||||
|
||||
Prerequisites
|
||||
===============
|
||||
|
||||
#. Having installed `Eclipse <http://www.eclipse.org/>`_ in your workstation (only the CDT plugin for C/C++ is needed). You can follow the following steps:
|
||||
|
||||
* Go to the Eclipse site
|
||||
|
||||
* Download `Eclipse IDE for C/C++ Developers <http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2>`_ . Choose the link according to your workstation.
|
||||
|
||||
#. Having installed OpenCV. If not yet, go :ref:`here <Linux_Installation>`
|
||||
|
||||
Making a project
|
||||
=================
|
||||
|
||||
#. Start Eclipse. Just run the executable that comes in the folder.
|
||||
|
||||
#. Go to **File -> New -> C/C++ Project**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-0.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 0
|
||||
:align: center
|
||||
|
||||
#. Choose a name for your project (i.e. DisplayImage). An **Empty Project** should be okay for this example.
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-1.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 1
|
||||
:align: center
|
||||
|
||||
#. Leave everything else by default. Press **Finish**.
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-2.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 2
|
||||
:align: center
|
||||
|
||||
#. Your project (in this case DisplayImage) should appear in the **Project Navigator** (usually at the left side of your window).
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-3.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 3
|
||||
:align: center
|
||||
|
||||
|
||||
#. Now, let's add a source file using OpenCV:
|
||||
|
||||
* Right click on **DisplayImage** (in the Navigator). **New -> Folder** .
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-4.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 4
|
||||
:align: center
|
||||
|
||||
* Name your folder **src** and then hit **Finish**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-5.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 5
|
||||
:align: center
|
||||
|
||||
* Right click on your newly created **src** folder. Choose **New source file**:
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-6.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 6
|
||||
:align: center
|
||||
|
||||
* Call it **DisplayImage.cpp**. Hit **Finish**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-7.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 7
|
||||
:align: center
|
||||
|
||||
#. So, now you have a project with a empty .cpp file. Let's fill it with some sample code (in other words, copy and paste the snippet below):
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat image;
|
||||
image = imread( argv[1], 1 );
|
||||
|
||||
if( argc != 2 || !image.data )
|
||||
{
|
||||
printf( "No image data \n" );
|
||||
return -1;
|
||||
}
|
||||
|
||||
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
|
||||
imshow( "Display Image", image );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#. We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are. For this, do the following:
|
||||
|
||||
* Go to **Project-->Properties**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-8.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 8
|
||||
:align: center
|
||||
|
||||
* In **C/C++ Build**, click on **Settings**. At the right, choose the **Tool Settings** Tab. Here we will enter the headers and libraries info:
|
||||
|
||||
a. In **GCC C++ Compiler**, go to **Includes**. In **Include paths(-l)** you should include the path of the folder where opencv was installed. In our example, this is:
|
||||
::
|
||||
|
||||
/usr/local/include/opencv
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-9.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 9
|
||||
:align: center
|
||||
|
||||
.. note::
|
||||
If you do not know where your opencv files are, open the **Terminal** and type:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
pkg-config --cflags opencv
|
||||
|
||||
For instance, that command gave me this output:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
-I/usr/local/include/opencv -I/usr/local/include
|
||||
|
||||
|
||||
b. Now go to **GCC C++ Linker**,there you have to fill two spaces:
|
||||
|
||||
* In **Library search path (-L)** you have to write the path to where the opencv libraries reside, in my case the path is:
|
||||
::
|
||||
|
||||
/usr/local/lib
|
||||
|
||||
* In **Libraries(-l)** add the OpenCV libraries that you may need. Usually just the 3 first on the list below are enough (for simple applications) . In my case, I am putting all of them since I plan to use the whole bunch:
|
||||
|
||||
|
||||
* opencv_core
|
||||
* opencv_imgproc
|
||||
* opencv_highgui
|
||||
* opencv_ml
|
||||
* opencv_video
|
||||
* opencv_features2d
|
||||
* opencv_calib3d
|
||||
* opencv_objdetect
|
||||
* opencv_contrib
|
||||
* opencv_legacy
|
||||
* opencv_flann
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-10.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 10
|
||||
:align: center
|
||||
|
||||
.. note::
|
||||
|
||||
If you don't know where your libraries are (or you are just psychotic and want to make sure the path is fine), type in **Terminal**:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
pkg-config --libs opencv
|
||||
|
||||
My output (in case you want to check) was:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
|
||||
|
||||
Now you are done. Click **OK**
|
||||
|
||||
* Your project should be ready to be built. For this, go to **Project->Build all**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-11.png
|
||||
:height: 400px
|
||||
:alt: Eclipse Tutorial Screenshot 11
|
||||
:align: center
|
||||
|
||||
In the Console you should get something like
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-12.png
|
||||
:height: 200px
|
||||
:alt: Eclipse Tutorial Screenshot 12
|
||||
:align: center
|
||||
|
||||
If you check in your folder, there should be an executable there.
|
||||
|
||||
Running the executable
|
||||
========================
|
||||
|
||||
So, now we have an executable ready to run. If we were to use the Terminal, we would probably do something like:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd <DisplayImage_directory>
|
||||
cd src
|
||||
./DisplayImage ../images/HappyLittleFish.jpg
|
||||
|
||||
Assuming that the image to use as the argument would be located in <DisplayImage_directory>/images/HappyLittleFish.jpg. We can still do this, but let's do it from Eclipse:
|
||||
|
||||
|
||||
#. Go to **Run->Run Configurations**
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-13.png
|
||||
:height: 300px
|
||||
:alt: Eclipse Tutorial Screenshot 13
|
||||
:align: center
|
||||
|
||||
#. Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Application a couple of times). Select the name (in this case **DisplayImage Debug**).
|
||||
|
||||
#. Now, in the right side of the window, choose the **Arguments** Tab. Write the path of the image file we want to open (path relative to the workspace/DisplayImage folder). Let's use **HappyLittleFish.jpg**:
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-14.png
|
||||
:height: 300px
|
||||
:alt: Eclipse Tutorial Screenshot 14
|
||||
:align: center
|
||||
|
||||
#. Click on the **Apply** button and then in Run. An OpenCV window should pop up with the fish image (or whatever you used).
|
||||
|
||||
.. image:: images/Eclipse_Tutorial_Screenshot-15.png
|
||||
:alt: Eclipse Tutorial Screenshot 15
|
||||
:align: center
|
||||
|
||||
|
||||
#. Congratulations! You are ready to have fun with OpenCV using Eclipse.
|
Before Width: | Height: | Size: 104 KiB |
Before Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 126 KiB |
Before Width: | Height: | Size: 105 KiB |
Before Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 93 KiB |
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 101 KiB |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 105 KiB |
Before Width: | Height: | Size: 110 KiB |
Before Width: | Height: | Size: 4.1 KiB |
@ -1,85 +0,0 @@
|
||||
.. _Linux_GCC_Usage:
|
||||
|
||||
Using OpenCV with gcc and CMake
|
||||
*********************************
|
||||
|
||||
.. note::
|
||||
We assume that you have successfully installed OpenCV in your workstation.
|
||||
|
||||
The easiest way of using OpenCV in your code is to use `CMake <http://www.cmake.org/>`_. A few advantages (taken from the Wiki):
|
||||
|
||||
* No need to change anything when porting between Linux and Windows
|
||||
* Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK )
|
||||
|
||||
If you are not familiar with CMake, checkout the `tutorial <http://www.cmake.org/cmake/help/cmake_tutorial.html>`_ on its website.
|
||||
|
||||
Steps
|
||||
======
|
||||
|
||||
Create a program using OpenCV
|
||||
-------------------------------
|
||||
|
||||
Let's use a simple program such as DisplayImage.cpp shown below.
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
Mat image;
|
||||
image = imread( argv[1], 1 );
|
||||
|
||||
if( argc != 2 || !image.data )
|
||||
{
|
||||
printf( "No image data \n" );
|
||||
return -1;
|
||||
}
|
||||
|
||||
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
|
||||
imshow( "Display Image", image );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Create a CMake file
|
||||
---------------------
|
||||
Now you have to create your CMakeLists.txt file. It should look like this:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project( DisplayImage )
|
||||
find_package( OpenCV REQUIRED )
|
||||
add_executable( DisplayImage DisplayImage )
|
||||
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
|
||||
|
||||
Generate the executable
|
||||
-------------------------
|
||||
This part is easy, just proceed as with any other project using CMake:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd <DisplayImage_directory>
|
||||
cmake .
|
||||
make
|
||||
|
||||
Result
|
||||
--------
|
||||
By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
./DisplayImage lena.jpg
|
||||
|
||||
You should get a nice window as the one shown below:
|
||||
|
||||
.. image:: images/GCC_CMake_Example_Tutorial.png
|
||||
:alt: Display Image - Lena
|
||||
:align: center
|
||||
|
Before Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 11 KiB |
@ -1,85 +0,0 @@
|
||||
.. _Linux_Installation:
|
||||
|
||||
Installation in Linux
|
||||
***********************
|
||||
These steps have been tested for Ubuntu 10.04 but should work with other distros.
|
||||
|
||||
Required packages
|
||||
==================
|
||||
|
||||
* GCC 4.x or later. This can be installed with
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
sudo apt-get install build-essential
|
||||
|
||||
* CMake 2.6 or higher
|
||||
* Subversion (SVN) client
|
||||
* GTK+2.x or higher, including headers
|
||||
* pkgconfig
|
||||
* libpng, zlib, libjpeg, libtiff, libjasper with development files (e.g. libpjeg-dev)
|
||||
* Python 2.3 or later with developer packages (e.g. python-dev)
|
||||
* SWIG 1.3.30 or later
|
||||
* libavcodec
|
||||
* libdc1394 2.x
|
||||
|
||||
All the libraries above can be installed via Terminal or by using Synaptic Manager
|
||||
|
||||
Getting OpenCV source code
|
||||
============================
|
||||
|
||||
You can use the latest stable OpenCV version available in *sourceforge* or you can grab the latest snapshot from the SVN repository:
|
||||
|
||||
Getting the latest stable OpenCV version
|
||||
------------------------------------------
|
||||
|
||||
* Go to http://sourceforge.net/projects/opencvlibrary
|
||||
|
||||
* Download the source tarball and unpack it
|
||||
|
||||
|
||||
Getting the cutting-edge OpenCV from SourceForge SVN repository
|
||||
-----------------------------------------------------------------
|
||||
|
||||
Launch SVN client and checkout either
|
||||
|
||||
a. the current OpenCV snapshot from here: https://code.ros.org/svn/opencv/trunk
|
||||
|
||||
#. or the latest tested OpenCV snapshot from here: http://code.ros.org/svn/opencv/tags/latest_tested_snapshot
|
||||
|
||||
In Ubuntu it can be done using the following command, e.g.:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd ~/<my_working _directory>
|
||||
svn co https://code.ros.org/svn/opencv/trunk
|
||||
|
||||
|
||||
Building OpenCV from source using CMake, using the command line
|
||||
================================================================
|
||||
|
||||
#. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object filees and output binaries
|
||||
|
||||
#. Enter the <cmake_binary_dir> and type
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cmake [<some optional parameters>] <path to the OpenCV source directory>
|
||||
|
||||
For example
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cd ~/opencv
|
||||
mkdir release
|
||||
cd release
|
||||
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX= /usr/local
|
||||
|
||||
#. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
make
|
||||
sudo make install
|
||||
|
||||
|
Before Width: | Height: | Size: 5.6 KiB |
@ -1,122 +0,0 @@
|
||||
.. _Load_Save_Image:
|
||||
|
||||
Load and Save an Image
|
||||
***********************
|
||||
|
||||
.. note::
|
||||
|
||||
We assume that by now you know:
|
||||
|
||||
* Load an image using :imread:`imread <>`
|
||||
* Display an image in an OpenCV window (using :imshow:`imshow <>`)
|
||||
|
||||
Goals
|
||||
======
|
||||
|
||||
In this tutorial you will learn how to:
|
||||
|
||||
* Transform an image from RGB to Grayscale format by using :cvt_color:`cvtColor <>`
|
||||
* Save your transformed image in a file on disk (using :imwrite:`imwrite <>`)
|
||||
|
||||
Code
|
||||
======
|
||||
|
||||
Here it is:
|
||||
|
||||
.. code-block:: cpp
|
||||
:linenos:
|
||||
|
||||
#include <cv.h>
|
||||
#include <highgui.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* imageName = argv[1];
|
||||
|
||||
Mat image;
|
||||
image = imread( imageName, 1 );
|
||||
|
||||
if( argc != 2 || !image.data )
|
||||
{
|
||||
printf( " No image data \n " );
|
||||
return -1;
|
||||
}
|
||||
|
||||
Mat gray_image;
|
||||
cvtColor( image, gray_image, CV_RGB2GRAY );
|
||||
|
||||
imwrite( "../../images/Gray_Image.png", gray_image );
|
||||
|
||||
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
|
||||
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
|
||||
|
||||
imshow( imageName, image );
|
||||
imshow( "Gray image", gray_image );
|
||||
|
||||
waitKey(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Explanation
|
||||
============
|
||||
|
||||
#. We begin by:
|
||||
|
||||
* Creating a Mat object to store the image information
|
||||
* Load an image using :imread:`imread <>`, located in the path given by *imageName*. Fort this example, assume you are loading a RGB image.
|
||||
|
||||
#. Now we are going to convert our image from RGB to Grayscale format. OpenCV has a really nice function to do this kind of transformations:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
cvtColor( image, gray_image, CV_RGB2GRAY );
|
||||
|
||||
As you can see, :cvt_color:`cvtColor <>` takes as arguments:
|
||||
|
||||
* a source image (*image*)
|
||||
* a destination image (*gray_image*), in which we will save the converted image.
|
||||
|
||||
And an additional parameter that indicates what kind of transformation will be performed. In this case we use **CV_RGB2GRAY** (self-explanatory).
|
||||
|
||||
#. So now we have our new *gray_image* and want to save it on disk (otherwise it will get lost after the program ends). To save it, we will use a function analagous to :imread:`imread <>`: :imwrite:`imwrite <>`
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
imwrite( "../../images/Gray_Image.png", gray_image );
|
||||
|
||||
Which will save our *gray_image* as *Gray_Image.png* in the folder *images* located two levels up of my current location.
|
||||
|
||||
#. Finally, let's check out the images. We create 02 windows and use them to show the original image as well as the new one:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
|
||||
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
|
||||
|
||||
imshow( imageName, image );
|
||||
imshow( "Gray image", gray_image );
|
||||
|
||||
#. Add the usual *waitKey(0)* for the program to wait forever until the user presses a key.
|
||||
|
||||
|
||||
Result
|
||||
=======
|
||||
|
||||
When you run your program you should get something like this:
|
||||
|
||||
.. image:: images/Load_Save_Image_Result_1.png
|
||||
:alt: Load Save Image Result 1
|
||||
:height: 400px
|
||||
:align: center
|
||||
|
||||
And if you check in your folder (in my case *images*), you should have a newly .png file named *Gray_Image.png*:
|
||||
|
||||
.. image:: images/Load_Save_Image_Result_2.png
|
||||
:alt: Load Save Image Result 2
|
||||
:height: 250px
|
||||
:align: center
|
||||
|
||||
Congratulations, you are done with this tutorial!
|
Before Width: | Height: | Size: 466 KiB |
Before Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 265 KiB |
Before Width: | Height: | Size: 137 KiB |
@ -1,5 +0,0 @@
|
||||
.. _Windows_Installation:
|
||||
|
||||
Installation in Windows
|
||||
***********************
|
||||
For now this is just a stub article. It will be updated with valuable content as soon as possible. Make sure to check back for it!
|
Before Width: | Height: | Size: 12 KiB |
@ -1,64 +0,0 @@
|
||||
#######
|
||||
Calib3D
|
||||
#######
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
Camera calibration
|
||||
==================
|
||||
|
||||
The goal of this tutorial is to learn how to calibrate a camera given a set of chessboard images.
|
||||
|
||||
*Test data*: use images in your data/chess folder.
|
||||
|
||||
#.
|
||||
Compile opencv with samples by setting ``BUILD_EXAMPLES`` to ``ON`` in cmake configuration.
|
||||
|
||||
#.
|
||||
Go to ``bin`` folder and use ``imagelist_creator`` to create an ``XML/YAML`` list of your images.
|
||||
|
||||
#.
|
||||
Then, run ``calibration`` sample to get camera parameters. Use square size equal to 3cm.
|
||||
|
||||
Pose estimation
|
||||
===============
|
||||
|
||||
Now, let us write a code that detects a chessboard in a new image and finds its distance from the camera. You can apply the same method to any object with known 3D geometry that you can detect in an image.
|
||||
|
||||
*Test data*: use chess_test*.jpg images from your data folder.
|
||||
|
||||
#.
|
||||
Create an empty console project. Load a test image: ::
|
||||
|
||||
Mat img = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
|
||||
|
||||
#.
|
||||
Detect a chessboard in this image using findChessboard function. ::
|
||||
|
||||
bool found = findChessboardCorners( img, boardSize, ptvec, CV_CALIB_CB_ADAPTIVE_THRESH );
|
||||
|
||||
#.
|
||||
Now, write a function that generates a ``vector<Point3f>`` array of 3d coordinates of a chessboard in any coordinate system. For simplicity, let us choose a system such that one of the chessboard corners is in the origin and the board is in the plane *z = 0*.
|
||||
|
||||
#.
|
||||
Read camera parameters from XML/YAML file: ::
|
||||
|
||||
FileStorage fs(filename, FileStorage::READ);
|
||||
Mat intrinsics, distortion;
|
||||
fs["camera_matrix"] >> intrinsics;
|
||||
fs["distortion_coefficients"] >> distortion;
|
||||
|
||||
#.
|
||||
Now we are ready to find chessboard pose by running ``solvePnP``: ::
|
||||
|
||||
vector<Point3f> boardPoints;
|
||||
// fill the array
|
||||
...
|
||||
|
||||
solvePnP(Mat(boardPoints), Mat(foundBoardCorners), cameraMatrix,
|
||||
distCoeffs, rvec, tvec, false);
|
||||
|
||||
#.
|
||||
Calculate reprojection error like it is done in ``calibration`` sample (see ``opencv/samples/cpp/calibration.cpp``, function ``computeReprojectionErrors``).
|
||||
|
||||
Question: how to calculate the distance from the camera origin to any of the corners?
|
@ -1,74 +0,0 @@
|
||||
##########
|
||||
Features2D
|
||||
##########
|
||||
|
||||
.. highlight:: cpp
|
||||
|
||||
Detection of planar objects
|
||||
===========================
|
||||
|
||||
The goal of this tutorial is to learn how to use *features2d* and *calib3d* modules for detecting known planar objects in scenes.
|
||||
|
||||
*Test data*: use images in your data folder, for instance, ``box.png`` and ``box_in_scene.png``.
|
||||
|
||||
#.
|
||||
Create a new console project. Read two input images. ::
|
||||
|
||||
Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
|
||||
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
|
||||
|
||||
#.
|
||||
Detect keypoints in both images. ::
|
||||
|
||||
// detecting keypoints
|
||||
FastFeatureDetector detector(15);
|
||||
vector<KeyPoint> keypoints1;
|
||||
detector.detect(img1, keypoints1);
|
||||
|
||||
... // do the same for the second image
|
||||
|
||||
#.
|
||||
Compute descriptors for each of the keypoints. ::
|
||||
|
||||
// computing descriptors
|
||||
SurfDescriptorExtractor extractor;
|
||||
Mat descriptors1;
|
||||
extractor.compute(img1, keypoints1, descriptors1);
|
||||
|
||||
... // process keypoints from the second image as well
|
||||
|
||||
#.
|
||||
Now, find the closest matches between descriptors from the first image to the second: ::
|
||||
|
||||
// matching descriptors
|
||||
BruteForceMatcher<L2<float> > matcher;
|
||||
vector<DMatch> matches;
|
||||
matcher.match(descriptors1, descriptors2, matches);
|
||||
|
||||
#.
|
||||
Visualize the results: ::
|
||||
|
||||
// drawing the results
|
||||
namedWindow("matches", 1);
|
||||
Mat img_matches;
|
||||
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
|
||||
imshow("matches", img_matches);
|
||||
waitKey(0);
|
||||
|
||||
#.
|
||||
Find the homography transformation between two sets of points: ::
|
||||
|
||||
vector<Point2f> points1, points2;
|
||||
// fill the arrays with the points
|
||||
....
|
||||
Mat H = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
|
||||
|
||||
|
||||
#.
|
||||
Create a set of inlier matches and draw them. Use perspectiveTransform function to map points with homography:
|
||||
|
||||
Mat points1Projected;
|
||||
perspectiveTransform(Mat(points1), points1Projected, H);
|
||||
|
||||
#.
|
||||
Use ``drawMatches`` for drawing inliers.
|