Applies an adaptive threshold to an array.
void adaptiveThreshold
(InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C)¶
cv2.
adaptiveThreshold
(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst¶
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 )¶
cv.
AdaptiveThreshold
(src, dst, maxValue, adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, thresholdType=CV_THRESH_BINARY, blockSize=3, param1=5) → None¶Parameters: 


The function transforms a grayscale image to a binary image according to the formulae:
THRESH_BINARY
THRESH_BINARY_INV
where is a threshold calculated individually for each pixel.
ADAPTIVE_THRESH_MEAN_C
, the threshold value
is a mean of the
neighborhood of
minus C
.ADAPTIVE_THRESH_GAUSSIAN_C
, the threshold value
is a weighted sum (crosscorrelation with a Gaussian window) of the
neighborhood of
minus C
. The default sigma (standard deviation) is used for the specified blockSize
. See
getGaussianKernel()
.The function can process the image inplace.
See also
Converts an image from one color space to another.
void cvtColor
(InputArray src, OutputArray dst, int code, int dstCn=0 )¶
cv2.
cvtColor
(src, code[, dst[, dstCn]]) → dst¶
void cvCvtColor
(const CvArr* src, CvArr* dst, int code)¶
cv.
CvtColor
(src, dst, code) → None¶Parameters: 


The function converts an input image from one color space to another. In case of a transformation tofrom 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 (24bit) color image will be an 8bit 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.
The conventional ranges for R, G, and B channel values are:
CV_8U
imagesCV_16U
imagesCV_32F
imagesIn case of linear transformations, the range does not matter.
But in case of a nonlinear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB
L*u*v* transformation. For example, if you have a 32bit floatingpoint image directly converted from an 8bit 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:
img *= 1./255;
cvtColor(img, img, CV_BGR2Luv);
If you use cvtColor
with 8bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.
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
.
The function can do the following transformations:
RGB GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB
)
Transformations within RGB space like adding/removing the alpha channel, reversing the channel order, conversion to/from 16bit RGB color (R5:G6:B5 or R5:G5:B5), as well as conversion to/from grayscale using:
and
The conversion from a RGB image to gray is done with:
cvtColor(src, bwsrc, CV_RGB2GRAY);
More advanced channel reordering can also be done with
mixChannels()
.
RGB
CIE XYZ.Rec 709 with D65 white point ( CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB
):
, and cover the whole value range (in case of floatingpoint images, may exceed 1).
RGB
YCrCb JPEG (or YCC) ( CV_BGR2YCrCb, CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB
)
where
Y, Cr, and Cb cover the whole value range.
CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB
)In case of 8bit and 16bit images, R, G, and B are converted to the floatingpoint format and scaled to fit the 0 to 1 range.
If then . On output , , .
The values are then converted to the destination data type:
8bit images
16bit images (currently not supported)
H, S, and V are left as is
CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB
).In case of 8bit and 16bit images, R, G, and B are converted to the floatingpoint format and scaled to fit the 0 to 1 range.
If then . On output , , .
The values are then converted to the destination data type:
8bit images
16bit images (currently not supported)
H, S, V are left as is
CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR, CV_Lab2RGB
).In case of 8bit and 16bit images, R, G, and B are converted to the floatingpoint format and scaled to fit the 0 to 1 range.
where
and
This outputs , , . The values are then converted to the destination data type:
8bit images
(currently not supported)
L, a, and b are left as is
CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB
).In case of 8bit and 16bit images, R, G, and B are converted to the floatingpoint format and scaled to fit 0 to 1 range.
This outputs , , .
The values are then converted to the destination data type:
8bit images
(currently not supported)
L, u, and v are left as is
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 http://www.poynton.com/ColorFAQ.html
Bayer RGB ( 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 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:
The output RGB components of a pixel are interpolated from 1, 2, or
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
and
in the conversion constants CV_Bayer
2BGR
and CV_Bayer
2RGB
indicate the particular pattern
type. These are components from the second row, second and third
columns, respectively. For example, the above pattern has a very
popular “BG” type.
Calculates the distance to the closest zero pixel for each pixel of the source image.
void distanceTransform
(InputArray src, OutputArray dst, int distanceType, int maskSize)¶
void distanceTransform
(InputArray src, OutputArray dst, OutputArray labels, int distanceType, int maskSize, int labelType=DIST_LABEL_CCOMP )¶
cv2.
distanceTransform
(src, distanceType, maskSize[, dst]) → dst¶
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 )¶
cv.
DistTransform
(src, dst, distance_type=CV_DIST_L2, mask_size=3, mask=None, labels=None) → None¶Parameters: 


The functions distanceTransform
calculate the approximate or precise
distance from every binary image pixel to the nearest zero pixel.
For zero image pixels, the distance will obviously be zero.
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.
In other cases, the algorithm
[Borgefors86]
is used. This means that
for a pixel the function finds the shortest path to the nearest zero pixel
consisting of basic shifts: horizontal,
vertical, diagonal, or knight’s move (the latest is available for a
mask). The overall distance is calculated as a sum of these
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,
whereas for CV_DIST_L2
(Euclidean distance) the distance
can be calculated only with a relative error (a
mask
gives more accurate results). For a
,``b`` , and c
, OpenCV uses the values suggested in the original paper:
CV_DIST_C 
a = 1, b = 1  

CV_DIST_L1 
a = 1, b = 2  
CV_DIST_L2 
a=0.955, b=1.3693  
CV_DIST_L2 
a=1, b=1.4, c=2.1969 
Typically, for a fast, coarse distance estimation CV_DIST_L2
, a
mask is used. For a more accurate distance estimation CV_DIST_L2
, a
mask or the precise algorithm is used.
Note that both the precise and the approximate algorithms are linear on the number of pixels.
The second variant of the function does not only compute the minimum distance for each pixel
but also identifies the nearest connected
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
.
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.
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.
Currently, the second variant can use only the approximate distance transform algorithm, i.e. maskSize=CV_DIST_MASK_PRECISE
is not supported yet.
Note
Fills a connected component with the given color.
int floodFill
(InputOutputArray image, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )¶
int floodFill
(InputOutputArray image, InputOutputArray mask, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )¶
cv2.
floodFill
(image, mask, seedPoint, newVal[, loDiff[, upDiff[, flags]]]) → retval, rect¶
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 )¶
cv.
FloodFill
(image, seed_point, new_val, lo_diff=(0, 0, 0, 0), up_diff=(0, 0, 0, 0), flags=4, mask=None) → comp¶Parameters: 


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
is considered to belong to the repainted domain if:
in case of a grayscale image and floating range
in case of a grayscale image and fixed range
and
in case of a color image and floating range
and
in case of a color image and fixed range
where 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:
Use these functions to either mark a connected component with the specified color inplace, or build a mask and then extract the contour, or copy the region to another image, and so on.
See also
Note
Calculates the integral of an image.
void integral
(InputArray src, OutputArray sum, int sdepth=1 )¶
void integral
(InputArray src, OutputArray sum, OutputArray sqsum, int sdepth=1 )¶
void integral
(InputArray src, OutputArray sum, OutputArray sqsum, OutputArray tilted, int sdepth=1 )¶
cv2.
integral
(src[, sum[, sdepth]]) → sum¶
cv2.
integral2
(src[, sum[, sqsum[, sdepth]]]) → sum, sqsum¶
cv2.
integral3
(src[, sum[, sqsum[, tilted[, sdepth]]]]) → sum, sqsum, tilted¶
void cvIntegral
(const CvArr* image, CvArr* sum, CvArr* sqsum=NULL, CvArr* tilted_sum=NULL )¶
cv.
Integral
(image, sum, sqsum=None, tiltedSum=None) → None¶Parameters: 


The functions calculate one or more integral images for the source image as follows:
Using these integral images, you can calculate sum, mean, and standard deviation over a specific upright or rotated rectangular region of the image in a constant time, for example:
It makes possible to do a fast blurring or fast block correlation with a variable window size, for example. In case of multichannel images, sums for each channel are accumulated independently.
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
.
Applies a fixedlevel threshold to each array element.
double threshold
(InputArray src, OutputArray dst, double thresh, double maxval, int type)¶
cv2.
threshold
(src, thresh, maxval, type[, dst]) → retval, dst¶
double cvThreshold
(const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type)¶
cv.
Threshold
(src, dst, threshold, maxValue, thresholdType) → None¶Parameters: 


The function applies fixedlevel thresholding
to a singlechannel array. The function is typically used to get a
bilevel (binary) image out of a grayscale image (
compare()
could
be also used for this purpose) or for removing a noise, that is, filtering
out pixels with too small or too large values. There are several
types of thresholding supported by the function. They are determined by type
:
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV
Also, the special value THRESH_OTSU
may be combined with
one of the above values. In this case, the function determines the optimal threshold
value using the Otsu’s algorithm and uses it instead of the specified thresh
.
The function returns the computed threshold value.
Currently, the Otsu’s method is implemented only for 8bit images.
See also
adaptiveThreshold()
,
findContours()
,
compare()
,
min()
,
max()
Performs a markerbased image segmentation using the watershed algorithm.
void watershed
(InputArray image, InputOutputArray markers)¶
void cvWatershed
(const CvArr* image, CvArr* markers)¶
cv2.
watershed
(image, markers) → None¶Parameters: 


The function implements one of the variants of watershed, nonparametric markerbased segmentation algorithm, described in [Meyer92].
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 findContours()
and 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).
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.
See also
Note
Runs the GrabCut algorithm.
void grabCut
(InputArray img, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArray fgdModel, int iterCount, int mode=GC_EVAL )¶
cv2.
grabCut
(img, mask, rect, bgdModel, fgdModel, iterCount[, mode]) → None¶Parameters: 


The function implements the GrabCut image segmentation algorithm.
See the sample grabcut.cpp
to learn how to use the function.
[Borgefors86]  Borgefors, Gunilla, Distance transformations in digital images. Comput. Vision Graph. Image Process. 34 3, pp 344–371 (1986) 
[Felzenszwalb04]  Felzenszwalb, Pedro F. and Huttenlocher, Daniel P. Distance Transforms of Sampled Functions, TR20041963, TR20041963 (2004) 
[Meyer92]  Meyer, F. Color Image Segmentation, ICIP92, 1992 
[Telea04]  Alexandru Telea, An Image Inpainting Technique Based on the Fast Marching Method. Journal of Graphics, GPU, and Game Tools 9 1, pp 2334 (2004) 
Note