1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/**
\page tutorial-imgproc-contour Tutorial: Contours extraction from a binary image
\tableofcontents
\section imgproc_contour_intro Introduction
This tutorial will show you how to extract the contours from a binary image. The contour extraction algorithm is based on \cite articleSuzuki article and most of the implementation has been ported from \cite Hare:2011:OIJ:2072298.2072421 library.
The function to call is vp::findContours(const vpImage<unsigned char> &, vpContour &, std::vector<std::vector<vpImagePoint> > &, const vpContourRetrievalType&):
- the first argument is the image where '0' pixel value means the background and '1' pixel value means the foreground. **Other values are not allowed.**
- the second argument is a vp::vpContour structure that contains the list of contours in a tree
- the third argument is the list of contours
- the last argument is an option to choose the type of contour extraction, see vp::vpContourRetrievalType
The vp::vpContour structure is composed of:
- std::vector< \ref vp::vpContour * > m_children, the list of children contours for the current contour
- vp::vpContourType m_contourType, the type of contour (vp::CONTOUR_OUTER or vp::CONTOUR_HOLE)
- vp::vpContour * m_parent, the parent contour for the current contour
- std::vector< \ref vpImagePoint > m_points, the list of contour points
- the first or top level contour is called the root contour (with vp::CONTOUR_HOLE type by default) and contains in \a m_children the list of contours
The different contour extraction methods are:
- vp::CONTOUR_RETR_TREE, all the contours are extracted and stored in a hierarchical tree.
- vp::CONTOUR_RETR_LIST, all the contours are extracted and stored in a list. The top level contour contains in \a m_children the list of all the extracted contours.
- vp::CONTOUR_RETR_EXTERNAL, only the external contours are extracted and stored in a list. The top level contour contains in \a m_children the list of the external extracted contours.
The next section will provide a concrete example for better understanding.
\section imgproc_contour_example Example code
The following example also available in tutorial-contour.cpp will demonstrate on a sample image the result of each of these methods:
\include tutorial-contour.cpp
These functions are provided in a \a vp:: namespace and accessible using this include:
\snippet tutorial-contour.cpp Include
The first steps are:
- read an image in grayscale
\snippet tutorial-contour.cpp Read
- threshold / binarize the image, here with the Otsu method.
\snippet tutorial-contour.cpp Otsu
If the object of interest is in white in the image, the formula for the binarization is:
\f[
I_{bin}\left ( i,j \right ) =
\left \{ \begin{matrix}
0 \text{ if } I_{src}\left ( i,j \right ) < \text{threshold} \\
1 \text{ otherwise}
\end{matrix} \right.
\f]
If the object of interest is in black in the image, the formula for the binarization is:
\f[
I_{bin}\left ( i,j \right ) =
\left \{ \begin{matrix}
1 \text{ if } I_{src}\left ( i,j \right ) < \text{threshold} \\
0 \text{ otherwise}
\end{matrix} \right.
\f]
- extract the contours (by default, it is the vp::CONTOUR_RETR_TREE method)
\snippet tutorial-contour.cpp Find contours
- draw the contours if wanted
\snippet tutorial-contour.cpp Draw contours
The result images for each step are:
\image html img-auto-threshold-grid36-03.png "Input image"
\image html img-tutorial-contour-binarisation.png "Image after binarization using the Otsu method"
\image html img-tutorial-contour-draw-contours.png "Contours extracted and displayed on a new image"
To understand how the hierarchical contours extraction works, let's switch on another example. In a terminal, run:
\code
$ ./tutorial-contour --input Contours_tree.pgm --white_foreground
\endcode
The image after binarisation:
\image html img-tutorial-contour-binarisation2.png "Image after binarization using the Otsu method"
Instead of drawing all the contours with the same color, we can assign a first color for vp::CONTOUR_OUTER contour and a second color for vp::CONTOUR_HOLE contour.
The function to navigate in the contour tree is the following:
\snippet tutorial-contour.cpp Draw contours hierarchical func
The call to draw the hierarchical contours:
\snippet tutorial-contour.cpp Draw contours hierarchical
The result image is:
\image html img-tutorial-contour-draw-contours2.png "Contours extracted and displayed on a new image, in red outer contours, in green hole contours"
To display the hierarchy, we can use this function:
\snippet tutorial-contour.cpp Print contours hierarchy func
For the vp::CONTOUR_RETR_TREE method, the output is:
<blockquote>
Contour:\n
level: 0\n
contour type: hole contour\n
contour size: 0\n
nb children: 3\n\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 438\n
nb children: 0\n\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 748\n
nb children: 0\n\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 2012\n
nb children: 1\n\n
Contour:\n
level: 2\n
contour type: hole contour\n
contour size: 1906\n
nb children: 1\n\n
Contour:\n
level: 3\n
contour type: outer contour\n
contour size: 1610\n
nb children: 1\n\n
Contour:\n
level: 4\n
contour type: hole contour\n
contour size: 1494\n
nb children: 1\n\n
Contour:\n
level: 5\n
contour type: outer contour\n
contour size: 792\n
nb children: 2\n\n
Contour:\n
level: 6\n
contour type: hole contour\n
contour size: 372\n
nb children: 0\n\n
Contour:\n
level: 6\n
contour type: hole contour\n
contour size: 392\n
nb children: 0\n
</blockquote>
The top level contour is always the root contour with zero contour point and which contains the list of contours.
For the vp::CONTOUR_RETR_EXTERNAL method, the output is:
<blockquote>
Contour:\n
level: 0\n
contour type: hole contour\n
contour size: 0\n
nb children: 3\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 438\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 748\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 2012\n
nb children: 0\n
</blockquote>
The result image is:
\image html img-tutorial-contour-draw-contours3.png "External contours extracted and displayed on a new image"
For the vp::CONTOUR_RETR_LIST method, the output is:
<blockquote>
Contour:\n
level: 0\n
contour type: hole contour\n
contour size: 0\n
nb children: 9\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 438\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 748\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 2012\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: hole contour\n
contour size: 1906\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 1610\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: hole contour\n
contour size: 1494\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: outer contour\n
contour size: 792\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: hole contour\n
contour size: 372\n
nb children: 0\n
Contour:\n
level: 1\n
contour type: hole contour\n
contour size: 392\n
nb children: 0\n
</blockquote>
\section imgproc_contour_next Next tutorial
You can now read the \ref tutorial-imgproc-connected-components, for a similar method to extract the connected-components in a grayscale image.
*/
|