1 \section{Miscellaneous Image Transformations}
5 \cvCPyFunc{AdaptiveThreshold}
6 Applies an adaptive threshold to an array.
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}
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
23 \cvarg{CV\_THRESH\_BINARY}{xxx}
24 \cvarg{CV\_THRESH\_BINARY\_INV}{xxx}
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}
30 The function transforms a grayscale image to a binary image according to the formulas:
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} \]}
37 where $T(x,y)$ is a threshold calculated individually for each pixel.
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}.
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}.
44 Converts an image from one color space to another.
48 \par const CvArr* src,
51 }\cvdefPy{CvtColor(src,dst,code)-> None}
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)}
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, ...$
68 The conventional range for R,G,B channel values is:
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.
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.
80 The function can do the following transformations:
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:
85 \text{RGB[A] to Gray:} Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
89 \text{Gray to RGB[A]:} R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow 0
92 The conversion from a RGB image to gray is done with:
94 cvCvtColor(src ,bwsrc, CV_RGB2GRAY)
97 \item RGB $\leftrightarrow$ CIE XYZ.Rec 709 with D65 white point (\texttt{CV\_BGR2XYZ, CV\_RGB2XYZ, CV\_XYZ2BGR, CV\_XYZ2RGB}):
106 0.412453 & 0.357580 & 0.180423\\
107 0.212671 & 0.715160 & 0.072169\\
108 0.019334 & 0.119193 & 0.950227
125 3.240479 & -1.53715 & -0.498535\\
126 -0.969256 & 1.875991 & 0.041556\\
127 0.055648 & -0.204043 & 1.057311
136 $X$, $Y$ and $Z$ cover the whole value range (in the case of floating-point images $Z$ may exceed 1).
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) \]
149 128 & \mbox{for 8-bit images}\\
150 32768 & \mbox{for 16-bit images}\\
151 0.5 & \mbox{for floating-point images}
154 Y, Cr and Cb cover the whole value range.
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) \]
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$
168 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
170 The values are then converted to the destination data type:
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 \]
177 H, S, V are left as is
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$.
196 The values are then converted to the destination data type:
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 \]
203 H, S, V are left as is
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}
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 \]
224 {t^{1/3}}{for $t>0.008856$}
225 {7.787 t+16/116}{for $t<=0.008856$} \]
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$
230 The values are then converted to the destination data type:
233 \[L \leftarrow L*255/100, a \leftarrow a + 128, b \leftarrow b + 128\]
234 \item[16-bit images] currently not supported
236 L, a, b are left as is
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}
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$.
257 The values are then converted to the destination data type:
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
265 The above formulas for converting RGB to/from various color spaces have been taken from multiple sources on Web, primarily from
267 at the Charles Poynton site.
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:
271 \newcommand{\Rcell}{\color{red}R}
272 \newcommand{\Gcell}{\color{green}G}
273 \newcommand{\Bcell}{\color{blue}B}
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
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
292 in the conversion constants
293 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2BGR}
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
302 \cvCPyFunc{DistTransform}
303 Calculates the distance to the closest zero pixel for all non-zero pixels of the source image.
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}
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}}
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
336 \begin{tabular}{| c | c | c |}
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
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:
346 User-defined $3 \times 3$ mask (a=1, b=1.5)
348 \begin{tabular}{| c | c | c | c | c | c | c |}
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
359 User-defined $5 \times 5$ mask (a=1, b=1.5, c=2)
361 \begin{tabular}{| c | c | c | c | c | c | c |}
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
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.
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.
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.
386 \cvCPyFunc{FloodFill}
387 Fills a connected component with the given color.
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 );
394 }\cvdefPy{FloodFill(image,seed\_point,new\_val,lo\_diff=cvScalarAll(0),up\_diff=cvScalarAll(0),flags=4,mask=NULL)-> comp}
398 typedef struct CvConnectedComp
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) */
407 #define CV_FLOODFILL_FIXED_RANGE (1 << 16)
408 #define CV_FLOODFILL_MASK_ONLY (1 << 17)
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:
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)}
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)$ }
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:
431 \item[grayscale image, floating range] \[
432 src(x',y')-\texttt{lo\_diff} <= src(x,y) <= src(x',y')+\texttt{up\_diff} \]
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} \]
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 \]
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 \]
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:
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.
455 Inpaints the selected region in the image.
458 void cvInpaint( \par const CvArr* src, \par const CvArr* mask, \par CvArr* dst,
459 \par double inpaintRadius, \par int flags);
461 }\cvdefPy{Inpaint(src,mask,dst,inpaintRadius,flags) -> None}
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:
470 \cvarg{CV\_INPAINT\_NS}{Navier-Stokes based method.}
471 \cvarg{CV\_INPAINT\_TELEA}{The method by Alexandru Telea \href{\#Telea04}{[Telea04]}}
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.
478 Calculates the integral of an image.
482 \par const CvArr* image,
484 \par CvArr* sqsum=NULL,
485 \par CvArr* tiltedSum=NULL );
486 }\cvdefPy{Integral(image,sum,sqsum=NULL,tiltedSum=NULL)-> None}
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}}
495 The function calculates one or more integral images for the source image as following:
498 \texttt{sum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)
502 \texttt{sqsum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)^2
506 \texttt{tiltedSum}(X,Y) = \sum_{y<Y,abs(x-X)<y} \texttt{image}(x,y)
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:
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)
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.
518 \cvCPyFunc{PyrMeanShiftFiltering}
519 Does meanshift image segmentation
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));
527 }\cvdefPy{PyrMeanShiftFiltering(src,dst,sp,sr,max\_level=1,
528 termcrit=\par (CV\_TERMCRIT\_ITER+CV\_TERMCRIT\_EPS,5,1))-> None}
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.}
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:
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}
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:
553 $(X,Y)~(X',Y'), (R,G,B)~(R',G',B').$
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):
557 $I(X,Y) <- (R*,G*,B*)$
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$ ).
569 \cvCPyFunc{PyrSegmentation}
570 Implements image segmentation by pyramids.
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}
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}
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
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).
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.
602 \cvCPyFunc{Threshold}
603 Applies a fixed-level threshold to array elements.
607 \par const CvArr* src,
609 \par double threshold,
610 \par double maxValue,
611 \par int thresholdType );
612 }\cvdefPy{Threshold(src,dst,threshld,maxValue,thresholdType)-> None}
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)}
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}:
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} \]}
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.
644 \includegraphics[width=0.5\textwidth]{pics/threshold.png}
651 \cvCppFunc{adaptiveThreshold}
652 Applies an adaptive threshold to an array.
654 \cvdefCpp{void adaptiveThreshold( const Mat\& src, Mat\& dst, double maxValue,\par
655 int adaptiveMethod, int thresholdType,\par
656 int blockSize, double C );}
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}
668 The function transforms a grayscale image to a binary image according to the formulas:
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} \]}
675 where $T(x,y)$ is a threshold calculated individually for each pixel.
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}.
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}.
684 The function can process the image in-place.
686 See also: \cvCppCross{threshold}, \cvCppCross{blur}, \cvCppCross{GaussianBlur}
690 Converts image from one color space to another
692 \cvdefCpp{void cvtColor( const Mat\& src, Mat\& dst, int code, int dstCn=0 );}
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}}
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).
703 The conventional ranges for R, G and B channel values are:
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.
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:
715 cvtColor(img, img, CV_BGR2Luv);
718 The function can do the following transformations:
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:
723 \text{RGB[A] to Gray:}\quad Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
727 \text{Gray to RGB[A]:}\quad R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow 0
730 The conversion from a RGB image to gray is done with:
732 cvtColor(src, bwsrc, CV_RGB2GRAY);
735 Some more advanced channel reordering can also be done with \cvCppCross{mixChannels}.
737 \item RGB $\leftrightarrow$ CIE XYZ.Rec 709 with D65 white point (\texttt{CV\_BGR2XYZ, CV\_RGB2XYZ, CV\_XYZ2BGR, CV\_XYZ2RGB}):
746 0.412453 & 0.357580 & 0.180423\\
747 0.212671 & 0.715160 & 0.072169\\
748 0.019334 & 0.119193 & 0.950227
765 3.240479 & -1.53715 & -0.498535\\
766 -0.969256 & 1.875991 & 0.041556\\
767 0.055648 & -0.204043 & 1.057311
776 $X$, $Y$ and $Z$ cover the whole value range (in the case of floating-point images $Z$ may exceed 1).
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) \]
789 128 & \mbox{for 8-bit images}\\
790 32768 & \mbox{for 16-bit images}\\
791 0.5 & \mbox{for floating-point images}
794 Y, Cr and Cb cover the whole value range.
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) \]
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$
808 On output $0 \leq V \leq 1$, $0 \leq S \leq 1$, $0 \leq H \leq 360$.
810 The values are then converted to the destination data type:
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 \]
817 H, S, V are left as is
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$.
836 The values are then converted to the destination data type:
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 \]
843 H, S, V are left as is
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}
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 \]
864 {t^{1/3}}{for $t>0.008856$}
865 {7.787 t+16/116}{for $t\leq 0.008856$} \]
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$
870 The values are then converted to the destination data type:
873 \[L \leftarrow L*255/100,\; a \leftarrow a + 128,\; b \leftarrow b + 128\]
874 \item[16-bit images] currently not supported
876 L, a, b are left as is
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}
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$.
897 The values are then converted to the destination data type:
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
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}
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:
909 \newcommand{\Rcell}{\color{red}R}
910 \newcommand{\Gcell}{\color{green}G}
911 \newcommand{\Bcell}{\color{blue}B}
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
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
930 in the conversion constants
931 \texttt{CV\_Bayer} $ C_1 C_2 $ \texttt{2BGR}
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
942 \cvCppFunc{distanceTransform}
943 Calculates the distance to the closest zero pixel for each pixel of the source image.
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 );}
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}
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).
961 When \texttt{maskSize == CV\_DIST\_MASK\_PRECISE} and \texttt{distanceType == CV\_DIST\_L2}, the function runs the algorithm described in \cite{Felzenszwalb04}.
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:
980 \begin{tabular}{| c | c | c |}
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
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.
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.
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.
1004 \cvCppFunc{floodFill}
1005 Fills a connected component with the given color.
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
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:
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}
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:
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} \]
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} \]
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 \]
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 \]
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:
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.
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.
1059 See also: \cvCppCross{findContours}
1063 Inpaints the selected region in the image.
1065 \cvdefCpp{void inpaint( const Mat\& src, const Mat\& inpaintMask,\par
1066 Mat\& dst, double inpaintRadius, int flags );}
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:
1075 \cvarg{INPAINT\_NS}{Navier-Stokes based method.}
1076 \cvarg{INPAINT\_TELEA}{The method by Alexandru Telea \cite{Telea04}}
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.
1083 \cvCppFunc{integral}
1084 Calculates the integral of an image.
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 );}
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}}
1097 The functions \texttt{integral} calculate one or more integral images for the source image as following:
1100 \texttt{sum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)
1104 \texttt{sqsum}(X,Y) = \sum_{x<X,y<Y} \texttt{image}(x,y)^2
1108 \texttt{tilted}(X,Y) = \sum_{y<Y,abs(x-X)<y} \texttt{image}(x,y)
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:
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)
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.
1120 \cvCppFunc{threshold}
1121 Applies a fixed-level threshold to each array element
1123 \cvdefCpp{double threshold( const Mat\& src, Mat\& dst, double thresh,\par
1124 double maxVal, int thresholdType );}
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)}
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}:
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} \]}
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.
1155 \includegraphics[width=0.5\textwidth]{pics/threshold.png}
1157 See also: \cvCppCross{adaptiveThreshold}, \cvCppCross{findContours}, \cvCppCross{compare}, \cvCppCross{min}, \cvCppCross{max}
1159 \cvCppFunc{watershed}
1160 Does marker-based image segmentation using watershed algrorithm
1162 \cvdefCpp{void watershed( const Mat\& image, Mat\& markers );}
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}}
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.
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.
1189 See also: \cvCppCross{findContours}