~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/doc/tutorials/imgproc/morph_lines_detection/moprh_lines_detection.md

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Extract horizontal and vertical lines by using morphological operations {#tutorial_moprh_lines_detection}
 
2
=============
 
3
 
 
4
Goal
 
5
----
 
6
 
 
7
In this tutorial you will learn how to:
 
8
 
 
9
-   Apply two very common morphology operators (i.e. Dilation and Erosion), with the creation of custom kernels, in order to extract straight lines on the horizontal and vertical axes. For this purpose, you will use the following OpenCV functions:
 
10
    -   @ref cv::erode
 
11
    -   @ref cv::dilate
 
12
    -   @ref cv::getStructuringElement
 
13
 
 
14
    in an example where your goal will be to extract the music notes from a music sheet.
 
15
 
 
16
Theory
 
17
------
 
18
 
 
19
### Morphology Operations
 
20
Morphology is a set of image processing operations that process images based on predefined *structuring elements* known also as kernels. The value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors. By choosing the size and shape of the kernel, you can construct a morphological operation that is sensitive to specific shapes regarding the input image.
 
21
 
 
22
Two of the most basic morphological operations are dilation and erosion. Dilation adds pixels to the boundaries of the object in an image, while erosion does exactly the opposite. The amount of pixels added or removed, respectively depends on the size and shape of the structuring element used to process the image. In general the rules followed from these two operations have as follows:
 
23
 
 
24
-   __Dilation__: The value of the output pixel is the <b><em>maximum</em></b> value of all the pixels that fall within the structuring element's size and shape. For example in a binary image, if any of the pixels of the input image falling within the range of the kernel is set to the value 1, the corresponding pixel of the output image will be set to 1 as well. The latter applies to any type of image (e.g. grayscale, bgr, etc).
 
25
 
 
26
    ![Dilation on a Binary Image](images/morph21.gif)
 
27
 
 
28
    ![Dilation on a Grayscale Image](images/morph6.gif)
 
29
 
 
30
-   __Erosion__: The vise versa applies for the erosion operation. The value of the output pixel is the <b><em>minimum</em></b> value of all the pixels that fall within the structuring element's size and shape. Look the at the example figures below:
 
31
 
 
32
    ![Erosion on a Binary Image](images/morph211.png)
 
33
 
 
34
    ![Erosion on a Grayscale Image](images/morph61.png)
 
35
 
 
36
### Structuring Elements
 
37
 
 
38
As it can be seen above and in general in any morphological operation the structuring element used to probe the input image, is the most important part.
 
39
 
 
40
A structuring element is a matrix consisting of only 0's and 1's that can have any arbitrary shape and size. Typically are much smaller than the image being processed, while the pixels with values of 1 define the neighborhood. The center pixel of the structuring element, called the origin, identifies the pixel of interest -- the pixel being processed.
 
41
 
 
42
For example, the following illustrates a diamond-shaped structuring element of 7x7 size.
 
43
 
 
44
![A Diamond-Shaped Structuring Element and its Origin](images/morph12.gif)
 
45
 
 
46
A structuring element can have many common shapes, such as lines, diamonds, disks, periodic lines, and circles and sizes. You typically choose a structuring element the same size and shape as the objects you want to process/extract in the input image. For example, to find lines in an image, create a linear structuring element as you will see later.
 
47
 
 
48
Code
 
49
----
 
50
 
 
51
This tutorial code's is shown lines below. You can also download it from [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp).
 
52
@include samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp
 
53
 
 
54
Explanation / Result
 
55
--------------------
 
56
 
 
57
-#  Load the source image and check if it is loaded without any problem, then show it:
 
58
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp load_image
 
59
    ![](images/src.png)
 
60
 
 
61
-#  Then transform image to grayscale if it not already:
 
62
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp gray
 
63
    ![](images/gray.png)
 
64
 
 
65
-#  Afterwards transform grayscale image to binary. Notice the ~ symbol which indicates that we use the inverse (i.e. bitwise_not) version of it:
 
66
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp bin
 
67
    ![](images/binary.png)
 
68
 
 
69
-#  Now we are ready to apply morphological operations in order to extract the horizontal and vertical lines and as a consequence to separate the the music notes from the music sheet, but first let's initialize the output images that we will use for that reason:
 
70
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp init
 
71
 
 
72
-#  As we specified in the theory in order to extract the object that we desire, we need to create the corresponding structure element. Since here we want to extract the horizontal lines, a corresponding structure element for that purpose will have the following shape:
 
73
    ![](images/linear_horiz.png)
 
74
    and in the source code this is represented by the following code snippet:
 
75
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp horiz
 
76
    ![](images/horiz.png)
 
77
 
 
78
-#  The same applies for the vertical lines, with the corresponding structure element:
 
79
    ![](images/linear_vert.png)
 
80
    and again this is represented as follows:
 
81
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp vert
 
82
    ![](images/vert.png)
 
83
 
 
84
-#  As you can see we are almost there. However, at that point you will notice that the edges of the notes are a bit rough. For that reason we need to refine the edges in order to obtain a smoother result:
 
85
    @snippet samples/cpp/tutorial_code/ImgProc/Morphology_3.cpp smooth
 
86
    ![](images/smooth.png)