Pre-built Python bindings for OpenCV, the comprehensive open-source computer vision and image processing library with 2500+ algorithms
Core operations in OpenCV provide fundamental array manipulation, mathematical operations, and linear algebra capabilities that form the foundation for image processing and computer vision tasks. These operations work on multi-dimensional arrays (Mat objects) and include arithmetic, logical, statistical, and transformation functions.
Element-wise arithmetic operations on arrays with support for saturation arithmetic, masking, and multiple data types.
cv2.add(src1, src2, dst=None, mask=None, dtype=None) -> dstCalculates the per-element sum of two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output array of the same size and type as input arraysmask (ndarray, optional): Optional operation mask, 8-bit single channel arraydtype (int, optional): Optional depth of the output arrayReturns:
dst (ndarray): Output arraycv2.subtract(src1, src2, dst=None, mask=None, dtype=None) -> dstCalculates the per-element difference between two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output array of the same size and type as input arraysmask (ndarray, optional): Optional operation mask, 8-bit single channel arraydtype (int, optional): Optional depth of the output arrayReturns:
dst (ndarray): Output arraycv2.multiply(src1, src2, dst=None, scale=1.0, dtype=None) -> dstCalculates the per-element scaled product of two arrays.
Parameters:
src1 (ndarray): First input arraysrc2 (ndarray): Second input arraydst (ndarray, optional): Output array of the same size and type as input arraysscale (float, optional): Optional scale factordtype (int, optional): Optional depth of the output arrayReturns:
dst (ndarray): Output arraycv2.divide(src1, src2, dst=None, scale=1.0, dtype=None) -> dstPerforms per-element division of two arrays or a scalar by an array.
Parameters:
src1 (ndarray): First input array (dividend)src2 (ndarray): Second input array (divisor)dst (ndarray, optional): Output array of the same size and type as input arraysscale (float, optional): Optional scale factordtype (int, optional): Optional depth of the output arrayReturns:
dst (ndarray): Output arraycv2.absdiff(src1, src2, dst=None) -> dstCalculates the per-element absolute difference between two arrays or between an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of the same size and type as input arrayscv2.addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None) -> dstCalculates the weighted sum of two arrays: dst = src1 * alpha + src2 * beta + gamma.
Parameters:
src1 (ndarray): First input arrayalpha (float): Weight of the first array elementssrc2 (ndarray): Second input array of the same size and channel number as src1beta (float): Weight of the second array elementsgamma (float): Scalar added to each sumdst (ndarray, optional): Output arraydtype (int, optional): Optional depth of the output arrayReturns:
dst (ndarray): Output arrayBitwise logical operations on arrays, commonly used for masking and binary image manipulation.
cv2.bitwise_and(src1, src2, dst=None, mask=None) -> dstCalculates the per-element bit-wise conjunction of two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arraymask (ndarray, optional): Optional operation mask, 8-bit single channel arrayReturns:
dst (ndarray): Output arraycv2.bitwise_or(src1, src2, dst=None, mask=None) -> dstCalculates the per-element bit-wise disjunction of two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arraymask (ndarray, optional): Optional operation mask, 8-bit single channel arrayReturns:
dst (ndarray): Output arraycv2.bitwise_xor(src1, src2, dst=None, mask=None) -> dstCalculates the per-element bit-wise "exclusive or" operation on two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arraymask (ndarray, optional): Optional operation mask, 8-bit single channel arrayReturns:
dst (ndarray): Output arraycv2.bitwise_not(src, dst=None, mask=None) -> dstInverts every bit of an array.
Parameters:
src (ndarray): Input arraydst (ndarray, optional): Output arraymask (ndarray, optional): Optional operation mask, 8-bit single channel arrayReturns:
dst (ndarray): Output arrayElement-wise mathematical functions for arrays including square root, power, exponential, and logarithm.
cv2.sqrt(src, dst=None) -> dstCalculates a square root of array elements.
Parameters:
src (ndarray): Input floating-point arraydst (ndarray, optional): Output array of the same size and type as srcReturns:
dst (ndarray): Output array containing square roots of input elementscv2.pow(src, power, dst=None) -> dstRaises every array element to a power.
Parameters:
src (ndarray): Input arraypower (float): Exponent of powerdst (ndarray, optional): Output array of the same size and type as srcReturns:
dst (ndarray): Output array with each element raised to the specified powercv2.exp(src, dst=None) -> dstCalculates the exponent of every array element.
Parameters:
src (ndarray): Input arraydst (ndarray, optional): Output array of the same size and type as srcReturns:
dst (ndarray): Output array where dst[I] = e^(src(I))cv2.log(src, dst=None) -> dstCalculates the natural logarithm of every array element.
Parameters:
src (ndarray): Input arraydst (ndarray, optional): Output array of the same size and type as srcReturns:
dst (ndarray): Output array where dst(I) = log(src(I)). Output on zero, negative and special (NaN, Inf) values is undefinedcv2.magnitude(x, y, magnitude=None) -> magnitudeCalculates the magnitude of 2D vectors.
Parameters:
x (ndarray): Floating-point array of x-coordinates of the vectorsy (ndarray): Floating-point array of y-coordinates of the vectors (same size as x)magnitude (ndarray, optional): Output array of the same size and type as xReturns:
magnitude (ndarray): Output array where magnitude(I) = sqrt(x(I)^2 + y(I)^2)cv2.phase(x, y, angle=None, angleInDegrees=False) -> angleCalculates the rotation angle of 2D vectors.
Parameters:
x (ndarray): Floating-point array of x-coordinates of the vectorsy (ndarray): Floating-point array of y-coordinates of the vectors (same size as x)angle (ndarray, optional): Output array of angles (in radians or degrees)angleInDegrees (bool): When true, the function calculates the angle in degrees, otherwise in radiansReturns:
angle (ndarray): Output array of vector anglescv2.cartToPolar(x, y, magnitude=None, angle=None, angleInDegrees=False) -> magnitude, angleCalculates the magnitude and angle of 2D vectors.
Parameters:
x (ndarray): Array of x-coordinates (must be single-precision or double-precision floating-point)y (ndarray): Array of y-coordinates (same size and type as x)magnitude (ndarray, optional): Output array of magnitudesangle (ndarray, optional): Output array of angles (in radians or degrees)angleInDegrees (bool): When true, the angles are measured in degrees (0-360), otherwise in radians (0-2*pi)Returns:
magnitude (ndarray): Output array of magnitudesangle (ndarray): Output array of anglescv2.polarToCart(magnitude, angle, x=None, y=None, angleInDegrees=False) -> x, yCalculates x and y coordinates of 2D vectors from their magnitude and angle.
Parameters:
magnitude (ndarray): Input array of magnitudes (floating-point)angle (ndarray): Input array of angles (in radians or degrees)x (ndarray, optional): Output array of x-coordinates (same size and type as magnitude)y (ndarray, optional): Output array of y-coordinates (same size and type as magnitude)angleInDegrees (bool): When true, the input angles are measured in degrees, otherwise in radiansReturns:
x (ndarray): Output array of x-coordinatesy (ndarray): Output array of y-coordinatescv2.scaleAdd(src1, alpha, src2, dst=None) -> dstCalculates the sum of a scaled array and another array.
Parameters:
src1 (ndarray): First input arrayalpha (float): Scale factor for the first arraysrc2 (ndarray): Second input array (same size and type as src1)dst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array where dst = src1 * alpha + src2Element-wise comparison and minimum/maximum operations for array analysis and thresholding.
cv2.compare(src1, src2, cmpop) -> dstPerforms per-element comparison of two arrays.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalarcmpop (int): Flag specifying the relation between elements to be checked
cv2.CMP_EQ: src1 equal to src2cv2.CMP_GT: src1 greater than src2cv2.CMP_GE: src1 greater than or equal to src2cv2.CMP_LT: src1 less than src2cv2.CMP_LE: src1 less than or equal to src2cv2.CMP_NE: src1 not equal to src2Returns:
dst (ndarray): Output array of type CV_8U with elements set to 255 (true) or 0 (false)cv2.min(src1, src2, dst=None) -> dstCalculates per-element minimum of two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of the same size and type as input arrayscv2.max(src1, src2, dst=None) -> dstCalculates per-element maximum of two arrays or an array and a scalar.
Parameters:
src1 (ndarray): First input array or scalarsrc2 (ndarray): Second input array or scalardst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of the same size and type as input arraysStatistical operations for analyzing array contents, including norms, means, and extrema.
cv2.mean(src, mask=None) -> retvalCalculates an average (mean) of array elements.
Parameters:
src (ndarray): Input array that should have from 1 to 4 channels so that the result can be stored in Scalar_mask (ndarray, optional): Optional operation maskReturns:
retval (Scalar): Mean value for each channel. When all mask elements are 0, the function returns Scalar::all(0)cv2.norm(src1, normType=None, mask=None) -> retval
cv2.norm(src1, src2, normType=None, mask=None) -> retvalCalculates an absolute array norm, absolute difference norm, or relative difference norm.
Parameters:
src1 (ndarray): First input arraysrc2 (ndarray, optional): Second input array of the same size and type as src1normType (int, optional): Type of norm to calculate
cv2.NORM_INF: Max normcv2.NORM_L1: L1 normcv2.NORM_L2: L2 norm (default)cv2.NORM_L2SQR: Squared L2 normcv2.NORM_HAMMING: Hamming normcv2.NORM_HAMMING2: Hamming norm with two bits per elementmask (ndarray, optional): Optional operation mask, 8-bit single channel arrayReturns:
retval (float): Calculated norm valuecv2.normalize(src, dst=None, alpha=1.0, beta=0.0, norm_type=cv2.NORM_L2,
dtype=None, mask=None) -> dstNormalizes the norm or value range of an array.
Parameters:
src (ndarray): Input arraydst (ndarray, optional): Output array of the same size as srcalpha (float): Norm value to normalize to or lower range boundary in range normalizationbeta (float): Upper range boundary in range normalizationnorm_type (int): Normalization type
cv2.NORM_INF, cv2.NORM_L1, cv2.NORM_L2: For norm normalizationcv2.NORM_MINMAX: For range normalizationdtype (int, optional): Output array depthmask (ndarray, optional): Optional operation maskReturns:
dst (ndarray): Normalized arraycv2.meanStdDev(src, mean=None, stddev=None, mask=None) -> mean, stddevCalculates mean and standard deviation of array elements.
Parameters:
src (ndarray): Input arraymean (ndarray, optional): Output parameter for calculated meanstddev (ndarray, optional): Output parameter for calculated standard deviationmask (ndarray, optional): Optional operation maskReturns:
mean (ndarray): Mean value of array elements per channelstddev (ndarray): Standard deviation of array elements per channelcv2.minMaxLoc(src, mask=None) -> minVal, maxVal, minLoc, maxLocFinds the global minimum and maximum in an array.
Parameters:
src (ndarray): Input single-channel arraymask (ndarray, optional): Optional mask to select a sub-arrayReturns:
minVal (float): Minimum valuemaxVal (float): Maximum valueminLoc (tuple): Position of minimum value (x, y)maxLoc (tuple): Position of maximum value (x, y)cv2.countNonZero(src) -> retvalCounts non-zero array elements.
Parameters:
src (ndarray): Single-channel arrayReturns:
retval (int): Number of non-zero elementscv2.sum(src) -> retval
cv2.sumElems(src) -> retvalCalculates the sum of array elements.
Parameters:
src (ndarray): Input arrayReturns:
retval (Scalar): Sum of array elements per channelOperations for manipulating multi-channel arrays, essential for working with color images.
cv2.split(src) -> mvDivides a multi-channel array into several single-channel arrays.
Parameters:
src (ndarray): Input multi-channel arrayReturns:
mv (list of ndarray): List of single-channel arrayscv2.merge(mv, dst=None) -> dstMerges several single-channel arrays into a multi-channel array.
Parameters:
mv (list of ndarray): Input list of single-channel arrays to be mergeddst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output multi-channel arraycv2.mixChannels(src, dst, fromTo) -> dstCopies specified channels from input arrays to specified channels of output arrays.
Parameters:
src (list of ndarray): Input array or list of input arraysdst (list of ndarray): Output array or list of output arraysfromTo (list of int): List of index pairs specifying which channels are copied
Returns:
dst (list of ndarray): Output arrayscv2.extractChannel(src, coi, dst=None) -> dstExtracts a single channel from a multi-channel array.
Parameters:
src (ndarray): Input multi-channel arraycoi (int): Index of channel to extract (0-based)dst (ndarray, optional): Output arrayReturns:
dst (ndarray): Single-channel output arraycv2.insertChannel(src, dst, coi) -> dstInserts a single channel into a multi-channel array.
Parameters:
src (ndarray): Input single-channel array to be inserteddst (ndarray): Target multi-channel arraycoi (int): Index of channel to insert into (0-based)Returns:
dst (ndarray): Modified multi-channel arrayGeometric transformations and manipulations of array structure.
cv2.flip(src, flipCode, dst=None) -> dstFlips an array around vertical, horizontal, or both axes.
Parameters:
src (ndarray): Input arrayflipCode (int): Flag to specify how to flip the array
0: Flip vertically (around x-axis)> 0: Flip horizontally (around y-axis)< 0: Flip both vertically and horizontallydst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of the same type as srccv2.rotate(src, rotateCode, dst=None) -> dstRotates an array by 90, 180, or 270 degrees.
Parameters:
src (ndarray): Input arrayrotateCode (int): Rotation direction
cv2.ROTATE_90_CLOCKWISE: Rotate 90 degrees clockwisecv2.ROTATE_180: Rotate 180 degreescv2.ROTATE_90_COUNTERCLOCKWISE: Rotate 90 degrees counterclockwisedst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output rotated arraycv2.transpose(src, dst=None) -> dstTransposes a matrix (swaps rows and columns).
Parameters:
src (ndarray): Input arraydst (ndarray, optional): Output arrayReturns:
dst (ndarray): Transposed arraycv2.repeat(src, ny, nx, dst=None) -> dstFills the output array with repeated copies of the input array.
Parameters:
src (ndarray): Input array to replicateny (int): Number of times to repeat the array along the vertical axisnx (int): Number of times to repeat the array along the horizontal axisdst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output arraycv2.hconcat(src, dst=None) -> dstApplies horizontal concatenation to given matrices.
Parameters:
src (list of ndarray): Input arrays to concatenate (must have the same number of rows)dst (ndarray, optional): Output arrayReturns:
dst (ndarray): Horizontally concatenated arraycv2.vconcat(src, dst=None) -> dstApplies vertical concatenation to given matrices.
Parameters:
src (list of ndarray): Input arrays to concatenate (must have the same number of columns)dst (ndarray, optional): Output arrayReturns:
dst (ndarray): Vertically concatenated arrayOperations for value range checking and lookup table transformations.
cv2.inRange(src, lowerb, upperb, dst=None) -> dstChecks if array elements lie between the elements of two other arrays or scalars.
Parameters:
src (ndarray): Input arraylowerb (ndarray or scalar): Inclusive lower boundary array or scalarupperb (ndarray or scalar): Inclusive upper boundary array or scalardst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of type CV_8U with elements set to 255 (within range) or 0 (outside range)cv2.LUT(src, lut, dst=None) -> dstPerforms a look-up table transform of an array.
Parameters:
src (ndarray): Input array of 8-bit elementslut (ndarray): Look-up table of 256 elements; should have the same depth as the output arraydst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array of the same size and number of channels as srcOperations that reduce arrays to vectors or scalars.
cv2.reduce(src, dim, rtype, dst=None, dtype=None) -> dstReduces a matrix to a vector.
Parameters:
src (ndarray): Input 2D matrixdim (int): Dimension index along which the reduction is performed
0: Reduce to a single row1: Reduce to a single columnrtype (int): Reduction operation type
cv2.REDUCE_SUM: Sum over all rows/columnscv2.REDUCE_AVG: Mean over all rows/columnscv2.REDUCE_MAX: Maximum over all rows/columnscv2.REDUCE_MIN: Minimum over all rows/columnsdst (ndarray, optional): Output vectordtype (int, optional): Output array depthReturns:
dst (ndarray): Output vectorMatrix operations and linear algebra computations.
cv2.solve(src1, src2, flags=cv2.DECOMP_LU) -> retval, dstSolves one or more linear systems or least-squares problems.
Parameters:
src1 (ndarray): Input matrix on the left-hand side of the systemsrc2 (ndarray): Input matrix on the right-hand side of the systemflags (int): Solution method
cv2.DECOMP_LU: Gaussian elimination with optimal pivot elementcv2.DECOMP_SVD: Singular value decomposition (SVD)cv2.DECOMP_EIG: Eigenvalue decompositioncv2.DECOMP_CHOLESKY: Cholesky factorizationcv2.DECOMP_QR: QR factorizationcv2.DECOMP_NORMAL: Use normal equations (for overdetermined systems)Returns:
retval (bool): True if the system has a solutiondst (ndarray): Output solutioncv2.invert(src, flags=cv2.DECOMP_LU) -> retval, dstFinds the inverse or pseudo-inverse of a matrix.
Parameters:
src (ndarray): Input floating-point M×N matrixflags (int): Inversion method
cv2.DECOMP_LU: Gaussian elimination (for square matrices)cv2.DECOMP_SVD: Singular value decomposition (for any matrices)cv2.DECOMP_CHOLESKY: Cholesky factorization (for symmetric positive-definite matrices)Returns:
retval (float): Inverse condition number (DECOMP_LU) or 0 if singulardst (ndarray): Output inverse matrixcv2.eigen(src, computeEigenvectors=True) -> retval, eigenvalues, eigenvectorsCalculates eigenvalues and eigenvectors of a symmetric matrix.
Parameters:
src (ndarray): Input symmetric square matrixcomputeEigenvectors (bool): Flag indicating whether eigenvectors should be computedReturns:
retval (bool): True if eigenvalues/eigenvectors were computed successfullyeigenvalues (ndarray): Output vector of eigenvalues (sorted in descending order)eigenvectors (ndarray): Output matrix of eigenvectors (one per row)cv2.determinant(src) -> retvalReturns the determinant of a square floating-point matrix.
Parameters:
src (ndarray): Input square matrixReturns:
retval (float): Determinant of the matrixcv2.SVDecomp(src, flags=0) -> retval, w, u, vtPerforms singular value decomposition of a matrix.
Parameters:
src (ndarray): Input M×N matrixflags (int): Operation flags
cv2.SVD_MODIFY_A: Allows the function to modify the input matrixcv2.SVD_NO_UV: Only singular values are computedcv2.SVD_FULL_UV: Full-size U and V are computedReturns:
retval (bool): True if decomposition was successfulw (ndarray): Output vector of singular valuesu (ndarray): Output left singular vectors (M×M or M×min(M,N))vt (ndarray): Output right singular vectors transposed (N×N or min(M,N)×N)cv2.PCACompute(data, mean, maxComponents=0) -> mean, eigenvectors, eigenvalues
cv2.PCACompute2(data, mean, maxComponents=0, retainedVariance=0) -> mean, eigenvectors, eigenvaluesPerforms Principal Component Analysis on a set of vectors.
Parameters:
data (ndarray): Input data matrix where each row is a sample vectormean (ndarray): Input/output mean vector (if empty, computed from data)maxComponents (int): Maximum number of components to retainretainedVariance (float, PCACompute2 only): Percentage of variance to retain (0-1)Returns:
mean (ndarray): Mean vector of the input dataeigenvectors (ndarray): Principal components (eigenvectors of covariance matrix)eigenvalues (ndarray): Eigenvalues corresponding to the eigenvectorscv2.gemm(src1, src2, alpha, src3, beta, flags=0, dst=None) -> dstPerforms generalized matrix multiplication: dst = alpha * src1 * src2 + beta * src3.
Parameters:
src1 (ndarray): First input matrixsrc2 (ndarray): Second input matrixalpha (float): Weight of the matrix productsrc3 (ndarray): Third input matrix added to the productbeta (float): Weight of src3flags (int): Operation flags
cv2.GEMM_1_T: Transpose src1cv2.GEMM_2_T: Transpose src2cv2.GEMM_3_T: Transpose src3dst (ndarray, optional): Output matrixReturns:
dst (ndarray): Output matrixcv2.transform(src, m, dst=None) -> dstPerforms matrix transformation of every array element.
Parameters:
src (ndarray): Input array (must be of floating-point type)m (ndarray): Transformation matrix (2×2, 2×3, 3×3, or 3×4)dst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array with the same size and depth as srccv2.perspectiveTransform(src, m, dst=None) -> dstPerforms perspective transformation of vectors.
Parameters:
src (ndarray): Input two-channel or three-channel floating-point array (each element is a 2D/3D vector)m (ndarray): 3×3 or 4×4 floating-point transformation matrixdst (ndarray, optional): Output arrayReturns:
dst (ndarray): Output array with the same size and type as srccv2.Mahalanobis(v1, v2, icovar) -> retvalCalculates the Mahalanobis distance between two vectors.
Parameters:
v1 (ndarray): First input vectorv2 (ndarray): Second input vectoricovar (ndarray): Inverse covariance matrixReturns:
retval (float): Mahalanobis distancecv2.mulTransposed(src, aTa, dst=None, delta=None, scale=1.0, dtype=None) -> dstCalculates the product of a matrix and its transpose.
Parameters:
src (ndarray): Input single-channel matrixaTa (bool): Flag specifying multiplication ordering
True: dst = scale * (src - delta)^T * (src - delta)False: dst = scale * (src - delta) * (src - delta)^Tdst (ndarray, optional): Output square matrixdelta (ndarray, optional): Optional delta matrix subtracted from srcscale (float): Optional scale factordtype (int, optional): Output matrix depthReturns:
dst (ndarray): Output matrixcv2.setIdentity(mtx, s=1.0) -> mtxInitializes a scaled identity matrix.
Parameters:
mtx (ndarray): Matrix to initialize (not necessarily square)s (Scalar): Value to assign to diagonal elementsReturns:
mtx (ndarray): Modified matrixRandom number generation and shuffling operations.
cv2.setRNGSeed(seed) -> NoneSets the state of the default random number generator.
Parameters:
seed (int): New random number generator seed valueReturns:
cv2.randu(dst, low, high) -> dstGenerates a uniformly distributed random numbers.
Parameters:
dst (ndarray): Output array of random numbers (pre-allocated)low (ndarray or scalar): Inclusive lower boundary of generated random numbershigh (ndarray or scalar): Exclusive upper boundary of generated random numbersReturns:
dst (ndarray): Output array filled with random numberscv2.randn(dst, mean, stddev) -> dstFills the array with normally distributed random numbers.
Parameters:
dst (ndarray): Output array of random numbers (pre-allocated)mean (ndarray or scalar): Mean value (expectation) of the generated random numbersstddev (ndarray or scalar): Standard deviation of the generated random numbersReturns:
dst (ndarray): Output array filled with random numberscv2.randShuffle(dst, iterFactor=1.0) -> dstShuffles the array elements randomly.
Parameters:
dst (ndarray): Input/output array to shuffleiterFactor (float): Scale factor that determines the number of random swap operationsReturns:
dst (ndarray): Shuffled arrayK-means clustering and advanced statistical computations.
cv2.kmeans(data, K, bestLabels, criteria, attempts, flags) -> retval, bestLabels, centersFinds centers of clusters and groups input samples around the clusters.
Parameters:
data (ndarray): Data for clustering (each row is a sample, floating-point type)K (int): Number of clusters to split the set intobestLabels (ndarray): Input/output integer array storing cluster indices for every samplecriteria (tuple): Termination criteria (type, max_iter, epsilon)
cv2.TERM_CRITERIA_EPS, cv2.TERM_CRITERIA_MAX_ITER, or bothattempts (int): Number of times the algorithm is executed with different initial labelingsflags (int): Flag to specify initial center selection
cv2.KMEANS_RANDOM_CENTERS: Random initial centerscv2.KMEANS_PP_CENTERS: K-means++ center initializationcv2.KMEANS_USE_INITIAL_LABELS: Use supplied bestLabels as initial labelingReturns:
retval (float): Compactness measure (sum of squared distances from samples to centers)bestLabels (ndarray): Output integer array of labels (cluster indices)centers (ndarray): Output matrix of cluster centers (one row per cluster)Discrete Fourier Transform operations for frequency domain analysis.
cv2.dft(src, dst=None, flags=0, nonzeroRows=0) -> dstPerforms a forward or inverse Discrete Fourier Transform of a 1D or 2D array.
Parameters:
src (ndarray): Input array that could be real or complexdst (ndarray, optional): Output array whose size and type depends on the flagsflags (int, optional): Transformation flags, representing a combination of DftFlags:
cv2.DFT_INVERSE: Perform inverse transformcv2.DFT_SCALE: Scale the resultcv2.DFT_ROWS: Perform forward or inverse transform of every rowcv2.DFT_COMPLEX_OUTPUT: Output is complex array with 2 channelscv2.DFT_REAL_OUTPUT: Output is real array (for inverse transform only)nonzeroRows (int, optional): When not zero, the function assumes that only the first nonzeroRows rows of the input array contain non-zerosReturns:
dst (ndarray): Output arraycv2.idft(src, dst=None, flags=0, nonzeroRows=0) -> dstCalculates the inverse Discrete Fourier Transform of a 1D or 2D array.
Parameters:
src (ndarray): Input floating-point real or complex arraydst (ndarray, optional): Output array whose size and type depend on the flagsflags (int, optional): Operation flags (see dft and DftFlags)nonzeroRows (int, optional): Number of dst rows to process; the rest have undefined contentReturns:
dst (ndarray): Output array. Note: idft(src, dst, flags) is equivalent to dft(src, dst, flags | DFT_INVERSE). Neither dft nor idft scales the result by default, so you should pass DFT_SCALE to one of them explicitly to make these transforms mutually inverseOperations for creating borders around images.
cv2.copyMakeBorder(src, top, bottom, left, right, borderType, dst=None, value=None) -> dstForms a border around an image.
Parameters:
src (ndarray): Source imagetop (int): Number of top pixels to extrapolatebottom (int): Number of bottom pixels to extrapolateleft (int): Number of left pixels to extrapolateright (int): Number of right pixels to extrapolateborderType (int): Border type. See borderInterpolate for details:
cv2.BORDER_CONSTANT: Border filled with constant valuecv2.BORDER_REPLICATE: Border filled with replicated edge pixelscv2.BORDER_REFLECT: Border reflects border elementscv2.BORDER_WRAP: Border wraps aroundcv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT: Similar to BORDER_REFLECT but with slight differencedst (ndarray, optional): Destination image of the same type as src and size Size(src.cols+left+right, src.rows+top+bottom)value (Scalar, optional): Border value if borderType==BORDER_CONSTANTReturns:
dst (ndarray): Output image with border. The function copies the source image into the middle of the destination image and fills border areasInstall with Tessl CLI
npx tessl i tessl/pypi-opencv-python@4.12.1