2011-02-23 04:43:26 +08:00
Miscellaneous Image Transformations
===================================
2011-04-19 19:41:12 +08:00
.. highlight :: cpp
2011-03-09 06:22:24 +08:00
2011-03-01 05:26:43 +08:00
adaptiveThreshold
2011-02-23 04:43:26 +08:00
---------------------
2011-06-30 06:06:42 +08:00
Applies an adaptive threshold to an array.
2011-06-16 20:48:23 +08:00
.. ocv:function :: void adaptiveThreshold( InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C )
2011-02-23 04:43:26 +08:00
2011-06-30 06:06:42 +08:00
.. ocv:pyfunction :: cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst
2012-05-28 15:36:14 +08:00
.. ocv:cfunction :: void cvAdaptiveThreshold( const CvArr* src, CvArr* dst, double max_value, int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, int threshold_type=CV_THRESH_BINARY, int block_size=3, double param1=5 )
2011-04-25 05:02:14 +08:00
:param src: Source 8-bit single-channel image.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param dst: Destination image of the same size and the same type as ``src`` .
2012-05-28 15:36:14 +08:00
2011-04-25 05:02:14 +08:00
:param maxValue: Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param adaptiveMethod: Adaptive thresholding algorithm to use, ``ADAPTIVE_THRESH_MEAN_C`` or ``ADAPTIVE_THRESH_GAUSSIAN_C`` . See the details below.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param thresholdType: Thresholding type that must be either ``THRESH_BINARY`` or ``THRESH_BINARY_INV`` .
2012-05-28 15:36:14 +08:00
2011-04-25 05:02:14 +08:00
:param blockSize: Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param C: Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
The function transforms a grayscale image to a binary image according to the formulae:
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_BINARY**
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
dst(x,y) = \fork{\texttt{maxValue}}{if $src(x,y) > T(x,y)$}{0}{otherwise}
* **THRESH_BINARY_INV**
.. math ::
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
dst(x,y) = \fork{0}{if $src(x,y) > T(x,y)$}{\texttt{maxValue}}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
where
:math: `T(x,y)` is a threshold calculated individually for each pixel.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
*
For the method `` ADAPTIVE_THRESH_MEAN_C `` , the threshold value
:math: `T(x,y)` is a mean of the
2011-02-26 19:05:10 +08:00
:math: `\texttt{blockSize} \times \texttt{blockSize}` neighborhood of
2011-04-25 05:02:14 +08:00
:math: `(x, y)` minus `` C `` .
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
*
For the method `` ADAPTIVE_THRESH_GAUSSIAN_C `` , the threshold value
:math: `T(x, y)` is a weighted sum (cross-correlation with a Gaussian window) of the
2011-02-26 19:05:10 +08:00
:math: `\texttt{blockSize} \times \texttt{blockSize}` neighborhood of
2011-04-25 05:02:14 +08:00
:math: `(x, y)` minus `` C `` . The default sigma (standard deviation) is used for the specified `` blockSize `` . See
2011-06-16 20:48:23 +08:00
:ocv:func: `getGaussianKernel` .
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
The function can process the image in-place.
2011-02-23 04:43:26 +08:00
2011-06-30 20:06:26 +08:00
.. seealso ::
:ocv:func: `threshold` ,
:ocv:func: `blur` ,
:ocv:func: `GaussianBlur`
2011-03-09 06:22:24 +08:00
2011-03-01 05:26:43 +08:00
cvtColor
2012-08-23 18:56:02 +08:00
--------
2011-06-30 06:06:42 +08:00
Converts an image from one color space to another.
2011-06-16 20:48:23 +08:00
.. ocv:function :: void cvtColor( InputArray src, OutputArray dst, int code, int dstCn=0 )
2011-02-23 04:43:26 +08:00
2011-06-30 06:06:42 +08:00
.. ocv:pyfunction :: cv2.cvtColor(src, code[, dst[, dstCn]]) -> dst
.. ocv:cfunction :: void cvCvtColor( const CvArr* src, CvArr* dst, int code )
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param src: input image: 8-bit unsigned, 16-bit unsigned ( ``CV_16UC...`` ), or single-precision floating-point.
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param dst: output image of the same size and depth as ``src``.
2012-05-28 15:36:14 +08:00
2012-08-23 18:56:02 +08:00
:param code: color space conversion code (see the description below).
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param dstCn: number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from ``src`` and ``code`` .
2011-03-01 05:26:43 +08:00
2011-04-25 05:02:14 +08:00
The function converts an input image from one color
2011-07-01 17:39:22 +08:00
space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR).
Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
The conventional ranges for R, G, and B channel values are:
2011-02-23 04:43:26 +08:00
*
2011-02-26 19:05:10 +08:00
0 to 255 for `` CV_8U `` images
2011-02-23 04:43:26 +08:00
*
2012-05-28 15:36:14 +08:00
0 to 65535 for `` CV_16U `` images
2011-02-23 04:43:26 +08:00
*
2011-04-25 05:02:14 +08:00
0 to 1 for `` CV_32F `` images
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
In case of linear transformations, the range does not matter.
But in case of a non-linear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB
2011-07-01 17:39:22 +08:00
:math:`\rightarrow` L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will have the 0..255 value range instead of 0..1 assumed by the function. So, before calling ``cvtColor`` , you need first to scale the image down: ::
2011-02-23 04:43:26 +08:00
img *= 1./255;
2013-06-06 09:55:56 +08:00
cvtColor(img, img, COLOR_BGR2Luv);
2011-02-23 04:43:26 +08:00
2011-07-01 17:39:22 +08:00
If you use `` cvtColor `` with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.
2011-06-23 20:00:09 +08:00
2014-01-13 18:20:42 +08:00
If conversion adds the alpha channel, its value will set to the maximum of corresponding channel range: 255 for `` CV_8U `` , 65535 for `` CV_16U `` , 1 for `` CV_32F `` .
2011-02-23 04:43:26 +08:00
The function can do the following transformations:
*
2013-05-25 04:20:41 +08:00
RGB :math: `\leftrightarrow` GRAY ( `` CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB `` )
2011-02-23 04:43:26 +08:00
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:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
\text{RGB[A] to Gray:} \quad Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
and
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2014-01-13 18:20:42 +08:00
\text{Gray to RGB[A]:} \quad R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow \max (ChannelRange)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
The conversion from a RGB image to gray is done with:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
::
2011-02-26 19:05:10 +08:00
2013-06-06 09:55:56 +08:00
cvtColor(src, bwsrc, COLOR_RGB2GRAY);
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
..
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
More advanced channel reordering can also be done with
2011-06-16 20:48:23 +08:00
:ocv:func: `mixChannels` .
2011-02-23 04:43:26 +08:00
*
2011-02-26 19:05:10 +08:00
RGB
2013-06-06 09:55:56 +08:00
:math: `\leftrightarrow` CIE XYZ.Rec 709 with D65 white point ( `` COLOR_BGR2XYZ, COLOR_RGB2XYZ, COLOR_XYZ2BGR, COLOR_XYZ2RGB `` ):
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
\begin{bmatrix} X \\ Y \\ Z
\end{bmatrix} \leftarrow \begin{bmatrix} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227
\end{bmatrix} \cdot \begin{bmatrix} R \\ G \\ B
2011-02-26 19:05:10 +08:00
\end{bmatrix}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
\begin{bmatrix} R \\ G \\ B
\end{bmatrix} \leftarrow \begin{bmatrix} 3.240479 & -1.53715 & -0.498535 \\ -0.969256 & 1.875991 & 0.041556 \\ 0.055648 & -0.204043 & 1.057311
\end{bmatrix} \cdot \begin{bmatrix} X \\ Y \\ Z
2011-02-26 19:05:10 +08:00
\end{bmatrix}
:math: `X` , :math: `Y` and
2011-04-25 05:02:14 +08:00
:math: `Z` cover the whole value range (in case of floating-point images,
2011-02-26 19:05:10 +08:00
:math: `Z` may exceed 1).
2011-02-23 04:43:26 +08:00
*
2011-02-26 19:05:10 +08:00
RGB
2013-06-06 09:55:56 +08:00
:math: `\leftrightarrow` YCrCb JPEG (or YCC) ( `` COLOR_BGR2YCrCb, COLOR_RGB2YCrCb, COLOR_YCrCb2BGR, COLOR_YCrCb2RGB `` )
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
Cr \leftarrow (R-Y) \cdot 0.713 + delta
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
Cb \leftarrow (B-Y) \cdot 0.564 + delta
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
R \leftarrow Y + 1.403 \cdot (Cr - delta)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2013-01-28 22:29:01 +08:00
G \leftarrow Y - 0.714 \cdot (Cr - delta) - 0.344 \cdot (Cb - delta)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
B \leftarrow Y + 1.773 \cdot (Cb - delta)
2011-02-23 04:43:26 +08:00
where
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
delta = \left \{ \begin{array}{l l} 128 & \mbox{for 8-bit images} \\ 32768 & \mbox{for 16-bit images} \\ 0.5 & \mbox{for floating-point images} \end{array} \right .
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
Y, Cr, and Cb cover the whole value range.
2011-02-23 04:43:26 +08:00
*
2013-06-06 09:55:56 +08:00
RGB :math: `\leftrightarrow` HSV ( `` COLOR_BGR2HSV, COLOR_RGB2HSV, COLOR_HSV2BGR, COLOR_HSV2RGB `` )
2011-04-25 05:02:14 +08:00
In case of 8-bit and 16-bit images,
R, G, and B are converted to the floating-point format and scaled to fit the 0 to 1 range.
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V \leftarrow max(R,G,B)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
S \leftarrow \fork{\frac{V-min(R,G,B)}{V}}{if $V \neq 0$}{0}{otherwise}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-06-07 20:22:07 +08:00
H \leftarrow \forkthree{{60(G - B)}/{(V-min(R,G,B))}}{if $V=R$}{{120+60(B - R)}/{(V-min(R,G,B))}}{if $V=G$}{{240+60(R - G)}/{(V-min(R,G,B))}}{if $V=B$}
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
If
2011-02-26 19:05:10 +08:00
:math: `H<0` then
2011-04-25 05:02:14 +08:00
:math: `H \leftarrow H+360` . On output
2011-05-05 21:31:54 +08:00
:math: `0 \leq V \leq 1` , :math: `0 \leq S \leq 1` , :math: `0 \leq H \leq 360` .
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
The values are then converted to the destination data type:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 8-bit images
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V \leftarrow 255 V, S \leftarrow 255 S, H \leftarrow H/2 \text{(to fit to 0 to 255)}
2011-02-23 04:43:26 +08:00
* 16-bit images (currently not supported)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V <- 65535 V, S <- 65535 S, H <- H
2011-02-23 04:43:26 +08:00
* 32-bit images
2011-04-25 05:02:14 +08:00
H, S, and V are left as is
2011-02-23 04:43:26 +08:00
*
2013-06-06 09:55:56 +08:00
RGB :math: `\leftrightarrow` HLS ( `` COLOR_BGR2HLS, COLOR_RGB2HLS, COLOR_HLS2BGR, COLOR_HLS2RGB `` ).
2011-04-25 05:02:14 +08:00
In case of 8-bit and 16-bit images,
R, G, and B are converted to the floating-point format and scaled to fit the 0 to 1 range.
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V_{max} \leftarrow {max}(R,G,B)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V_{min} \leftarrow {min}(R,G,B)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
L \leftarrow \frac{V_{max} + V_{min}}{2}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
S \leftarrow \fork { \frac{V_{max} - V_{min}}{V_{max} + V_{min}} }{if $L < 0.5$ }
2011-02-26 19:05:10 +08:00
{ \frac{V_{max} - V_{min}}{2 - (V_{max} + V_{min})} }{if $L \ge 0.5$ }
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
H \leftarrow \forkthree {{60(G - B)}/{S}}{if $V_{max}=R$ }
{{120+60(B - R)}/{S}}{if $V_{max}=G$ }
2011-02-26 19:05:10 +08:00
{{240+60(R - G)}/{S}}{if $V_{max}=B$ }
2011-04-25 05:02:14 +08:00
If
2011-02-26 19:05:10 +08:00
:math: `H<0` then
2011-04-25 05:02:14 +08:00
:math: `H \leftarrow H+360` . On output
2011-02-26 19:05:10 +08:00
:math: `0 \leq L \leq 1` , :math: `0 \leq S \leq 1` , :math: `0 \leq H \leq 360` .
2011-02-23 04:43:26 +08:00
The values are then converted to the destination data type:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 8-bit images
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V \leftarrow 255 \cdot V, S \leftarrow 255 \cdot S, H \leftarrow H/2 \; \text{(to fit to 0 to 255)}
2011-02-23 04:43:26 +08:00
* 16-bit images (currently not supported)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
V <- 65535 \cdot V, S <- 65535 \cdot S, H <- H
2011-02-23 04:43:26 +08:00
* 32-bit images
H, S, V are left as is
*
2013-06-06 09:55:56 +08:00
RGB :math: `\leftrightarrow` CIE L*a* b* ( `` COLOR_BGR2Lab, COLOR_RGB2Lab, COLOR_Lab2BGR, COLOR_Lab2RGB `` ).
2011-04-25 05:02:14 +08:00
In case of 8-bit and 16-bit images,
R, G, and B are converted to the floating-point format and scaled to fit the 0 to 1 range.
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\vecthree{X}{Y}{Z} \leftarrow \vecthreethree{0.412453}{0.357580}{0.180423}{0.212671}{0.715160}{0.072169}{0.019334}{0.119193}{0.950227} \cdot \vecthree{R}{G}{B}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
X \leftarrow X/X_n, \text{where} X_n = 0.950456
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
Z \leftarrow Z/Z_n, \text{where} Z_n = 1.088754
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
L \leftarrow \fork{116*Y^{1/3}-16}{for $Y>0.008856$}{903.3* Y}{for $Y \le 0.008856$}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
a \leftarrow 500 (f(X)-f(Y)) + delta
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
b \leftarrow 200 (f(Y)-f(Z)) + delta
2011-02-23 04:43:26 +08:00
where
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
f(t)= \fork{t^{1/3}}{for $t>0.008856$}{7.787 t+16/116}{for $t\leq 0.008856$}
2011-02-23 04:43:26 +08:00
and
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
delta = \fork{128}{for 8-bit images}{0}{for floating-point images}
2011-04-25 05:02:14 +08:00
This outputs
:math: `0 \leq L \leq 100` , :math: `-127 \leq a \leq 127` , :math: `-127 \leq b \leq 127` . The values are then converted to the destination data type:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 8-bit images
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
L \leftarrow L*255/100, \; a \leftarrow a + 128, \; b \leftarrow b + 128
2011-02-23 04:43:26 +08:00
* 16-bit images
2011-04-25 05:02:14 +08:00
(currently not supported)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 32-bit images
2011-04-25 05:02:14 +08:00
L, a, and b are left as is
2011-02-23 04:43:26 +08:00
*
2013-06-06 09:55:56 +08:00
RGB :math: `\leftrightarrow` CIE L*u* v* ( `` COLOR_BGR2Luv, COLOR_RGB2Luv, COLOR_Luv2BGR, COLOR_Luv2RGB `` ).
2011-04-25 05:02:14 +08:00
In case of 8-bit and 16-bit images,
R, G, and B are converted to the floating-point format and scaled to fit 0 to 1 range.
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\vecthree{X}{Y}{Z} \leftarrow \vecthreethree{0.412453}{0.357580}{0.180423}{0.212671}{0.715160}{0.072169}{0.019334}{0.119193}{0.950227} \cdot \vecthree{R}{G}{B}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
L \leftarrow \fork{116 Y^{1/3}}{for $Y>0.008856$}{903.3 Y}{for $Y\leq 0.008856$}
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
u' \leftarrow 4*X/(X + 15* Y + 3 Z)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
v' \leftarrow 9*Y/(X + 15* Y + 3 Z)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
u \leftarrow 13*L* (u' - u_n) \quad \text{where} \quad u_n=0.19793943
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
v \leftarrow 13*L* (v' - v_n) \quad \text{where} \quad v_n=0.46831096
2011-04-25 05:02:14 +08:00
This outputs
2011-02-26 19:05:10 +08:00
:math: `0 \leq L \leq 100` , :math: `-134 \leq u \leq 220` , :math: `-140 \leq v \leq 122` .
2011-02-23 04:43:26 +08:00
The values are then converted to the destination data type:
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 8-bit images
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
L \leftarrow 255/100 L, \; u \leftarrow 255/354 (u + 134), \; v \leftarrow 255/256 (v + 140)
2011-02-23 04:43:26 +08:00
* 16-bit images
2011-04-25 05:02:14 +08:00
(currently not supported)
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
* 32-bit images
2011-04-25 05:02:14 +08:00
L, u, and v are left as is
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
The above formulae for converting RGB to/from various color spaces have been taken from multiple sources on the web, primarily from the Charles Poynton site
2011-02-23 04:43:26 +08:00
http://www.poynton.com/ColorFAQ.html
*
2013-06-06 09:55:56 +08:00
Bayer :math: `\rightarrow` RGB ( `` COLOR_BayerBG2BGR, COLOR_BayerGB2BGR, COLOR_BayerRG2BGR, COLOR_BayerGR2BGR, COLOR_BayerBG2RGB, COLOR_BayerGB2RGB, COLOR_BayerRG2RGB, COLOR_BayerGR2RGB `` ). The Bayer pattern is widely used in CCD and CMOS cameras. It enables you to get color pictures from a single plane where R,G, and B pixels (sensors of a particular component) are interleaved as follows:
2011-02-26 19:05:10 +08:00
2011-06-14 18:26:24 +08:00
.. image :: pics/bayer.png
2011-02-26 19:05:10 +08:00
2011-04-25 05:02:14 +08:00
The output RGB components of a pixel are interpolated from 1, 2, or
2011-02-23 04:43:26 +08:00
4 neighbors of the pixel having the same color. There are several
modifications of the above pattern that can be achieved by shifting
the pattern one pixel left and/or one pixel up. The two letters
2011-02-26 19:05:10 +08:00
:math: `C_1` and
:math: `C_2` in the conversion constants `` CV_Bayer `` :math: `C_1 C_2` `` 2BGR `` and `` CV_Bayer `` :math: `C_1 C_2` `` 2RGB `` indicate the particular pattern
2011-04-25 05:02:14 +08:00
type. These are components from the second row, second and third
columns, respectively. For example, the above pattern has a very
2011-02-23 04:43:26 +08:00
popular "BG" type.
2011-03-09 06:22:24 +08:00
2011-03-01 05:26:43 +08:00
distanceTransform
2012-08-23 18:56:02 +08:00
-----------------
2011-06-30 06:06:42 +08:00
Calculates the distance to the closest zero pixel for each pixel of the source image.
2011-06-16 20:48:23 +08:00
.. ocv:function :: void distanceTransform( InputArray src, OutputArray dst, int distanceType, int maskSize )
2011-02-23 04:43:26 +08:00
2012-03-27 17:06:21 +08:00
.. ocv:function :: void distanceTransform( InputArray src, OutputArray dst, OutputArray labels, int distanceType, int maskSize, int labelType=DIST_LABEL_CCOMP )
2011-02-23 04:43:26 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:pyfunction :: cv2.distanceTransform(src, distanceType, maskSize[, dst]) -> dst
2011-06-30 06:06:42 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:cfunction :: void cvDistTransform( const CvArr* src, CvArr* dst, int distance_type=CV_DIST_L2, int mask_size=3, const float* mask=NULL, CvArr* labels=NULL, int labelType=CV_DIST_LABEL_CCOMP )
2011-06-30 06:06:42 +08:00
2011-04-25 05:02:14 +08:00
:param src: 8-bit, single-channel (binary) source image.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param dst: Output image with calculated distances. It is a 32-bit floating-point, single-channel image of the same size as ``src`` .
2012-05-28 15:36:14 +08:00
2011-04-25 05:02:14 +08:00
:param distanceType: Type of distance. It can be ``CV_DIST_L1, CV_DIST_L2`` , or ``CV_DIST_C`` .
2012-05-28 15:36:14 +08:00
2011-05-05 21:31:54 +08:00
:param maskSize: Size of the distance transform mask. It can be 3, 5, or ``CV_DIST_MASK_PRECISE`` (the latter option is only supported by the first function). In case of the ``CV_DIST_L1`` or ``CV_DIST_C`` distance type, the parameter is forced to 3 because a :math:`3\times 3` mask gives the same result as :math:`5\times 5` or any larger aperture.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param labels: Optional output 2D array of labels (the discrete Voronoi diagram). It has the type ``CV_32SC1`` and the same size as ``src`` . See the details below.
2012-05-28 15:36:14 +08:00
2012-03-27 17:06:21 +08:00
:param labelType: Type of the label array to build. If ``labelType==DIST_LABEL_CCOMP`` then each connected component of zeros in ``src`` (as well as all the non-zero pixels closest to the connected component) will be assigned the same label. If ``labelType==DIST_LABEL_PIXEL`` then each zero pixel (and all the non-zero pixels closest to it) gets its own label.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
The functions `` distanceTransform `` calculate the approximate or precise
2011-02-23 04:43:26 +08:00
distance from every binary image pixel to the nearest zero pixel.
2011-04-25 05:02:14 +08:00
For zero image pixels, the distance will obviously be zero.
2011-02-23 04:43:26 +08:00
2012-04-14 05:50:59 +08:00
When `` maskSize == CV_DIST_MASK_PRECISE `` and `` distanceType == CV_DIST_L2 `` , the function runs the algorithm described in [Felzenszwalb04]_ . This algorithm is parallelized with the TBB library.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
In other cases, the algorithm
2011-06-30 06:06:42 +08:00
[Borgefors86]_
2011-04-25 05:02:14 +08:00
is used. This means that
for a pixel the function finds the shortest path to the nearest zero pixel
2011-02-23 04:43:26 +08:00
consisting of basic shifts: horizontal,
2011-04-25 05:02:14 +08:00
vertical, diagonal, or knight's move (the latest is available for a
2011-02-26 19:05:10 +08:00
:math: `5\times 5` mask). The overall distance is calculated as a sum of these
2011-04-25 05:02:14 +08:00
basic distances. Since the distance function should be symmetric,
all of the horizontal and vertical shifts must have the same cost (denoted as `` a `` ), all the diagonal shifts must have the
same cost (denoted as `` b `` ), and all knight's moves must have
the same cost (denoted as `` c `` ). For the `` CV_DIST_C `` and `` CV_DIST_L1 `` types, the distance is calculated precisely,
2012-04-14 05:50:59 +08:00
whereas for `` CV_DIST_L2 `` (Euclidean distance) the distance
2011-04-25 05:02:14 +08:00
can be calculated only with a relative error (a
2011-02-26 19:05:10 +08:00
:math: `5\times 5` mask
2011-04-25 05:02:14 +08:00
gives more accurate results). For `` a `` ,`` b `` , and `` c `` , OpenCV uses the values suggested in the original paper:
2011-02-23 04:43:26 +08:00
.. table ::
2011-03-01 05:26:43 +08:00
============== =================== ======================
`` CV_DIST_C `` :math: `(3\times 3)` a = 1, b = 1 \
============== =================== ======================
`` CV_DIST_L1 `` :math: `(3\times 3)` a = 1, b = 2 \
`` CV_DIST_L2 `` :math: `(3\times 3)` a=0.955, b=1.3693 \
`` CV_DIST_L2 `` :math: `(5\times 5)` a=1, b=1.4, c=2.1969 \
2011-02-23 04:43:26 +08:00
============== =================== ======================
2011-07-05 00:18:12 +08:00
Typically, for a fast, coarse distance estimation `` CV_DIST_L2 `` , a
2011-04-25 05:02:14 +08:00
:math: `3\times 3` mask is used. For a more accurate distance estimation `` CV_DIST_L2 `` , a
2011-02-26 19:05:10 +08:00
:math: `5\times 5` mask or the precise algorithm is used.
2011-02-23 04:43:26 +08:00
Note that both the precise and the approximate algorithms are linear on the number of pixels.
2011-02-26 19:05:10 +08:00
The second variant of the function does not only compute the minimum distance for each pixel
2011-04-25 05:02:14 +08:00
:math: `(x, y)` but also identifies the nearest connected
2012-03-27 17:06:21 +08:00
component consisting of zero pixels (`` labelType==DIST_LABEL_CCOMP `` ) or the nearest zero pixel (`` labelType==DIST_LABEL_PIXEL `` ). Index of the component/pixel is stored in
2011-02-26 19:05:10 +08:00
:math: `\texttt{labels}(x, y)` .
2012-03-27 17:06:21 +08:00
When `` labelType==DIST_LABEL_CCOMP `` , the function automatically finds connected components of zero pixels in the input image and marks them with distinct labels. When `` labelType==DIST_LABEL_CCOMP `` , the function scans through the input image and marks all the zero pixels with distinct labels.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
In this mode, the complexity is still linear.
That is, the function provides a very fast way to compute the Voronoi diagram for a binary image.
2012-03-27 17:06:21 +08:00
Currently, the second variant can use only the approximate distance transform algorithm, i.e. `` maskSize=CV_DIST_MASK_PRECISE `` is not supported yet.
2011-03-09 06:22:24 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
* An example on using the distance transform can be found at opencv_source_code/samples/cpp/distrans.cpp
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
* (Python) An example on using the distance transform can be found at opencv_source/samples/python2/distrans.py
2013-08-02 20:05:08 +08:00
2011-03-01 05:26:43 +08:00
floodFill
2012-08-23 18:56:02 +08:00
---------
2011-06-30 06:06:42 +08:00
Fills a connected component with the given color.
2012-05-28 15:36:14 +08:00
.. ocv:function :: int floodFill( InputOutputArray image, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )
2011-02-23 04:43:26 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:function :: int floodFill( InputOutputArray image, InputOutputArray mask, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )
2011-02-23 04:43:26 +08:00
2013-03-15 20:55:58 +08:00
.. ocv:pyfunction :: cv2.floodFill(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) -> retval, image, mask, rect
2011-06-30 06:06:42 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:cfunction :: void cvFloodFill( CvArr* image, CvPoint seed_point, CvScalar new_val, CvScalar lo_diff=cvScalarAll(0), CvScalar up_diff=cvScalarAll(0), CvConnectedComp* comp=NULL, int flags=4, CvArr* mask=NULL )
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param image: Input/output 1- or 3-channel, 8-bit, or floating-point image. It is modified by the function unless the ``FLOODFILL_MASK_ONLY`` flag is set in the second variant of the function. See the details below.
2011-02-23 04:43:26 +08:00
2012-05-28 15:36:14 +08:00
:param mask: (For the second function only) Operation mask that should be a single-channel 8-bit image, 2 pixels wider and 2 pixels taller. The function uses and updates the mask, so you take responsibility of initializing the ``mask`` content. Flood-filling cannot 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 does not overlap.
2011-07-07 19:43:21 +08:00
.. note :: Since the mask is larger than the filled image, a pixel :math: `(x, y)` in `` image `` corresponds to the pixel :math: `(x+1, y+1)` in the `` mask `` .
2012-05-29 18:36:19 +08:00
:param seedPoint: Starting point.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param newVal: New value of the repainted domain pixels.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param 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.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param 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.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param rect: Optional output parameter set by the function to the minimum bounding rectangle of the repainted domain.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param flags: Operation flags. Lower bits contain a connectivity value, 4 (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:
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
* **FLOODFILL_FIXED_RANGE** If set, the difference between the current pixel and seed pixel is considered. Otherwise, the difference between neighbor pixels is considered (that is, the range is floating).
2011-02-23 04:43:26 +08:00
2013-04-17 15:34:20 +08:00
* **FLOODFILL_MASK_ONLY** If set, the function does not change the image ( `` newVal `` is ignored), but fills the mask with the value in bits 8-16 of `` flags `` (that is, the fill value is set to newValue by adding (newValue << 8) to the `` flags `` ). The flag can be used for the second variant only.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
The functions `` 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
2011-05-05 21:31:54 +08:00
:math: `(x,y)` is considered to belong to the repainted domain if:
2011-02-26 19:05:10 +08:00
2012-05-28 15:36:14 +08:00
*
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{src} (x',y')- \texttt{loDiff} \leq \texttt{src} (x,y) \leq \texttt{src} (x',y')+ \texttt{upDiff}
2011-02-23 04:43:26 +08:00
2011-07-01 17:39:22 +08:00
in case of a grayscale image and floating range
2011-05-05 21:31:54 +08:00
2012-05-28 15:36:14 +08:00
*
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2012-05-29 18:36:19 +08:00
\texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)- \texttt{loDiff} \leq \texttt{src} (x,y) \leq \texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)+ \texttt{upDiff}
2011-02-23 04:43:26 +08:00
2011-07-01 17:39:22 +08:00
in case of a grayscale image and fixed range
2011-05-05 21:31:54 +08:00
*
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2011-05-05 21:31:54 +08:00
\texttt{src} (x',y')_r- \texttt{loDiff} _r \leq \texttt{src} (x,y)_r \leq \texttt{src} (x',y')_r+ \texttt{upDiff} _r,
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{src} (x',y')_g- \texttt{loDiff} _g \leq \texttt{src} (x,y)_g \leq \texttt{src} (x',y')_g+ \texttt{upDiff} _g
2012-05-28 15:36:14 +08:00
2011-05-05 21:31:54 +08:00
and
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{src} (x',y')_b- \texttt{loDiff} _b \leq \texttt{src} (x,y)_b \leq \texttt{src} (x',y')_b+ \texttt{upDiff} _b
2011-02-23 04:43:26 +08:00
2011-07-01 17:39:22 +08:00
in case of a color image and floating range
2011-05-05 21:31:54 +08:00
*
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2012-05-29 18:36:19 +08:00
\texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_r- \texttt{loDiff} _r \leq \texttt{src} (x,y)_r \leq \texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_r+ \texttt{upDiff} _r,
2011-02-26 19:05:10 +08:00
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
2012-05-29 18:36:19 +08:00
\texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_g- \texttt{loDiff} _g \leq \texttt{src} (x,y)_g \leq \texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_g+ \texttt{upDiff} _g
2011-02-26 19:05:10 +08:00
2011-05-05 21:31:54 +08:00
and
2011-02-23 04:43:26 +08:00
.. math ::
2012-05-29 18:36:19 +08:00
\texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_b- \texttt{loDiff} _b \leq \texttt{src} (x,y)_b \leq \texttt{src} ( \texttt{seedPoint} .x, \texttt{seedPoint} .y)_b+ \texttt{upDiff} _b
2011-02-23 04:43:26 +08:00
2011-07-01 17:39:22 +08:00
in case of a color image and fixed range
2011-05-05 21:31:54 +08:00
2011-02-26 19:05:10 +08:00
where
2011-04-25 05:02:14 +08:00
:math: `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 color/brightness of the pixel should be close enough to:
2011-02-23 04:43:26 +08:00
*
2011-07-01 17:39:22 +08:00
Color/brightness of one of its neighbors that already belong to the connected component in case of a floating range.
2011-02-23 04:43:26 +08:00
*
2011-07-01 17:39:22 +08:00
Color/brightness of the seed point in case of a fixed range.
2011-02-23 04:43:26 +08:00
2013-07-29 21:51:16 +08:00
Use these functions to 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, and so on.
2011-02-23 04:43:26 +08:00
2011-06-30 20:06:26 +08:00
.. seealso :: :ocv:func: `findContours`
2011-06-30 06:06:42 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2011-06-30 06:06:42 +08:00
2013-08-06 22:24:09 +08:00
* An example using the FloodFill technique can be found at opencv_source_code/samples/cpp/ffilldemo.cpp
2011-03-09 06:22:24 +08:00
2013-08-06 22:24:09 +08:00
* (Python) An example using the FloodFill technique can be found at opencv_source_code/samples/python2/floodfill.cpp
2011-03-09 06:22:24 +08:00
2011-03-01 05:26:43 +08:00
integral
2012-08-23 18:56:02 +08:00
--------
2011-06-30 06:06:42 +08:00
Calculates the integral of an image.
2012-05-28 15:36:14 +08:00
.. ocv:function :: void integral( InputArray src, OutputArray sum, int sdepth=-1 )
2011-02-23 04:43:26 +08:00
2013-12-05 23:43:50 +08:00
.. ocv:function :: void integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth=-1, int sqdepth=-1 )
2011-02-23 04:43:26 +08:00
2013-12-05 23:43:50 +08:00
.. ocv:function :: void integral( InputArray src, OutputArray sum, OutputArray sqsum, OutputArray tilted, int sdepth=-1, int sqdepth=-1 )
2011-02-23 04:43:26 +08:00
2011-06-30 06:06:42 +08:00
.. ocv:pyfunction :: cv2.integral(src[, sum[, sdepth]]) -> sum
2013-12-05 23:43:50 +08:00
.. ocv:pyfunction :: cv2.integral2(src[, sum[, sqsum[, sdepth[, sqdepth]]]]) -> sum, sqsum
2011-06-30 06:06:42 +08:00
2013-12-05 23:43:50 +08:00
.. ocv:pyfunction :: cv2.integral3(src[, sum[, sqsum[, tilted[, sdepth[, sqdepth]]]]]) -> sum, sqsum, tilted
2011-06-30 06:06:42 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:cfunction :: void cvIntegral( const CvArr* image, CvArr* sum, CvArr* sqsum=NULL, CvArr* tilted_sum=NULL )
2012-08-23 18:56:02 +08:00
:param image: input image as :math:`W \times H`, 8-bit or floating-point (32f or 64f).
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param sum: integral image as :math:`(W+1)\times (H+1)` , 32-bit integer or floating-point (32f or 64f).
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param sqsum: integral image for squared pixel values; it is :math:`(W+1)\times (H+1)`, double-precision floating-point (64f) array.
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param tilted: integral for the image rotated by 45 degrees; it is :math:`(W+1)\times (H+1)` array with the same data type as ``sum``.
2012-05-28 15:36:14 +08:00
2012-08-23 18:56:02 +08:00
:param sdepth: desired depth of the integral and the tilted integral images, ``CV_32S``, ``CV_32F``, or ``CV_64F``.
2011-03-01 05:26:43 +08:00
2013-12-05 23:43:50 +08:00
:param sqdepth: desired depth of the integral image of squared pixel values, ``CV_32F`` or ``CV_64F``.
2011-07-01 17:39:22 +08:00
The functions calculate one or more integral images for the source image as follows:
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{sum} (X,Y) = \sum _{x<X,y<Y} \texttt{image} (x,y)
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{sqsum} (X,Y) = \sum _{x<X,y<Y} \texttt{image} (x,y)^2
2011-02-23 04:43:26 +08:00
.. math ::
2011-02-26 19:05:10 +08:00
\texttt{tilted} (X,Y) = \sum _{y<Y,abs(x-X+1) \leq Y-y-1} \texttt{image} (x,y)
2011-02-23 04:43:26 +08:00
2014-02-24 17:06:20 +08:00
Using these integral images, you can calculate sum, mean, and standard deviation over a specific up-right or rotated rectangular region of the image in a constant time, for example:
2011-02-23 04:43:26 +08:00
.. math ::
2011-06-20 07:24:01 +08:00
\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,y_1)
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multi-channel images, sums for each channel are accumulated independently.
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
As a practical example, the next figure shows the calculation of the integral of a straight rectangle `` Rect(3,3,3,2) `` and of a tilted rectangle `` Rect(5,1,2,3) `` . The selected pixels in the original `` image `` are shown, as well as the relative pixels in the integral images `` sum `` and `` tilted `` .
2011-02-23 04:43:26 +08:00
2011-03-06 05:26:13 +08:00
.. image :: pics/integral.png
2011-02-23 04:43:26 +08:00
2011-06-30 06:06:42 +08:00
2011-03-09 06:22:24 +08:00
2011-03-01 05:26:43 +08:00
threshold
2012-08-23 18:56:02 +08:00
---------
2011-06-30 06:06:42 +08:00
Applies a fixed-level threshold to each array element.
2012-05-28 15:36:14 +08:00
.. ocv:function :: double threshold( InputArray src, OutputArray dst, double thresh, double maxval, int type )
2011-02-23 04:43:26 +08:00
2011-06-30 06:06:42 +08:00
.. ocv:pyfunction :: cv2.threshold(src, thresh, maxval, type[, dst]) -> retval, dst
2012-05-28 15:36:14 +08:00
.. ocv:cfunction :: double cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type )
2012-08-23 18:56:02 +08:00
:param src: input array (single-channel, 8-bit or 32-bit floating point).
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param dst: output array of the same size and type as ``src``.
2012-05-28 15:36:14 +08:00
2012-12-14 14:49:51 +08:00
:param thresh: threshold value.
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param maxval: maximum value to use with the ``THRESH_BINARY`` and ``THRESH_BINARY_INV`` thresholding types.
2011-02-23 04:43:26 +08:00
2012-08-23 18:56:02 +08:00
:param type: thresholding type (see the details below).
2011-02-23 04:43:26 +08:00
The function applies fixed-level thresholding
to a single-channel array. The function is typically used to get a
bi-level (binary) image out of a grayscale image (
2011-06-16 20:48:23 +08:00
:ocv:func: `compare` could
2011-04-25 05:02:14 +08:00
be also used for this purpose) or for removing a noise, that is, filtering
2011-02-23 04:43:26 +08:00
out pixels with too small or too large values. There are several
2012-05-29 18:36:19 +08:00
types of thresholding supported by the function. They are determined by `` type `` :
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_BINARY**
2011-02-23 04:43:26 +08:00
.. math ::
2012-05-29 18:36:19 +08:00
\texttt{dst} (x,y) = \fork{\texttt{maxval}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_BINARY_INV**
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
.. math ::
2011-02-23 04:43:26 +08:00
2012-05-29 18:36:19 +08:00
\texttt{dst} (x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{maxval}}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_TRUNC**
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
.. math ::
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_TOZERO**
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
.. math ::
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
* **THRESH_TOZERO_INV**
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
.. math ::
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
\texttt{dst} (x,y) = \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}
2011-02-23 04:43:26 +08:00
2011-02-26 19:05:10 +08:00
Also, the special value `` THRESH_OTSU `` may be combined with
2011-04-25 05:02:14 +08:00
one of the above values. In this case, the function determines the optimal threshold
2011-07-01 17:39:22 +08:00
value using the Otsu's algorithm and uses it instead of the specified `` thresh `` .
2011-02-26 19:05:10 +08:00
The function returns the computed threshold value.
2012-05-28 15:36:14 +08:00
Currently, the Otsu's method is implemented only for 8-bit images.
2012-04-14 05:50:59 +08:00
2011-02-23 04:43:26 +08:00
2011-03-06 05:26:13 +08:00
.. image :: pics/threshold.png
2011-02-23 04:43:26 +08:00
2011-06-30 20:06:26 +08:00
.. seealso ::
2011-03-09 06:22:24 +08:00
2011-06-30 20:06:26 +08:00
:ocv:func: `adaptiveThreshold` ,
:ocv:func: `findContours` ,
:ocv:func: `compare` ,
:ocv:func: `min` ,
:ocv:func: `max`
2011-06-30 06:06:42 +08:00
2011-02-23 04:43:26 +08:00
2011-03-01 05:26:43 +08:00
watershed
2012-08-23 18:56:02 +08:00
---------
2012-04-14 05:50:59 +08:00
Performs a marker-based image segmentation using the watershed algorithm.
2011-06-30 06:06:42 +08:00
2011-06-16 20:48:23 +08:00
.. ocv:function :: void watershed( InputArray image, InputOutputArray markers )
2011-02-23 04:43:26 +08:00
2011-08-07 03:17:14 +08:00
.. ocv:cfunction :: void cvWatershed( const CvArr* image, CvArr* markers )
2013-03-15 20:55:58 +08:00
.. ocv:pyfunction :: cv2.watershed(image, markers) -> markers
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param image: Input 8-bit 3-channel image.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param markers: Input/output 32-bit single-channel image (map) of markers. It should have the same size as ``image`` .
2011-03-01 05:26:43 +08:00
2011-08-07 03:17:14 +08:00
The function implements one of the variants of watershed, non-parametric marker-based segmentation algorithm, described in [Meyer92]_ .
2011-02-23 04:43:26 +08:00
2011-08-07 03:17:14 +08:00
Before passing the image to the function, you have to roughly outline the desired regions in the image `` markers `` with positive (`` >0 `` ) indices. So, every region is represented as one or more connected components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary mask using :ocv:func: `findContours` and :ocv:func: `drawContours` (see the `` watershed.cpp `` demo). The markers are "seeds" of the future image regions. All the other pixels in `` markers `` , whose relation to the outlined regions is not known and should be defined by the algorithm, should be set to 0's. In the function output, each pixel in markers is set to a value of the "seed" components or to -1 at boundaries between the regions.
Visual demonstration and usage example of the function can be found in the OpenCV samples directory (see the `` watershed.cpp `` demo).
2011-06-30 06:06:42 +08:00
2011-08-07 03:17:14 +08:00
.. note :: Any two neighbor connected components are not necessarily separated by a watershed boundary (-1's pixels); for example, they can touch each other in the initial marker image passed to the function.
.. seealso :: :ocv:func: `findContours`
2011-02-23 04:43:26 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
* An example using the watershed algorithm can be found at opencv_source_code/samples/cpp/watershed.cpp
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
* (Python) An example using the watershed algorithm can be found at opencv_source_code/samples/python2/watershed.py
2013-08-02 20:05:08 +08:00
2011-03-01 05:26:43 +08:00
grabCut
2011-03-09 06:22:24 +08:00
-------
2011-06-30 06:06:42 +08:00
Runs the GrabCut algorithm.
2011-03-09 06:22:24 +08:00
2012-05-28 15:36:14 +08:00
.. ocv:function :: void grabCut( InputArray img, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArray fgdModel, int iterCount, int mode=GC_EVAL )
2011-02-23 04:43:26 +08:00
2013-03-15 20:55:58 +08:00
.. ocv:pyfunction :: cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount[, mode]) -> mask, bgdModel, fgdModel
2011-02-23 04:43:26 +08:00
2012-05-29 18:36:19 +08:00
:param img: Input 8-bit 3-channel image.
2011-02-23 04:43:26 +08:00
2011-05-05 21:31:54 +08:00
:param mask: Input/output 8-bit single-channel mask. The mask is initialized by the function when ``mode`` is set to ``GC_INIT_WITH_RECT``. Its elements may have one of following values:
* **GC_BGD** defines an obvious background pixels.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
* **GC_FGD** defines an obvious foreground (object) pixel.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
* **GC_PR_BGD** defines a possible background pixel.
2011-02-23 04:43:26 +08:00
2013-05-25 03:25:31 +08:00
* **GC_PR_FGD** defines a possible foreground pixel.
2011-02-23 04:43:26 +08:00
2011-05-05 21:31:54 +08:00
:param rect: ROI containing a segmented object. The pixels outside of the ROI are marked as "obvious background". The parameter is only used when ``mode==GC_INIT_WITH_RECT`` .
2012-05-28 15:36:14 +08:00
2011-06-30 20:06:26 +08:00
:param bgdModel: Temporary array for the background model. Do not modify it while you are processing the same image.
2012-05-28 15:36:14 +08:00
2011-06-30 20:06:26 +08:00
:param fgdModel: Temporary arrays for the foreground model. Do not modify it while you are processing the same image.
2011-02-23 04:43:26 +08:00
2011-04-25 05:02:14 +08:00
:param iterCount: Number of iterations the algorithm should make before returning the result. Note that the result can be refined with further calls with ``mode==GC_INIT_WITH_MASK`` or ``mode==GC_EVAL`` .
2012-05-28 15:36:14 +08:00
2011-04-25 05:02:14 +08:00
:param mode: Operation mode that could be one of the following:
2011-02-23 04:43:26 +08:00
2011-07-07 19:23:51 +08:00
* **GC_INIT_WITH_RECT** The function initializes the state and the mask using the provided rectangle. After that it runs `` iterCount `` iterations of the algorithm.
2011-02-23 04:43:26 +08:00
2011-07-07 19:23:51 +08:00
* **GC_INIT_WITH_MASK** The function initializes the state using the provided mask. Note that `` GC_INIT_WITH_RECT `` and `` GC_INIT_WITH_MASK `` can be combined. Then, all the pixels outside of the ROI are automatically initialized with `` GC_BGD `` .
2011-02-26 19:05:10 +08:00
2011-07-07 19:23:51 +08:00
* **GC_EVAL** The value means that the algorithm should just resume.
2011-02-26 19:05:10 +08:00
2011-03-09 06:22:24 +08:00
The function implements the `GrabCut image segmentation algorithm <http://en.wikipedia.org/wiki/GrabCut> `_ .
2011-07-01 17:39:22 +08:00
See the sample `` grabcut.cpp `` to learn how to use the function.
2011-06-30 06:06:42 +08:00
2011-07-01 17:39:22 +08:00
.. [Borgefors86] Borgefors, Gunilla, *Distance transformations in digital images* . Comput. Vision Graph. Image Process. 34 3, pp 344– 371 (1986)
2011-06-30 06:06:42 +08:00
2011-07-01 17:39:22 +08:00
.. [Felzenszwalb04] Felzenszwalb, Pedro F. and Huttenlocher, Daniel P. *Distance Transforms of Sampled Functions* , TR2004-1963, TR2004-1963 (2004)
2011-06-30 06:06:42 +08:00
2011-07-01 17:39:22 +08:00
.. [Meyer92] Meyer, F. *Color Image Segmentation* , ICIP92, 1992
2011-06-30 06:06:42 +08:00
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
.. note ::
2013-07-29 21:51:16 +08:00
2013-08-06 22:24:09 +08:00
* An example using the GrabCut algorithm can be found at opencv_source_code/samples/cpp/grabcut.cpp
2013-08-02 20:05:08 +08:00
2013-08-13 16:56:39 +08:00
* (Python) An example using the GrabCut algorithm can be found at opencv_source_code/samples/python2/grabcut.py