]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cv_image_transform.tex
fixed both offline and online docs rendering
[opencv.git] / opencv / doc / cv_image_transform.tex
1 \section{Miscellaneous Image Transformations}
2
3 \ifCPy
4
5 \cvCPyFunc{AdaptiveThreshold}
6 Applies an adaptive threshold to an array.
7
8 \cvdefC{
9 void cvAdaptiveThreshold(
10 \par const CvArr* src,\par CvArr* dst,\par double maxValue,\par
11 int adaptive\_method=CV\_ADAPTIVE\_THRESH\_MEAN\_C,\par
12 int thresholdType=CV\_THRESH\_BINARY,\par
13 int blockSize=3,\par double param1=5 );
14 }\cvdefPy{AdaptiveThreshold(src,dst,maxValue, adaptive\_method=CV\_ADAPTIVE\_THRESH\_MEAN\_C, thresholdType=CV\_THRESH\_BINARY,blockSize=3,param1=5)-> None}
15
16 \begin{description}
17 \cvarg{src}{Source image}
18 \cvarg{dst}{Destination image}
19 \cvarg{maxValue}{Maximum value that is used with \texttt{CV\_THRESH\_BINARY} and \texttt{CV\_THRESH\_BINARY\_INV}}
20 \cvarg{adaptive\_method}{Adaptive thresholding algorithm to use: \texttt{CV\_ADAPTIVE\_THRESH\_MEAN\_C} or \texttt{CV\_ADAPTIVE\_THRESH\_GAUSSIAN\_C} (see the discussion)}
21 \cvarg{thresholdType}{Thresholding type; must be one of
22 \begin{description}
23   \cvarg{CV\_THRESH\_BINARY}{xxx}
24   \cvarg{CV\_THRESH\_BINARY\_INV}{xxx}
25 \end{description}}
26 \cvarg{blockSize}{The size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on}
27 \cvarg{param1}{The method-dependent parameter. For the methods \texttt{CV\_ADAPTIVE\_THRESH\_MEAN\_C} and \texttt{CV\_ADAPTIVE\_THRESH\_GAUSSIAN\_C} it is a constant subtracted from the mean or weighted mean (see the discussion), though it may be negative}
28 \end{description}
29
30 The function transforms a grayscale image to a binary image according to the formulas:
31
32 \begin{description}
33 \cvarg{CV\_THRESH\_BINARY}{\[ dst(x,y) = \fork{\texttt{maxValue}}{if $src(x,y) > T(x,y)$}{0}{otherwise} \]}
34 \cvarg{CV\_THRESH\_BINARY\_INV}{\[ dst(x,y) = \fork{0}{if $src(x,y) > T(x,y)$}{\texttt{maxValue}}{otherwise} \]}
35 \end{description}
36
37 where $T(x,y)$ is a threshold calculated individually for each pixel.
38
39 For the method \texttt{CV\_ADAPTIVE\_THRESH\_MEAN\_C} it is the mean of a $\texttt{blockSize} \times \texttt{blockSize}$ pixel neighborhood, minus \texttt{param1}.
40
41 For the method \texttt{CV\_ADAPTIVE\_THRESH\_GAUSSIAN\_C} it is the weighted sum (gaussian) of a $\texttt{blockSize} \times \texttt{blockSize}$ pixel neighborhood, minus \texttt{param1}.
42
43 \cvCPyFunc{CvtColor}
44 Converts an image from one color space to another.
45
46 \cvdefC{
47 void cvCvtColor(
48 \par const CvArr* src,
49 \par CvArr* dst,
50 \par int code );
51 }\cvdefPy{CvtColor(src,dst,code)-> None}
52
53 \begin{description}
54 \cvarg{src}{The source 8-bit (8u), 16-bit (16u) or single-precision floating-point (32f) image}
55 \cvarg{dst}{The destination image of the same data type as the source. The number of channels may be different}
56 \cvarg{code}{Color conversion operation that can be specifed using \texttt{CV\_ \textit{src\_color\_space} 2 \textit{dst\_color\_space}} constants (see below)}
57 \end{description}
58
59 The function converts the input image from one color
60 space to another. The function ignores the \texttt{colorModel} and
61 \texttt{channelSeq} fields of the \texttt{IplImage} header, so the
62 source image color space should be specified correctly (including
63 order of the channels in the case of RGB space. For example, BGR means 24-bit
64 format with $B_0, G_0, R_0, B_1, G_1, R_1, ...$ layout
65 whereas RGB means 24-format with $R_0, G_0, B_0, R_1, G_1, B_1, ...$
66 layout).
67
68 The conventional range for R,G,B channel values is:
69
70 \begin{itemize}
71  \item 0 to 255 for 8-bit images
72  \item 0 to 65535 for 16-bit images and
73  \item 0 to 1 for floating-point images.
74 \end{itemize}
75
76 Of course, in the case of linear transformations the range can be
77 specific, but in order to get correct results in the case of non-linear
78 transformations, the input image should be scaled.
79
80 The function can do the following transformations:
81
82 \begin{itemize}
83  \item Transformations within RGB space like adding/removing the alpha channel, reversing the channel order, conversion to/from 16-bit RGB color (R5:G6:B5 or R5:G5:B5), as well as conversion to/from grayscale using:
84  \[
85  \text{RGB[A] to Gray:} Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
86  \]
87  and
88  \[
89  \text{Gray to RGB[A]:} R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow 0
90  \]
91
92 The conversion from a RGB image to gray is done with:
93 \begin{lstlisting}
94 cvCvtColor(src ,bwsrc, CV_RGB2GRAY)
95 \end{lstlisting}
96
97  \item RGB $\leftrightarrow$ CIE XYZ.Rec 709 with D65 white point (\texttt{CV\_BGR2XYZ, CV\_RGB2XYZ, CV\_XYZ2BGR, CV\_XYZ2RGB}):
98  \[
99  \begin{bmatrix}
100  X \\
101  Y \\
102  Z
103  \end{bmatrix}
104  \leftarrow
105  \begin{bmatrix}
106 0.412453 & 0.357580 & 0.180423\\
107 0.212671 & 0.715160 & 0.072169\\
108 0.019334 & 0.119193 & 0.950227
109  \end{bmatrix}
110  \cdot
111  \begin{bmatrix}
112  R \\
113  G \\
114  B
115  \end{bmatrix}
116  \]
117  \[
118  \begin{bmatrix}
119  R \\
120  G \\
121  B
122  \end{bmatrix}
123  \leftarrow
124  \begin{bmatrix}
125 3.240479 & -1.53715 & -0.498535\\
126 -0.969256 &  1.875991 & 0.041556\\
127 0.055648 & -0.204043 & 1.057311
128  \end{bmatrix}
129  \cdot
130  \begin{bmatrix}
131  X \\
132  Y \\
133  Z
134  \end{bmatrix}
135  \]
136 $X$, $Y$ and $Z$ cover the whole value range (in the case of floating-point images $Z$ may exceed 1).
137
138  \item RGB $\leftrightarrow$ YCrCb JPEG (a.k.a. YCC) (\texttt{CV\_BGR2YCrCb, CV\_RGB2YCrCb, CV\_YCrCb2BGR, CV\_YCrCb2RGB})
139  \[ Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B \]
140  \[  Cr \leftarrow (R-Y) \cdot 0.713 + delta \]
141  \[  Cb \leftarrow (B-Y) \cdot 0.564 + delta \]
142  \[  R \leftarrow Y + 1.403 \cdot (Cr - delta) \]
143  \[  G \leftarrow Y - 0.344 \cdot (Cr - delta) - 0.714 \cdot (Cb - delta) \]
144  \[  B \leftarrow Y + 1.773 \cdot (Cb - delta) \]
145 where
146  \[
147   delta = \left\{
148   \begin{array}{l l}
149   128 & \mbox{for 8-bit images}\\
150   32768 & \mbox{for 16-bit images}\\
151   0.5 & \mbox{for floating-point images}
152   \end{array} \right.
153  \]
154 Y, Cr and Cb cover the whole value range.
155
156  \item RGB $\leftrightarrow$ HSV (\texttt{CV\_BGR2HSV, CV\_RGB2HSV, CV\_HSV2BGR, CV\_HSV2RGB})
157   in the case of 8-bit and 16-bit images
158   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range
159   \[ V \leftarrow max(R,G,B) \]
160
161 \[ S \leftarrow \fork{\frac{V-min(R,G,B)}{V}}{if $V \neq 0$}{0}{otherwise} \]
162 \[ H \leftarrow \forkthree
163 {{60(G - B)}/{S}}{if $V=R$}
164 {{120+60(B - R)}/{S}}{if $V=G$}
165 {{240+60(R - G)}/{S}}{if $V=B$} \]
166 if $H<0$ then $H \leftarrow H+360$
167
168 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
169
170 The values are then converted to the destination data type:
171 \begin{description}
172 \item[8-bit images]
173 \[ V \leftarrow 255 V, S \leftarrow 255 S, H \leftarrow H/2 \text{(to fit to 0 to 255)} \]
174 \item[16-bit images (currently not supported)]
175 \[ V <- 65535 V, S <- 65535 S, H <- H \]
176 \item[32-bit images]
177 H, S, V are left as is
178 \end{description}
179
180  \item RGB $\leftrightarrow$ HLS (\texttt{CV\_BGR2HLS, CV\_RGB2HLS, CV\_HLS2BGR, CV\_HLS2RGB}).
181   in the case of 8-bit and 16-bit images
182   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range.
183   \[ V_{max} \leftarrow {max}(R,G,B) \]
184   \[ V_{min} \leftarrow {min}(R,G,B) \]
185   \[ L \leftarrow \frac{V_{max} - V_{min}}{2} \]
186   \[ S \leftarrow \fork
187     {\frac{V_{max} - V_{min}}{V_{max} + V_{min}}}{if $L < 0.5$}
188     {\frac{V_{max} - V_{min}}{2 - (V_{max} + V_{min})}}{if $L \ge 0.5$} \]
189   \[ H \leftarrow \forkthree
190   {{60(G - B)}/{S}}{if $V_{max}=R$}
191   {{120+60(B - R)}/{S}}{if $V_{max}=G$}
192   {{240+60(R - G)}/{S}}{if $V_{max}=B$} \]
193   if $H<0$ then $H \leftarrow H+360$
194 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
195
196 The values are then converted to the destination data type:
197 \begin{description}
198 \item[8-bit images]
199 \[ V \leftarrow 255 V, S \leftarrow 255 S, H \leftarrow H/2 \text{(to fit to 0 to 255)} \]
200 \item[16-bit images (currently not supported)]
201 \[ V <- 65535 V, S <- 65535 S, H <- H \]
202 \item[32-bit images]
203 H, S, V are left as is
204 \end{description}
205
206  \item RGB $\leftrightarrow$ CIE L*a*b* (\texttt{CV\_BGR2Lab, CV\_RGB2Lab, CV\_Lab2BGR, CV\_Lab2RGB})
207   in the case of 8-bit and 16-bit images
208   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range
209 \[ \vecthree{X}{Y}{Z} \leftarrow \vecthreethree
210 {0.412453}{0.357580}{0.180423}
211 {0.212671}{0.715160}{0.072169}
212 {0.019334}{0.119193}{0.950227}
213 \cdot
214 \vecthree{R}{G}{B} \]
215 \[ X \leftarrow X/X_n, \text{where} X_n = 0.950456 \]
216 \[ Z \leftarrow Z/Z_n, \text{where} Z_n = 1.088754 \]
217 \[ L \leftarrow \fork
218 {116*Y^{1/3}-16}{for $Y>0.008856$}
219 {903.3*Y}{for $Y \le 0.008856$} \]
220 \[ a \leftarrow 500 (f(X)-f(Y)) + delta \]
221 \[ b \leftarrow 200 (f(Y)-f(Z)) + delta \]
222 where
223 \[f(t)=\fork
224 {t^{1/3}}{for $t>0.008856$}
225 {7.787 t+16/116}{for $t<=0.008856$} \]
226 and
227 \[ delta = \fork{128}{for 8-bit images}{0}{for floating-point images} \]
228 On output $0 \leq L \leq 100$, $-127 \leq a \leq 127$, $-127 \leq b \leq 127$
229
230 The values are then converted to the destination data type:
231 \begin{description}
232 \item[8-bit images]
233 \[L \leftarrow L*255/100, a \leftarrow a + 128, b \leftarrow b + 128\]
234 \item[16-bit images] currently not supported
235 \item[32-bit images]
236 L, a, b are left as is
237 \end{description}
238
239  \item RGB $\leftrightarrow$ CIE L*u*v* (\texttt{CV\_BGR2Luv, CV\_RGB2Luv, CV\_Luv2BGR, CV\_Luv2RGB})
240   in the case of 8-bit and 16-bit images
241   R, G and B are converted to floating-point format and scaled to fit 0 to 1 range
242   \[ \vecthree{X}{Y}{Z} \leftarrow \vecthreethree
243 {0.412453}{0.357580}{0.180423}
244 {0.212671}{0.715160}{0.072169}
245 {0.019334}{0.119193}{0.950227}
246 \cdot
247 \vecthree{R}{G}{B} \]
248 \[ L \leftarrow \fork
249 {116 Y^{1/3}}{for $Y>0.008856$}
250 {903.3 Y}{for $Y<=0.008856$} \]
251 \[ u' \leftarrow 4*X/(X + 15*Y + 3 Z) \]
252 \[ v' \leftarrow 9*Y/(X + 15*Y + 3 Z) \]
253 \[ u \leftarrow 13*L*(u' - u_n) \quad \text{where} \quad u_n=0.19793943 \]
254 \[ v \leftarrow 13*L*(v' - v_n) \quad \text{where} \quad v_n=0.46831096 \]
255 On output $0 \leq L \leq 100$, $-134 \leq u \leq 220$, $-140 \leq v \leq 122$.
256
257 The values are then converted to the destination data type:
258 \begin{description}
259 \item[8-bit images]
260 \[L \leftarrow 255/100 L, u \leftarrow 255/354 (u + 134), v \leftarrow 255/256 (v + 140) \]
261 \item[16-bit images] currently not supported
262 \item[32-bit images] L, u, v are left as is
263 \end{description}
264
265 The above formulas for converting RGB to/from various color spaces have been taken from multiple sources on Web, primarily from
266 the Ford98
267  at the Charles Poynton site.
268
269  \item Bayer $\rightarrow$ RGB (\texttt{CV\_BayerBG2BGR, CV\_BayerGB2BGR, CV\_BayerRG2BGR, CV\_BayerGR2BGR, CV\_BayerBG2RGB, CV\_BayerGB2RGB, CV\_BayerRG2RGB, CV\_BayerGR2RGB}) The Bayer pattern is widely used in CCD and CMOS cameras. It allows one to get color pictures from a single plane where R,G and B pixels (sensors of a particular component) are interleaved like this:
270
271 \newcommand{\Rcell}{\color{red}R}
272 \newcommand{\Gcell}{\color{green}G}
273 \newcommand{\Bcell}{\color{blue}B}
274
275
276 \[
277 \definecolor{BackGray}{rgb}{0.8,0.8,0.8}
278 \begin{array}{ c c c c c }
279 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell\\
280 \Gcell&\colorbox{BackGray}{\Bcell}&\colorbox{BackGray}{\Gcell}&\Bcell&\Gcell\\
281 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell\\
282 \Gcell&\Bcell&\Gcell&\Bcell&\Gcell\\
283 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell
284 \end{array}
285 \]
286
287 The output RGB components of a pixel are interpolated from 1, 2 or
288 4 neighbors of the pixel having the same color. There are several
289 modifications of the above pattern that can be achieved by shifting
290 the pattern one pixel left and/or one pixel up. The two letters
291 $C_1$ and $C_2$
292 in the conversion constants
293 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2BGR}
294 and
295 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2RGB}
296 indicate the particular pattern
297 type - these are components from the second row, second and third
298 columns, respectively. For example, the above pattern has very
299 popular "BG" type.
300 \end{itemize}
301
302 \cvCPyFunc{DistTransform}
303 Calculates the distance to the closest zero pixel for all non-zero pixels of the source image.
304
305 \cvdefC{
306 void cvDistTransform( \par const CvArr* src,\par CvArr* dst,\par int distance\_type=CV\_DIST\_L2,\par int mask\_size=3,\par const float* mask=NULL,\par CvArr* labels=NULL );
307 }\cvdefPy{DistTransform(src,dst,distance\_type=CV\_DIST\_L2,mask\_size=3,mask={NULL,0},labels=NULL)-> None}
308
309 \begin{description}
310 \cvarg{src}{8-bit, single-channel (binary) source image}
311 \cvarg{dst}{Output image with calculated distances (32-bit floating-point, single-channel)}
312 \cvarg{distance\_type}{Type of distance; can be \texttt{CV\_DIST\_L1, CV\_DIST\_L2, CV\_DIST\_C} or \texttt{CV\_DIST\_USER}}
313 \cvarg{mask\_size}{Size of the distance transform mask; can be 3 or 5. in the case of \texttt{CV\_DIST\_L1} or \texttt{CV\_DIST\_C} the parameter is forced to 3, because a $3\times 3$ mask gives the same result as a $5\times 5 $ yet it is faster}
314 \cvarg{mask}{User-defined mask in the case of a user-defined distance, it consists of 2 numbers (horizontal/vertical shift cost, diagonal shift cost) in the case ofa  $3\times 3$ mask and 3 numbers (horizontal/vertical shift cost, diagonal shift cost, knight's move cost) in the case of a $5\times 5$ mask}
315 \cvarg{labels}{The optional output 2d array of integer type labels, the same size as \texttt{src} and \texttt{dst}}
316 \end{description}
317
318 The function calculates the approximated
319 distance from every binary image pixel to the nearest zero pixel.
320 For zero pixels the function sets the zero distance, for others it
321 finds the shortest path consisting of basic shifts: horizontal,
322 vertical, diagonal or knight's move (the latest is available for a
323 $5\times 5$ mask). The overall distance is calculated as a sum of these
324 basic distances. Because the distance function should be symmetric,
325 all of the horizontal and vertical shifts must have the same cost (that
326 is denoted as \texttt{a}), all the diagonal shifts must have the
327 same cost (denoted \texttt{b}), and all knight's moves must have
328 the same cost (denoted \texttt{c}). For \texttt{CV\_DIST\_C} and
329 \texttt{CV\_DIST\_L1} types the distance is calculated precisely,
330 whereas for \texttt{CV\_DIST\_L2} (Euclidian distance) the distance
331 can be calculated only with some relative error (a $5\times 5$ mask
332 gives more accurate results), OpenCV uses the values suggested in
333 \cite{Borgefors86}:
334
335
336 \begin{tabular}{| c | c | c |}
337 \hline
338 \texttt{CV\_DIST\_C}  & $(3\times 3)$ & a = 1, b = 1\\ \hline
339 \texttt{CV\_DIST\_L1} & $(3\times 3)$ & a = 1, b = 2\\ \hline
340 \texttt{CV\_DIST\_L2} & $(3\times 3)$ & a=0.955, b=1.3693\\ \hline
341 \texttt{CV\_DIST\_L2} & $(5\times 5)$ & a=1, b=1.4, c=2.1969\\ \hline
342 \end{tabular}
343
344 And below are samples of the distance field (black (0) pixel is in the middle of white square) in the case of a user-defined distance:
345
346 User-defined $3 \times 3$ mask (a=1, b=1.5)
347
348 \begin{tabular}{| c | c | c | c | c | c | c |}
349 \hline
350 4.5 & 4 & 3.5 & 3 & 3.5 & 4 & 4.5\\ \hline
351 4 & 3 & 2.5 & 2 & 2.5 & 3 & 4\\ \hline
352 3.5 & 2.5 & 1.5 & 1 & 1.5 & 2.5 & 3.5\\ \hline
353 3 & 2 & 1 &     & 1 & 2 & 3\\ \hline
354 3.5 & 2.5 & 1.5 & 1 & 1.5 & 2.5 & 3.5\\ \hline
355 4 & 3 & 2.5 & 2 & 2.5 & 3 & 4\\ \hline
356 4.5 & 4 & 3.5 & 3 & 3.5 & 4 & 4.5\\ \hline
357 \end{tabular}
358
359 User-defined $5 \times 5$ mask (a=1, b=1.5, c=2)
360
361 \begin{tabular}{| c | c | c | c | c | c | c |}
362 \hline
363 4.5 & 3.5 & 3 & 3 & 3 & 3.5 & 4.5\\ \hline
364 3.5 & 3 & 2 & 2 & 2 & 3 & 3.5\\ \hline
365 3 & 2 & 1.5 & 1 & 1.5 & 2 & 3\\ \hline
366 3 & 2 & 1 & & 1 & 2 & 3\\ \hline
367 3 & 2 & 1.5 & 1 & 1.5 & 2 & 3\\ \hline
368 3.5 & 3 & 2 & 2 & 2 & 3 & 3.5\\ \hline
369 4 & 3.5 & 3 & 3 & 3 & 3.5 & 4\\ \hline
370 \end{tabular}
371
372
373 Typically, for a fast, coarse distance estimation \texttt{CV\_DIST\_L2},
374 a $3\times 3$ mask is used, and for a more accurate distance estimation
375 \texttt{CV\_DIST\_L2}, a $5\times 5$ mask is used.
376
377 When the output parameter \texttt{labels} is not \texttt{NULL}, for
378 every non-zero pixel the function also finds the nearest connected
379 component consisting of zero pixels. The connected components
380 themselves are found as contours in the beginning of the function.
381
382 In this mode the processing time is still O(N), where N is the number of
383 pixels. Thus, the function provides a very fast way to compute approximate
384 Voronoi diagram for the binary image.
385
386 \cvCPyFunc{FloodFill}
387 Fills a connected component with the given color.
388
389 \cvdefC{
390 void cvFloodFill(\par CvArr* image,\par CvPoint seed\_point,\par CvScalar new\_val,\par
391                   CvScalar lo\_diff=cvScalarAll(0),\par CvScalar up\_diff=cvScalarAll(0),\par
392                   CvConnectedComp* comp=NULL,\par int flags=4,\par CvArr* mask=NULL );
393
394 }\cvdefPy{FloodFill(image,seed\_point,new\_val,lo\_diff=cvScalarAll(0),up\_diff=cvScalarAll(0),flags=4,mask=NULL)-> comp}
395
396 \ifC
397 \begin{lstlisting}
398 typedef struct CvConnectedComp
399 {
400     double area;    /* area of the segmented component */
401     CvScalar value; /* average color of the connected component */
402     CvRect rect;    /* ROI of the segmented component */
403     CvSeq* contour; /* optional component boundary
404                       (the contour might have child contours corresponding to the holes) */
405 } CvConnectedComp;
406
407 #define CV_FLOODFILL_FIXED_RANGE (1 << 16)
408 #define CV_FLOODFILL_MASK_ONLY   (1 << 17)
409 \end{lstlisting}
410 \fi
411
412 \begin{description}
413 \cvarg{image}{Input 1- or 3-channel, 8-bit or floating-point image. It is modified by the function unless the \texttt{CV\_FLOODFILL\_MASK\_ONLY} flag is set (see below)}
414 \cvarg{seed\_point}{The starting point}
415 \cvarg{new\_val}{New value of the repainted domain pixels}
416 \cvarg{lo\_diff}{Maximal lower brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component. In the case of 8-bit color images it is a packed value}
417 \cvarg{up\_diff}{Maximal upper brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component. In the case of 8-bit color images it is a packed value}
418 \cvarg{comp}{Pointer to the structure that the function fills with the information about the repainted domain}
419 \cvarg{flags}{The operation flags. Lower bits contain connectivity value, 4 (by default) or 8, used within the function. Connectivity determines which neighbors of a pixel are considered. Upper bits can be 0 or a combination of the following flags:
420 \begin{description}
421   \cvarg{CV\_FLOODFILL\_FIXED\_RANGE}{if set, the difference between the current pixel and seed pixel is considered, otherwise the difference between neighbor pixels is considered (the range is floating)}
422   \cvarg{CV\_FLOODFILL\_MASK\_ONLY}{if set, the function does not fill the image (\texttt{new\_val} is ignored), but fills the mask (that must be non-NULL in this case)}
423 \end{description}}
424 \cvarg{mask}{Operation mask, should be a single-channel 8-bit image, 2 pixels wider and 2 pixels taller than \texttt{image}. If not NULL, the function uses and updates the mask, so the user takes responsibility of initializing the \texttt{mask} content. Floodfilling can't go across non-zero pixels in the mask, for example, an edge detector output can be used as a mask to stop filling at edges. It is possible to use the same mask in multiple calls to the function to make sure the filled area do not overlap. \textbf{Note}: because the mask is larger than the filled image, a pixel in \texttt{mask} that corresponds to $(x,y)$ pixel in \texttt{image} will have coordinates $(x+1,y+1)$ }
425 \end{description}
426
427 The function fills a connected component starting from the seed point with the specified color. The connectivity is determined by the closeness of pixel values. The pixel at $(x,y)$ is considered to belong to the repainted domain if:
428
429 \begin{description}
430
431 \item[grayscale image, floating range] \[
432 src(x',y')-\texttt{lo\_diff} <= src(x,y) <= src(x',y')+\texttt{up\_diff} \]
433
434 \item[grayscale image, fixed range] \[
435 src(seed.x,seed.y)-\texttt{lo\_diff}<=src(x,y)<=src(seed.x,seed.y)+\texttt{up\_diff} \]
436
437 \item[color image, floating range]
438 \[ src(x',y')_r-\texttt{lo\_diff}_r<=src(x,y)_r<=src(x',y')_r+\texttt{up\_diff}_r \]
439 \[ src(x',y')_g-\texttt{lo\_diff}_g<=src(x,y)_g<=src(x',y')_g+\texttt{up\_diff}_g \]
440 \[ src(x',y')_b-\texttt{lo\_diff}_b<=src(x,y)_b<=src(x',y')_b+\texttt{up\_diff}_b \]
441
442 \item[color image, fixed range]
443 \[ src(seed.x,seed.y)_r-\texttt{lo\_diff}_r<=src(x,y)_r<=src(seed.x,seed.y)_r+\texttt{up\_diff}_r \]
444 \[ src(seed.x,seed.y)_g-\texttt{lo\_diff}_g<=src(x,y)_g<=src(seed.x,seed.y)_g+\texttt{up\_diff}_g \]
445 \[ src(seed.x,seed.y)_b-\texttt{lo\_diff}_b<=src(x,y)_b<=src(seed.x,seed.y)_b+\texttt{up\_diff}_b \]
446 \end{description}
447
448 where $src(x',y')$ is the value of one of pixel neighbors. That is, to be added to the connected component, a pixel's color/brightness should be close enough to the:
449 \begin{itemize}
450   \item color/brightness of one of its neighbors that are already referred to the connected component in the case of floating range
451   \item color/brightness of the seed point in the case of fixed range.
452 \end{itemize}
453
454 \cvCPyFunc{Inpaint}
455 Inpaints the selected region in the image.
456
457 \cvdefC{
458 void cvInpaint( \par const CvArr* src, \par const CvArr* mask, \par CvArr* dst,
459                 \par double inpaintRadius, \par int flags);
460
461 }\cvdefPy{Inpaint(src,mask,dst,inpaintRadius,flags) -> None}
462
463 \begin{description}
464 \cvarg{src}{The input 8-bit 1-channel or 3-channel image.}
465 \cvarg{mask}{The inpainting mask, 8-bit 1-channel image. Non-zero pixels indicate the area that needs to be inpainted.}
466 \cvarg{dst}{The output image of the same format and the same size as input.}
467 \cvarg{inpaintRadius}{The radius of circlular neighborhood of each point inpainted that is considered by the algorithm.}
468 \cvarg{flags}{The inpainting method, one of the following:
469 \begin{description}
470 \cvarg{CV\_INPAINT\_NS}{Navier-Stokes based method.}
471 \cvarg{CV\_INPAINT\_TELEA}{The method by Alexandru Telea \href{\#Telea04}{[Telea04]}}
472 \end{description}}
473 \end{description}
474
475 The function reconstructs the selected image area from the pixel near the area boundary. The function may be used to remove dust and scratches from a scanned photo, or to remove undesirable objects from still images or video.
476
477 \cvCPyFunc{Integral}
478 Calculates the integral of an image.
479
480 \cvdefC{
481 void cvIntegral(
482 \par const CvArr* image,
483 \par CvArr* sum,
484 \par CvArr* sqsum=NULL,
485 \par CvArr* tiltedSum=NULL );
486 }\cvdefPy{Integral(image,sum,sqsum=NULL,tiltedSum=NULL)-> None}
487
488 \begin{description}
489 \cvarg{image}{The source image, $W\times H$, 8-bit or floating-point (32f or 64f)}
490 \cvarg{sum}{The integral image, $(W+1)\times (H+1)$, 32-bit integer or double precision floating-point (64f)}
491 \cvarg{sqsum}{The integral image for squared pixel values, $(W+1)\times (H+1)$, double precision floating-point (64f)}
492 \cvarg{tiltedSum}{The integral for the image rotated by 45 degrees, $(W+1)\times (H+1)$, the same data type as \texttt{sum}}
493 \end{description}
494
495 The function calculates one or more integral images for the source image as following:
496
497 \[
498 \texttt{sum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)
499 \]
500
501 \[
502 \texttt{sqsum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)^2
503 \]
504
505 \[
506 \texttt{tiltedSum}(X,Y) = \sum_{y<Y,abs(x-X)<y} \texttt{image}(x,y)
507 \]
508
509 Using these integral images, one may calculate sum, mean and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
510
511 \[
512 \sum_{x_1<=x<x_2, \, y_1<=y<y_2} = \texttt{sum}(x_2,y_2)-\texttt{sum}(x_1,y_2)-\texttt{sum}(x_2,y_1)+\texttt{sum}(x_1,x_1)
513 \]
514
515 It makes possible to do a fast blurring or fast block correlation with variable window size, for example. In the case of multi-channel images, sums for each channel are accumulated independently.
516
517
518 \cvCPyFunc{PyrMeanShiftFiltering}
519 Does meanshift image segmentation
520
521 \cvdefC{
522
523 void cvPyrMeanShiftFiltering( \par const CvArr* src, \par CvArr* dst,
524      \par double sp, \par double sr, \par int max\_level=1,
525      \par CvTermCriteria termcrit=\par cvTermCriteria(CV\_TERMCRIT\_ITER+CV\_TERMCRIT\_EPS,5,1));
526
527 }\cvdefPy{PyrMeanShiftFiltering(src,dst,sp,sr,max\_level=1,
528        termcrit=\par (CV\_TERMCRIT\_ITER+CV\_TERMCRIT\_EPS,5,1))-> None}
529
530 \begin{description}
531 \cvarg{src}{The source 8-bit, 3-channel image.}
532 \cvarg{dst}{The destination image of the same format and the same size as the source.}
533 \cvarg{sp}{The spatial window radius.}
534 \cvarg{sr}{The color window radius.}
535 \cvarg{max\_level}{Maximum level of the pyramid for the segmentation.}
536 \cvarg{termcrit}{Termination criteria: when to stop meanshift iterations.}
537 \end{description}
538
539 The function implements the filtering
540 stage of meanshift segmentation, that is, the output of the function is
541 the filtered "posterized" image with color gradients and fine-grain
542 texture flattened. At every pixel $(X,Y)$ of the input image (or
543 down-sized input image, see below) the function executes meanshift
544 iterations, that is, the pixel $(X,Y)$ neighborhood in the joint
545 space-color hyperspace is considered:
546
547 \[
548 (x,y): X-\texttt{sp} \le x \le X+\texttt{sp} , Y-\texttt{sp} \le y \le Y+\texttt{sp} , ||(R,G,B)-(r,g,b)||  \le  \texttt{sr}
549 \]
550
551 where \texttt{(R,G,B)} and \texttt{(r,g,b)} are the vectors of color components at \texttt{(X,Y)} and \texttt{(x,y)}, respectively (though, the algorithm does not depend on the color space used, so any 3-component color space can be used instead). Over the neighborhood the average spatial value \texttt{(X',Y')} and average color vector \texttt{(R',G',B')} are found and they act as the neighborhood center on the next iteration: 
552
553 $(X,Y)~(X',Y'), (R,G,B)~(R',G',B').$
554
555 After the iterations over, the color components of the initial pixel (that is, the pixel from where the iterations started) are set to the final value (average color at the last iteration): 
556
557 $I(X,Y) <- (R*,G*,B*)$
558
559 Then $\texttt{max\_level}>0$ , the gaussian pyramid of
560 $\texttt{max\_level}+1$ levels is built, and the above procedure is run
561 on the smallest layer. After that, the results are propagated to the
562 larger layer and the iterations are run again only on those pixels where
563 the layer colors differ much ( $>\texttt{sr}$ ) from the lower-resolution
564 layer, that is, the boundaries of the color regions are clarified. Note,
565 that the results will be actually different from the ones obtained by
566 running the meanshift procedure on the whole original image (i.e. when
567 $\texttt{max\_level}==0$ ).
568
569 \cvCPyFunc{PyrSegmentation}
570 Implements image segmentation by pyramids.
571
572 \cvdefC{
573 void cvPyrSegmentation(\par IplImage* src,\par IplImage* dst,\par
574                         CvMemStorage* storage,\par CvSeq** comp,\par
575                         int level,\par double threshold1,\par double threshold2 );
576 }\cvdefPy{PyrSegmentation(src,dst,storage,level,threshold1,threshold2)-> comp}
577
578 \begin{description}
579 \cvarg{src}{The source image}
580 \cvarg{dst}{The destination image}
581 \cvarg{storage}{Storage; stores the resulting sequence of connected components}
582 \cvarg{comp}{Pointer to the output sequence of the segmented components}
583 \cvarg{level}{Maximum level of the pyramid for the segmentation}
584 \cvarg{threshold1}{Error threshold for establishing the links}
585 \cvarg{threshold2}{Error threshold for the segments clustering}
586 \end{description}
587
588 The function implements image segmentation by pyramids. The pyramid builds up to the level \texttt{level}. The links between any pixel \texttt{a} on level \texttt{i} and its candidate father pixel \texttt{b} on the adjacent level are established if
589 $p(c(a),c(b))<threshold1$.
590 After the connected components are defined, they are joined into several clusters.
591 Any two segments A and B belong to the same cluster, if $p(c(A),c(B))<threshold2$.
592 If the input image has only one channel, then $p(c^1,c^2)=|c^1-c^2|$.
593 If the input image has three channels (red, green and blue), then
594 \[
595   p(c^1,c^2) = 0.30 (c^1_r - c^2_r) +
596                0.59 (c^1_g - c^2_g) +
597                0.11 (c^1_b - c^2_b).
598 \]
599
600 There may be more than one connected component per a cluster. The images \texttt{src} and \texttt{dst} should be 8-bit single-channel or 3-channel images or equal size.
601
602 \cvCPyFunc{Threshold}
603 Applies a fixed-level threshold to array elements.
604
605 \cvdefC{
606 double cvThreshold(
607 \par const CvArr* src,
608 \par CvArr* dst,
609 \par double threshold,
610 \par double maxValue,
611 \par int thresholdType );
612 }\cvdefPy{Threshold(src,dst,threshld,maxValue,thresholdType)-> None}
613
614 \begin{description}
615 \cvarg{src}{Source array (single-channel, 8-bit of 32-bit floating point)}
616 \cvarg{dst}{Destination array; must be either the same type as \texttt{src} or 8-bit}
617 \cvarg{threshold}{Threshold value}
618 \cvarg{maxValue}{Maximum value to use with \texttt{CV\_THRESH\_BINARY} and \texttt{CV\_THRESH\_BINARY\_INV} thresholding types}
619 \cvarg{thresholdType}{Thresholding type (see the discussion)}
620 \end{description}
621
622 The function applies fixed-level thresholding
623 to a single-channel array. The function is typically used to get a
624 bi-level (binary) image out of a grayscale image (\cvCPyCross{CmpS} could
625 be also used for this purpose) or for removing a noise, i.e. filtering
626 out pixels with too small or too large values. There are several
627 types of thresholding that the function supports that are determined by
628 \texttt{thresholdType}:
629
630 \begin{description}
631 \cvarg{CV\_THRESH\_BINARY}{\[ \texttt{dst}(x,y) = \fork{\texttt{maxValue}}{if $\texttt{src}(x,y) > \texttt{threshold}$}{0}{otherwise} \]}
632 \cvarg{CV\_THRESH\_BINARY\_INV}{\[ \texttt{dst}(x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{threshold}$}{\texttt{maxValue}}{otherwise} \]}
633 \cvarg{CV\_THRESH\_TRUNC}{\[ \texttt{dst}(x,y) = \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{threshold}$}{\texttt{src}(x,y)}{otherwise} \]}
634 \cvarg{CV\_THRESH\_TOZERO}{\[ \texttt{dst}(x,y) = \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{threshold}$}{0}{otherwise} \]}
635 \cvarg{CV\_THRESH\_TOZERO\_INV}{\[ \texttt{dst}(x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{threshold}$}{\texttt{src}(x,y)}{otherwise} \]}
636 \end{description}
637
638 Also, the special value \texttt{CV\_THRESH\_OTSU} may be combined with
639 one of the above values. In this case the function determines the optimal threshold
640 value using Otsu's algorithm and uses it instead of the specified \texttt{thresh}.
641 The function returns the computed threshold value.
642 Currently, Otsu's method is implemented only for 8-bit images.
643
644 \includegraphics[width=0.5\textwidth]{pics/threshold.png}
645
646
647 \fi
648
649 \ifCpp
650
651 \cvCppFunc{adaptiveThreshold}
652 Applies an adaptive threshold to an array.
653
654 \cvdefCpp{void adaptiveThreshold( const Mat\& src, Mat\& dst, double maxValue,\par
655                         int adaptiveMethod, int thresholdType,\par
656                         int blockSize, double C );}
657 \begin{description}
658 \cvarg{src}{Source 8-bit single-channel image}
659 \cvarg{dst}{Destination image; will have the same size and the same type as \texttt{src}}
660 \cvarg{maxValue}{The non-zero value assigned to the pixels for which the condition is satisfied. See the discussion}
661 \cvarg{adaptiveMethod}{Adaptive thresholding algorithm to use,
662    \cvarg{ADAPTIVE\_THRESH\_MEAN\_C} or \cvarg{ADAPTIVE\_THRESH\_GAUSSIAN\_C} (see the discussion)}
663 \cvarg{thresholdType}{Thresholding type; must be one of \cvarg{THRESH\_BINARY} or \cvarg{THRESH\_BINARY\_INV}}
664 \cvarg{blockSize}{The size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on}
665 \cvarg{C}{The constant subtracted from the mean or weighted mean (see the discussion); normally, it's positive, but may be zero or negative as well}
666 \end{description}
667
668 The function transforms a grayscale image to a binary image according to the formulas:
669
670 \begin{description}
671 \cvarg{THRESH\_BINARY}{\[ dst(x,y) = \fork{\texttt{maxValue}}{if $src(x,y) > T(x,y)$}{0}{otherwise} \]}
672 \cvarg{THRESH\_BINARY\_INV}{\[ dst(x,y) = \fork{0}{if $src(x,y) > T(x,y)$}{\texttt{maxValue}}{otherwise} \]}
673 \end{description}
674
675 where $T(x,y)$ is a threshold calculated individually for each pixel.
676
677 \begin{enumerate}
678     \item
679 For the method \texttt{ADAPTIVE\_THRESH\_MEAN\_C} the threshold value $T(x,y)$ is the mean of a $\texttt{blockSize} \times \texttt{blockSize}$ neighborhood of $(x, y)$, minus \texttt{C}.
680     \item
681 For the method \texttt{ADAPTIVE\_THRESH\_GAUSSIAN\_C} the threshold value $T(x, y)$ is the weighted sum (i.e. cross-correlation with a Gaussian window) of a $\texttt{blockSize} \times \texttt{blockSize}$ neighborhood of $(x, y)$, minus \texttt{C}. The default sigma (standard deviation) is used for the specified \texttt{blockSize}, see \cvCppCross{getGaussianKernel}.
682 \end{enumerate}
683
684 The function can process the image in-place.
685
686 See also: \cvCppCross{threshold}, \cvCppCross{blur}, \cvCppCross{GaussianBlur}
687
688
689 \cvCppFunc{cvtColor}
690 Converts image from one color space to another
691
692 \cvdefCpp{void cvtColor( const Mat\& src, Mat\& dst, int code, int dstCn=0 );}
693 \begin{description}
694 \cvarg{src}{The source image, 8-bit unsigned, 16-bit unsigned (\texttt{CV\_16UC...}) or single-precision floating-point}
695 \cvarg{dst}{The destination image; will have the same size and the same depth as \texttt{src}}
696 \cvarg{code}{The color space conversion code; see the discussion}
697 \cvarg{dstCn}{The number of channels in the destination image; if the parameter is 0, the number of the channels will be derived automatically from \texttt{src} and the \texttt{code}}
698 \end{description}
699
700 The function converts the input image from one color
701 space to another. In the case of transformation to-from RGB color space the ordering of the channels should be specified explicitly (RGB or BGR).
702
703 The conventional ranges for R, G and B channel values are:
704
705 \begin{itemize}
706  \item 0 to 255 for \texttt{CV\_8U} images
707  \item 0 to 65535 for \texttt{CV\_16U} images and
708  \item 0 to 1 for \texttt{CV\_32F} images.
709 \end{itemize}
710
711 Of course, in the case of linear transformations the range does not matter,
712 but in the non-linear cases the input RGB image should be normalized to the proper value range in order to get the correct results, e.g. for RGB$\rightarrow$L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from 8-bit image without any scaling, then it will have 0..255 value range, instead of the assumed by the function 0..1. So, before calling \texttt{cvtColor}, you need first to scale the image down:
713 \begin{lstlisting}
714 img *= 1./255;
715 cvtColor(img, img, CV_BGR2Luv);
716 \end{lstlisting}
717
718 The function can do the following transformations:
719
720 \begin{itemize}
721  \item Transformations within RGB space like adding/removing the alpha channel, reversing the channel order, conversion to/from 16-bit RGB color (R5:G6:B5 or R5:G5:B5), as well as conversion to/from grayscale using:
722  \[
723  \text{RGB[A] to Gray:}\quad Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
724  \]
725  and
726  \[
727  \text{Gray to RGB[A]:}\quad R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow 0
728  \]
729
730 The conversion from a RGB image to gray is done with:
731 \begin{lstlisting}
732 cvtColor(src, bwsrc, CV_RGB2GRAY);
733 \end{lstlisting}
734
735 Some more advanced channel reordering can also be done with \cvCppCross{mixChannels}.
736
737  \item RGB $\leftrightarrow$ CIE XYZ.Rec 709 with D65 white point (\texttt{CV\_BGR2XYZ, CV\_RGB2XYZ, CV\_XYZ2BGR, CV\_XYZ2RGB}):
738  \[
739  \begin{bmatrix}
740  X \\
741  Y \\
742  Z
743  \end{bmatrix}
744  \leftarrow
745  \begin{bmatrix}
746 0.412453 & 0.357580 & 0.180423\\
747 0.212671 & 0.715160 & 0.072169\\
748 0.019334 & 0.119193 & 0.950227
749  \end{bmatrix}
750  \cdot
751  \begin{bmatrix}
752  R \\
753  G \\
754  B
755  \end{bmatrix}
756  \]
757  \[
758  \begin{bmatrix}
759  R \\
760  G \\
761  B
762  \end{bmatrix}
763  \leftarrow
764  \begin{bmatrix}
765 3.240479 & -1.53715 & -0.498535\\
766 -0.969256 &  1.875991 & 0.041556\\
767 0.055648 & -0.204043 & 1.057311
768  \end{bmatrix}
769  \cdot
770  \begin{bmatrix}
771  X \\
772  Y \\
773  Z
774  \end{bmatrix}
775  \]
776 $X$, $Y$ and $Z$ cover the whole value range (in the case of floating-point images $Z$ may exceed 1).
777
778  \item RGB $\leftrightarrow$ YCrCb JPEG (a.k.a. YCC) (\texttt{CV\_BGR2YCrCb, CV\_RGB2YCrCb, CV\_YCrCb2BGR, CV\_YCrCb2RGB})
779  \[ Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B \]
780  \[  Cr \leftarrow (R-Y) \cdot 0.713 + delta \]
781  \[  Cb \leftarrow (B-Y) \cdot 0.564 + delta \]
782  \[  R \leftarrow Y + 1.403 \cdot (Cr - delta) \]
783  \[  G \leftarrow Y - 0.344 \cdot (Cr - delta) - 0.714 \cdot (Cb - delta) \]
784  \[  B \leftarrow Y + 1.773 \cdot (Cb - delta) \]
785 where
786  \[
787   delta = \left\{
788   \begin{array}{l l}
789   128 & \mbox{for 8-bit images}\\
790   32768 & \mbox{for 16-bit images}\\
791   0.5 & \mbox{for floating-point images}
792   \end{array} \right.
793  \]
794 Y, Cr and Cb cover the whole value range.
795
796  \item RGB $\leftrightarrow$ HSV (\texttt{CV\_BGR2HSV, CV\_RGB2HSV, CV\_HSV2BGR, CV\_HSV2RGB})
797   in the case of 8-bit and 16-bit images
798   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range
799   \[ V \leftarrow max(R,G,B) \]
800
801 \[ S \leftarrow \fork{\frac{V-min(R,G,B)}{V}}{if $V \neq 0$}{0}{otherwise} \]
802 \[ H \leftarrow \forkthree
803 {{60(G - B)}/{S}}{if $V=R$}
804 {{120+60(B - R)}/{S}}{if $V=G$}
805 {{240+60(R - G)}/{S}}{if $V=B$} \]
806 if $H<0$ then $H \leftarrow H+360$
807
808 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
809
810 The values are then converted to the destination data type:
811 \begin{description}
812 \item[8-bit images]
813 \[ V \leftarrow 255 V, S \leftarrow 255 S, H \leftarrow H/2 \text{(to fit to 0 to 255)} \]
814 \item[16-bit images (currently not supported)]
815 \[ V <- 65535 V, S <- 65535 S, H <- H \]
816 \item[32-bit images]
817 H, S, V are left as is
818 \end{description}
819
820  \item RGB $\leftrightarrow$ HLS (\texttt{CV\_BGR2HLS, CV\_RGB2HLS, CV\_HLS2BGR, CV\_HLS2RGB}).
821   in the case of 8-bit and 16-bit images
822   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range.
823   \[ V_{max} \leftarrow {max}(R,G,B) \]
824   \[ V_{min} \leftarrow {min}(R,G,B) \]
825   \[ L \leftarrow \frac{V_{max} - V_{min}}{2} \]
826   \[ S \leftarrow \fork
827     {\frac{V_{max} - V_{min}}{V_{max} + V_{min}}}{if $L < 0.5$}
828     {\frac{V_{max} - V_{min}}{2 - (V_{max} + V_{min})}}{if $L \ge 0.5$} \]
829   \[ H \leftarrow \forkthree
830   {{60(G - B)}/{S}}{if $V_{max}=R$}
831   {{120+60(B - R)}/{S}}{if $V_{max}=G$}
832   {{240+60(R - G)}/{S}}{if $V_{max}=B$} \]
833   if $H<0$ then $H \leftarrow H+360$
834 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
835
836 The values are then converted to the destination data type:
837 \begin{description}
838 \item[8-bit images]
839 \[ V \leftarrow 255\cdot V, S \leftarrow 255\cdot S, H \leftarrow H/2\; \text{(to fit to 0 to 255)} \]
840 \item[16-bit images (currently not supported)]
841 \[ V <- 65535\cdot V, S <- 65535\cdot S, H <- H \]
842 \item[32-bit images]
843 H, S, V are left as is
844 \end{description}
845
846  \item RGB $\leftrightarrow$ CIE L*a*b* (\texttt{CV\_BGR2Lab, CV\_RGB2Lab, CV\_Lab2BGR, CV\_Lab2RGB})
847   in the case of 8-bit and 16-bit images
848   R, G and B are converted to floating-point format and scaled to fit the 0 to 1 range
849 \[ \vecthree{X}{Y}{Z} \leftarrow \vecthreethree
850 {0.412453}{0.357580}{0.180423}
851 {0.212671}{0.715160}{0.072169}
852 {0.019334}{0.119193}{0.950227}
853 \cdot
854 \vecthree{R}{G}{B} \]
855 \[ X \leftarrow X/X_n, \text{where} X_n = 0.950456 \]
856 \[ Z \leftarrow Z/Z_n, \text{where} Z_n = 1.088754 \]
857 \[ L \leftarrow \fork
858 {116*Y^{1/3}-16}{for $Y>0.008856$}
859 {903.3*Y}{for $Y \le 0.008856$} \]
860 \[ a \leftarrow 500 (f(X)-f(Y)) + delta \]
861 \[ b \leftarrow 200 (f(Y)-f(Z)) + delta \]
862 where
863 \[f(t)=\fork
864 {t^{1/3}}{for $t>0.008856$}
865 {7.787 t+16/116}{for $t\leq 0.008856$} \]
866 and
867 \[ delta = \fork{128}{for 8-bit images}{0}{for floating-point images} \]
868 On output $0 \leq L \leq 100$, $-127 \leq a \leq 127$, $-127 \leq b \leq 127$
869
870 The values are then converted to the destination data type:
871 \begin{description}
872 \item[8-bit images]
873 \[L \leftarrow L*255/100,\; a \leftarrow a + 128,\; b \leftarrow b + 128\]
874 \item[16-bit images] currently not supported
875 \item[32-bit images]
876 L, a, b are left as is
877 \end{description}
878
879  \item RGB $\leftrightarrow$ CIE L*u*v* (\texttt{CV\_BGR2Luv, CV\_RGB2Luv, CV\_Luv2BGR, CV\_Luv2RGB})
880   in the case of 8-bit and 16-bit images
881   R, G and B are converted to floating-point format and scaled to fit 0 to 1 range
882   \[ \vecthree{X}{Y}{Z} \leftarrow \vecthreethree
883 {0.412453}{0.357580}{0.180423}
884 {0.212671}{0.715160}{0.072169}
885 {0.019334}{0.119193}{0.950227}
886 \cdot
887 \vecthree{R}{G}{B} \]
888 \[ L \leftarrow \fork
889 {116 Y^{1/3}}{for $Y>0.008856$}
890 {903.3 Y}{for $Y\leq 0.008856$} \]
891 \[ u' \leftarrow 4*X/(X + 15*Y + 3 Z) \]
892 \[ v' \leftarrow 9*Y/(X + 15*Y + 3 Z) \]
893 \[ u \leftarrow 13*L*(u' - u_n) \quad \text{where} \quad u_n=0.19793943 \]
894 \[ v \leftarrow 13*L*(v' - v_n) \quad \text{where} \quad v_n=0.46831096 \]
895 On output $0 \leq L \leq 100$, $-134 \leq u \leq 220$, $-140 \leq v \leq 122$.
896
897 The values are then converted to the destination data type:
898 \begin{description}
899 \item[8-bit images]
900 \[L \leftarrow 255/100 L,\; u \leftarrow 255/354 (u + 134),\; v \leftarrow 255/256 (v + 140) \]
901 \item[16-bit images] currently not supported
902 \item[32-bit images] L, u, v are left as is
903 \end{description}
904
905 The above formulas for converting RGB to/from various color spaces have been taken from multiple sources on Web, primarily from the Charles Poynton site \url{http://www.poynton.com/ColorFAQ.html}
906
907  \item Bayer $\rightarrow$ RGB (\texttt{CV\_BayerBG2BGR, CV\_BayerGB2BGR, CV\_BayerRG2BGR, CV\_BayerGR2BGR, CV\_BayerBG2RGB, CV\_BayerGB2RGB, CV\_BayerRG2RGB, CV\_BayerGR2RGB}) The Bayer pattern is widely used in CCD and CMOS cameras. It allows one to get color pictures from a single plane where R,G and B pixels (sensors of a particular component) are interleaved like this:
908
909 \newcommand{\Rcell}{\color{red}R}
910 \newcommand{\Gcell}{\color{green}G}
911 \newcommand{\Bcell}{\color{blue}B}
912
913
914 \[
915 \definecolor{BackGray}{rgb}{0.8,0.8,0.8}
916 \begin{array}{ c c c c c }
917 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell\\
918 \Gcell&\colorbox{BackGray}{\Bcell}&\colorbox{BackGray}{\Gcell}&\Bcell&\Gcell\\
919 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell\\
920 \Gcell&\Bcell&\Gcell&\Bcell&\Gcell\\
921 \Rcell&\Gcell&\Rcell&\Gcell&\Rcell
922 \end{array}
923 \]
924
925 The output RGB components of a pixel are interpolated from 1, 2 or
926 4 neighbors of the pixel having the same color. There are several
927 modifications of the above pattern that can be achieved by shifting
928 the pattern one pixel left and/or one pixel up. The two letters
929 $C_1$ and $C_2$
930 in the conversion constants
931 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2BGR}
932 and
933 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2RGB}
934 indicate the particular pattern
935 type - these are components from the second row, second and third
936 columns, respectively. For example, the above pattern has very
937 popular "BG" type.
938 \end{itemize}
939
940
941
942 \cvCppFunc{distanceTransform}
943 Calculates the distance to the closest zero pixel for each pixel of the source image.
944
945 \cvdefCpp{void distanceTransform( const Mat\& src, Mat\& dst,\par
946                         int distanceType, int maskSize );\newline
947 void distanceTransform( const Mat\& src, Mat\& dst, Mat\& labels,\par
948                         int distanceType, int maskSize );}
949 \begin{description}
950 \cvarg{src}{8-bit, single-channel (binary) source image}
951 \cvarg{dst}{Output image with calculated distances; will be 32-bit floating-point, single-channel image of the same size as \texttt{src}}
952 \cvarg{distanceType}{Type of distance; can be \texttt{CV\_DIST\_L1, CV\_DIST\_L2} or \texttt{CV\_DIST\_C}}
953 \cvarg{maskSize}{Size of the distance transform mask; can be 3, 5 or \texttt{CV\_DIST\_MASK\_PRECISE} (the latter option is only supported by the first of the functions). In the case of \texttt{CV\_DIST\_L1} or \texttt{CV\_DIST\_C} distance type the parameter is forced to 3, because a $3\times 3$ mask gives the same result as a $5\times 5$ or any larger aperture.}
954 \cvarg{labels}{The optional output 2d array of labels - the discrete Voronoi diagram; will have type \texttt{CV\_32SC1} and the same size as \texttt{src}. See the discussion}
955 \end{description}
956
957 The functions \texttt{distanceTransform} calculate the approximate or precise
958 distance from every binary image pixel to the nearest zero pixel.
959 (for zero image pixels the distance will obviously be zero).
960
961 When \texttt{maskSize == CV\_DIST\_MASK\_PRECISE} and \texttt{distanceType == CV\_DIST\_L2}, the function runs the algorithm described in \cite{Felzenszwalb04}.
962
963 In other cases the algorithm \cite{Borgefors86} is used, that is,
964 for pixel the function finds the shortest path to the nearest zero pixel
965 consisting of basic shifts: horizontal,
966 vertical, diagonal or knight's move (the latest is available for a
967 $5\times 5$ mask). The overall distance is calculated as a sum of these
968 basic distances. Because the distance function should be symmetric,
969 all of the horizontal and vertical shifts must have the same cost (that
970 is denoted as \texttt{a}), all the diagonal shifts must have the
971 same cost (denoted \texttt{b}), and all knight's moves must have
972 the same cost (denoted \texttt{c}). For \texttt{CV\_DIST\_C} and
973 \texttt{CV\_DIST\_L1} types the distance is calculated precisely,
974 whereas for \texttt{CV\_DIST\_L2} (Euclidian distance) the distance
975 can be calculated only with some relative error (a $5\times 5$ mask
976 gives more accurate results). For \texttt{a}, \texttt{b} and \texttt{c}
977 OpenCV uses the values suggested in the original paper:
978
979
980 \begin{tabular}{| c | c | c |}
981 \hline
982 \texttt{CV\_DIST\_C}  & $(3\times 3)$ & a = 1, b = 1\\ \hline
983 \texttt{CV\_DIST\_L1} & $(3\times 3)$ & a = 1, b = 2\\ \hline
984 \texttt{CV\_DIST\_L2} & $(3\times 3)$ & a=0.955, b=1.3693\\ \hline
985 \texttt{CV\_DIST\_L2} & $(5\times 5)$ & a=1, b=1.4, c=2.1969\\ \hline
986 \end{tabular}
987
988
989 Typically, for a fast, coarse distance estimation \texttt{CV\_DIST\_L2},
990 a $3\times 3$ mask is used, and for a more accurate distance estimation
991 \texttt{CV\_DIST\_L2}, a $5\times 5$ mask or the precise algorithm is used.
992 Note that both the precise and the approximate algorithms are linear on the number of pixels.
993
994 The second variant of the function does not only compute the minimum distance for each pixel $(x, y)$,
995 but it also identifies the nearest the nearest connected
996 component consisting of zero pixels. Index of the component is stored in $\texttt{labels}(x, y)$.
997 The connected components of zero pixels are also found and marked by the function.
998
999 In this mode the complexity is still linear.
1000 That is, the function provides a very fast way to compute Voronoi diagram for the binary image.
1001 Currently, this second variant can only use the approximate distance transform algorithm.
1002
1003
1004 \cvCppFunc{floodFill}
1005 Fills a connected component with the given color.
1006
1007 \cvdefCpp{int floodFill( Mat\& image,\par
1008                Point seed, Scalar newVal, Rect* rect=0,\par
1009                Scalar loDiff=Scalar(), Scalar upDiff=Scalar(),\par
1010                int flags=4 );\newline
1011 int floodFill( Mat\& image, Mat\& mask,\par
1012                Point seed, Scalar newVal, Rect* rect=0,\par
1013                Scalar loDiff=Scalar(), Scalar upDiff=Scalar(),\par
1014                int flags=4 );}
1015 \begin{description}
1016 \cvarg{image}{Input/output 1- or 3-channel, 8-bit or floating-point image. It is modified by the function unless the \texttt{FLOODFILL\_MASK\_ONLY} flag is set (in the second variant of the function; see below)}
1017 \cvarg{mask}{(For the second function only) Operation mask, should be a single-channel 8-bit image, 2 pixels wider and 2 pixels taller. The function uses and updates the mask, so the user takes responsibility of initializing the \texttt{mask} content. Flood-filling can't go across non-zero pixels in the mask, for example, an edge detector output can be used as a mask to stop filling at edges. It is possible to use the same mask in multiple calls to the function to make sure the filled area do not overlap. \textbf{Note}: because the mask is larger than the filled image, a pixel $(x, y)$ in \texttt{image} will correspond to the pixel $(x+1, y+1)$ in the \texttt{mask}}
1018 \cvarg{seed}{The starting point}
1019 \cvarg{newVal}{New value of the repainted domain pixels}
1020 \cvarg{loDiff}{Maximal lower brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component}
1021 \cvarg{upDiff}{Maximal upper brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component}
1022 \cvarg{rect}{The optional output parameter that the function sets to the minimum bounding rectangle of the repainted domain}
1023 \cvarg{flags}{The operation flags. Lower bits contain connectivity value, 4 (by default) or 8, used within the function. Connectivity determines which neighbors of a pixel are considered. Upper bits can be 0 or a combination of the following flags:
1024 \begin{description}
1025   \cvarg{FLOODFILL\_FIXED\_RANGE}{if set, the difference between the current pixel and seed pixel is considered, otherwise the difference between neighbor pixels is considered (i.e. the range is floating)}
1026   \cvarg{FLOODFILL\_MASK\_ONLY}{(for the second variant only) if set, the function does not change the image (\texttt{newVal} is ignored), but fills the mask}
1027 \end{description}}
1028 \end{description}
1029
1030 The functions \texttt{floodFill} fill a connected component starting from the seed point with the specified color. The connectivity is determined by the color/brightness closeness of the neighbor pixels. The pixel at $(x,y)$ is considered to belong to the repainted domain if:
1031
1032 \begin{description}
1033
1034 \item[grayscale image, floating range] \[
1035 \texttt{src}(x',y')-\texttt{loDiff} \leq \texttt{src}(x,y) \leq \texttt{src}(x',y')+\texttt{upDiff} \]
1036
1037 \item[grayscale image, fixed range] \[
1038 \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)-\texttt{loDiff}\leq \texttt{src}(x,y) \leq \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)+\texttt{upDiff} \]
1039
1040 \item[color image, floating range]
1041 \[ \texttt{src}(x',y')_r-\texttt{loDiff}_r\leq \texttt{src}(x,y)_r\leq \texttt{src}(x',y')_r+\texttt{upDiff}_r \]
1042 \[ \texttt{src}(x',y')_g-\texttt{loDiff}_g\leq \texttt{src}(x,y)_g\leq \texttt{src}(x',y')_g+\texttt{upDiff}_g \]
1043 \[ \texttt{src}(x',y')_b-\texttt{loDiff}_b\leq \texttt{src}(x,y)_b\leq \texttt{src}(x',y')_b+\texttt{upDiff}_b \]
1044
1045 \item[color image, fixed range]
1046 \[ \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_r-\texttt{loDiff}_r\leq \texttt{src}(x,y)_r\leq \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_r+\texttt{upDiff}_r \]
1047 \[ \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_g-\texttt{loDiff}_g\leq \texttt{src}(x,y)_g\leq \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_g+\texttt{upDiff}_g \]
1048 \[ \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_b-\texttt{loDiff}_b\leq \texttt{src}(x,y)_b\leq \texttt{src}(\texttt{seed}.x,\texttt{seed}.y)_b+\texttt{upDiff}_b \]
1049 \end{description}
1050
1051 where $src(x',y')$ is the value of one of pixel neighbors that is already known to belong to the component. That is, to be added to the connected component, a pixel's color/brightness should be close enough to the:
1052 \begin{itemize}
1053   \item color/brightness of one of its neighbors that are already referred to the connected component in the case of floating range
1054   \item color/brightness of the seed point in the case of fixed range.
1055 \end{itemize}
1056
1057 By using these functions you can either mark a connected component with the specified color in-place, or build a mask and then extract the contour or copy the region to another image etc. Various modes of the function are demonstrated in \texttt{floodfill.c} sample.
1058
1059 See also: \cvCppCross{findContours}
1060
1061
1062 \cvCppFunc{inpaint}
1063 Inpaints the selected region in the image.
1064
1065 \cvdefCpp{void inpaint( const Mat\& src, const Mat\& inpaintMask,\par
1066               Mat\& dst, double inpaintRadius, int flags );}
1067
1068 \begin{description}
1069 \cvarg{src}{The input 8-bit 1-channel or 3-channel image.}
1070 \cvarg{inpaintMask}{The inpainting mask, 8-bit 1-channel image. Non-zero pixels indicate the area that needs to be inpainted.}
1071 \cvarg{dst}{The output image; will have the same size and the same type as \texttt{src}}
1072 \cvarg{inpaintRadius}{The radius of a circlular neighborhood of each point inpainted that is considered by the algorithm.}
1073 \cvarg{flags}{The inpainting method, one of the following:
1074 \begin{description}
1075 \cvarg{INPAINT\_NS}{Navier-Stokes based method.}
1076 \cvarg{INPAINT\_TELEA}{The method by Alexandru Telea \cite{Telea04}}
1077 \end{description}}
1078 \end{description}
1079
1080 The function reconstructs the selected image area from the pixel near the area boundary. The function may be used to remove dust and scratches from a scanned photo, or to remove undesirable objects from still images or video. See \url{http://en.wikipedia.org/wiki/Inpainting} for more details.
1081
1082
1083 \cvCppFunc{integral}
1084 Calculates the integral of an image.
1085
1086 \cvdefCpp{void integral( const Mat\& image, Mat\& sum, int sdepth=-1 );\newline
1087 void integral( const Mat\& image, Mat\& sum, Mat\& sqsum, int sdepth=-1 );\newline
1088 void integral( const Mat\& image, Mat\& sum, \par Mat\& sqsum, Mat\& tilted, int sdepth=-1 );}
1089 \begin{description}
1090 \cvarg{image}{The source image, $W \times H$, 8-bit or floating-point (32f or 64f)}
1091 \cvarg{sum}{The integral image, $(W+1)\times (H+1)$, 32-bit integer or floating-point (32f or 64f)}
1092 \cvarg{sqsum}{The integral image for squared pixel values, $(W+1)\times (H+1)$, double precision floating-point (64f)}
1093 \cvarg{tilted}{The integral for the image rotated by 45 degrees, $(W+1)\times (H+1)$, the same data type as \texttt{sum}}
1094 \cvarg{sdepth}{The desired depth of the integral and the tilted integral images, \texttt{CV\_32S},  \texttt{CV\_32F} or \texttt{CV\_64F}}
1095 \end{description}
1096
1097 The functions \texttt{integral} calculate one or more integral images for the source image as following:
1098
1099 \[
1100 \texttt{sum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)
1101 \]
1102
1103 \[
1104 \texttt{sqsum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)^2
1105 \]
1106
1107 \[
1108 \texttt{tilted}(X,Y) = \sum_{y<Y,abs(x-X)<y} \texttt{image}(x,y)
1109 \]
1110
1111 Using these integral images, one may calculate sum, mean and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
1112
1113 \[
1114 \sum_{x_1\leq x < x_2, \, y_1 \leq y < y_2} \texttt{image}(x,y) = \texttt{sum}(x_2,y_2)-\texttt{sum}(x_1,y_2)-\texttt{sum}(x_2,y_1)+\texttt{sum}(x_1,x_1)
1115 \]
1116
1117 It makes possible to do a fast blurring or fast block correlation with variable window size, for example. In the case of multi-channel images, sums for each channel are accumulated independently.
1118
1119
1120 \cvCppFunc{threshold}
1121 Applies a fixed-level threshold to each array element
1122
1123 \cvdefCpp{double threshold( const Mat\& src, Mat\& dst, double thresh,\par
1124                   double maxVal, int thresholdType );}
1125 \begin{description}
1126 \cvarg{src}{Source array (single-channel, 8-bit of 32-bit floating point)}
1127 \cvarg{dst}{Destination array; will have the same size and the same type as \texttt{src}}
1128 \cvarg{thresh}{Threshold value}
1129 \cvarg{maxVal}{Maximum value to use with \texttt{THRESH\_BINARY} and \texttt{THRESH\_BINARY\_INV} thresholding types}
1130 \cvarg{thresholdType}{Thresholding type (see the discussion)}
1131 \end{description}
1132
1133 The function applies fixed-level thresholding
1134 to a single-channel array. The function is typically used to get a
1135 bi-level (binary) image out of a grayscale image (\cvCppCross{compare} could
1136 be also used for this purpose) or for removing a noise, i.e. filtering
1137 out pixels with too small or too large values. There are several
1138 types of thresholding that the function supports that are determined by
1139 \texttt{thresholdType}:
1140
1141 \begin{description}
1142 \cvarg{THRESH\_BINARY}{\[ \texttt{dst}(x,y) = \fork{\texttt{maxVal}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise} \]}
1143 \cvarg{THRESH\_BINARY\_INV}{\[ \texttt{dst}(x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{maxVal}}{otherwise} \]}
1144 \cvarg{THRESH\_TRUNC}{\[ \texttt{dst}(x,y) = \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise} \]}
1145 \cvarg{THRESH\_TOZERO}{\[ \texttt{dst}(x,y) = \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise} \]}
1146 \cvarg{THRESH\_TOZERO\_INV}{\[ \texttt{dst}(x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise} \]}
1147 \end{description}
1148
1149 Also, the special value \texttt{THRESH\_OTSU} may be combined with
1150 one of the above values. In this case the function determines the optimal threshold
1151 value using Otsu's algorithm and uses it instead of the specified \texttt{thresh}.
1152 The function returns the computed threshold value.
1153 Currently, Otsu's method is implemented only for 8-bit images.
1154
1155 \includegraphics[width=0.5\textwidth]{pics/threshold.png}
1156
1157 See also: \cvCppCross{adaptiveThreshold}, \cvCppCross{findContours}, \cvCppCross{compare}, \cvCppCross{min}, \cvCppCross{max}
1158
1159 \cvCppFunc{watershed}
1160 Does marker-based image segmentation using watershed algrorithm
1161
1162 \cvdefCpp{void watershed( const Mat\& image, Mat\& markers );}
1163 \begin{description}
1164 \cvarg{image}{The input 8-bit 3-channel image.} 
1165 \cvarg{markers}{The input/output 32-bit single-channel image (map) of markers. It should have the same size as \texttt{image}}
1166 \end{description}
1167
1168 The function implements one of the variants
1169 of watershed, non-parametric marker-based segmentation algorithm,
1170 described in \cite{Meyer92}. Before passing the image to the
1171 function, user has to outline roughly the desired regions in the image
1172 \texttt{markers} with positive ($>0$) indices, i.e. every region is
1173 represented as one or more connected components with the pixel values
1174 1, 2, 3 etc (such markers can be retrieved from a binary mask
1175 using \cvCppCross{findContours}and \cvCppCross{drawContours}, see \texttt{watershed.cpp} demo).
1176 The markers will be "seeds" of the future image
1177 regions. All the other pixels in \texttt{markers}, which relation to the
1178 outlined regions is not known and should be defined by the algorithm,
1179 should be set to 0's. On the output of the function, each pixel in
1180 markers is set to one of values of the "seed" components, or to -1 at
1181 boundaries between the regions.
1182
1183 Note, that it is not necessary that every two neighbor connected
1184 components are separated by a watershed boundary (-1's pixels), for
1185 example, in case when such tangent components exist in the initial
1186 marker image. Visual demonstration and usage example of the function
1187 can be found in OpenCV samples directory; see \texttt{watershed.cpp} demo.
1188
1189 See also: \cvCppCross{findContours}
1190
1191 \fi