OpenCV  4.1.0 Open Source Computer Vision
Graph API: Math operations

## Functions

GMat cv::gapi::add (const GMat &src1, const GMat &src2, int ddepth=-1)
Calculates the per-element sum of two matrices.

GMat cv::gapi::addC (const GMat &src1, const GScalar &c, int ddepth=-1)
Calculates the per-element sum of matrix and given scalar.

GMat cv::gapi::addC (const GScalar &c, const GMat &src1, int ddepth=-1)

std::tuple< GMat, GMat > cv::gapi::cartToPolar (const GMat &x, const GMat &y, bool angleInDegrees=false)
Calculates the magnitude and angle of 2D vectors.

GMat cv::gapi::div (const GMat &src1, const GMat &src2, double scale, int ddepth=-1)
Performs per-element division of two matrices.

GMat cv::gapi::divC (const GMat &src, const GScalar &divisor, double scale, int ddepth=-1)
Divides matrix by scalar.

GMat cv::gapi::divRC (const GScalar &divident, const GMat &src, double scale, int ddepth=-1)
Divides scalar by matrix.

GMat cv::gapi::mask (const GMat &src, const GMat &mask)
Applies a mask to a matrix.

GScalar cv::gapi::mean (const GMat &src)
Calculates an average (mean) of matrix elements.

GMat cv::gapi::mul (const GMat &src1, const GMat &src2, double scale=1.0, int ddepth=-1)
Calculates the per-element scaled product of two matrices.

GMat cv::gapi::mulC (const GMat &src, double multiplier, int ddepth=-1)
Multiplies matrix by scalar.

GMat cv::gapi::mulC (const GMat &src, const GScalar &multiplier, int ddepth=-1)

GMat cv::gapi::mulC (const GScalar &multiplier, const GMat &src, int ddepth=-1)

GMat cv::gapi::phase (const GMat &x, const GMat &y, bool angleInDegrees=false)
Calculates the rotation angle of 2D vectors.

std::tuple< GMat, GMat > cv::gapi::polarToCart (const GMat &magnitude, const GMat &angle, bool angleInDegrees=false)
Calculates x and y coordinates of 2D vectors from their magnitude and angle.

GMat cv::gapi::sqrt (const GMat &src)
Calculates a square root of array elements.

GMat cv::gapi::sub (const GMat &src1, const GMat &src2, int ddepth=-1)
Calculates the per-element difference between two matrices.

GMat cv::gapi::subC (const GMat &src, const GScalar &c, int ddepth=-1)
Calculates the per-element difference between matrix and given scalar.

GMat cv::gapi::subRC (const GScalar &c, const GMat &src, int ddepth=-1)
Calculates the per-element difference between given scalar and the matrix.

## Function Documentation

 GMat cv::gapi::add ( const GMat & src1, const GMat & src2, int ddepth = -1 )

Calculates the per-element sum of two matrices.

The function add calculates sum of two matrices of the same size and the same number of channels:

$\texttt{dst}(I) = \texttt{saturate} ( \texttt{src1}(I) + \texttt{src2}(I)) \quad \texttt{if mask}(I) \ne0$

The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{src1} + \texttt{src2}$

The input matrices and the output matrix can all have the same or different depths. For example, you can add a 16-bit unsigned matrix to a 8-bit signed matrix and store the sum as a 32-bit floating-point matrix. Depth of the output matrix is determined by the ddepth parameter. If src1.depth() == src2.depth(), ddepth can be set to the default -1. In this case, the output matrix will have the same depth as the input matrices.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.add"
Parameters
 src1 first input matrix. src2 second input matrix. ddepth optional depth of the output matrix.
 GMat cv::gapi::addC ( const GMat & src1, const GScalar & c, int ddepth = -1 )

Calculates the per-element sum of matrix and given scalar.

The function addC adds a given scalar value to each element of given matrix. The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{src1} + \texttt{c}$

Depth of the output matrix is determined by the ddepth parameter. If ddepth is set to default -1, the depth of output matrix will be the same as the depth of input matrix. The matrices can be single or multi channel. Output matrix must have the same size and number of channels as the input matrix.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.addC"
Parameters
 src1 first input matrix. c scalar value to be added. ddepth optional depth of the output matrix.
 GMat cv::gapi::addC ( const GScalar & c, const GMat & src1, int ddepth = -1 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

 std::tuple cv::gapi::cartToPolar ( const GMat & x, const GMat & y, bool angleInDegrees = false )

Calculates the magnitude and angle of 2D vectors.

The function cartToPolar calculates either the magnitude, angle, or both for every 2D vector (x(I),y(I)):

$\begin{array}{l} \texttt{magnitude} (I)= \sqrt{\texttt{x}(I)^2+\texttt{y}(I)^2} , \\ \texttt{angle} (I)= \texttt{atan2} ( \texttt{y} (I), \texttt{x} (I))[ \cdot180 / \pi ] \end{array}$

The angles are calculated with accuracy about 0.3 degrees. For the point (0,0), the angle is set to 0.

First output is a matrix of magnitudes of the same size and depth as input x. Second output is a matrix of angles that has the same size and depth as x; the angles are measured in radians (from 0 to 2*Pi) or in degrees (0 to 360 degrees).

Note
Function textual ID is "org.opencv.core.math.cartToPolar"
Parameters
 x matrix of CV_32FC1 x-coordinates. y array of CV_32FC1 y-coordinates. angleInDegrees a flag, indicating whether the angles are measured in radians (which is by default), or in degrees.
polarToCart
 GMat cv::gapi::div ( const GMat & src1, const GMat & src2, double scale, int ddepth = -1 )

Performs per-element division of two matrices.

The function divides one matrix by another:

$\texttt{dst(I) = saturate(src1(I)*scale/src2(I))}$

When src2(I) is zero, dst(I) will also be zero. Different channels of multi-channel matrices are processed independently. The matrices can be single or multi channel. Output matrix must have the same size and depth as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.div"
Parameters
 src1 first input matrix. src2 second input matrix of the same size and depth as src1. scale scalar factor. ddepth optional depth of the output matrix; you can only pass -1 when src1.depth() == src2.depth().
 GMat cv::gapi::divC ( const GMat & src, const GScalar & divisor, double scale, int ddepth = -1 )

Divides matrix by scalar.

The function divC divides each element of matrix src by given scalar value:

$\texttt{dst(I) = saturate(src(I)*scale/divisor)}$

When divisor is zero, dst(I) will also be zero. Different channels of multi-channel matrices are processed independently. The matrices can be single or multi channel. Output matrix must have the same size and depth as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.divC"
Parameters
 src input matrix. divisor number to be divided by. ddepth optional depth of the output matrix. If -1, the depth of output matrix will be the same as input matrix depth. scale scale factor.
 GMat cv::gapi::divRC ( const GScalar & divident, const GMat & src, double scale, int ddepth = -1 )

Divides scalar by matrix.

The function divRC divides given scalar by each element of matrix src and keep the division result in new matrix of the same size and type as src:

$\texttt{dst(I) = saturate(divident*scale/src(I))}$

When src(I) is zero, dst(I) will also be zero. Different channels of multi-channel matrices are processed independently. The matrices can be single or multi channel. Output matrix must have the same size and depth as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.divRC"
Parameters
 src input matrix. divident number to be divided. ddepth optional depth of the output matrix. If -1, the depth of output matrix will be the same as input matrix depth. scale scale factor
 GMat cv::gapi::mask ( const GMat & src, const GMat & mask )

Applies a mask to a matrix.

The function mask set value from given matrix if the corresponding pixel value in mask matrix set to true, and set the matrix value to 0 overwise.

Supported src matrix data types are CV_8UC1, CV_16SC1, CV_16UC1. Supported mask data type is CV_8UC1.

Note
Function textual ID is "org.opencv.core.math.mask"
Parameters
 src input matrix. mask input mask matrix.
Examples:
samples/cpp/camshiftdemo.cpp, samples/cpp/create_mask.cpp, samples/cpp/ffilldemo.cpp, samples/cpp/grabcut.cpp, samples/cpp/segment_objects.cpp, samples/cpp/stitching_detailed.cpp, samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp, and samples/cpp/tutorial_code/photo/seamless_cloning/cloning_demo.cpp.
 GScalar cv::gapi::mean ( const GMat & src )

Calculates an average (mean) of matrix elements.

The function mean calculates the mean value M of matrix elements, independently for each channel, and return it.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.mean"
Parameters
 src input matrix.
 GMat cv::gapi::mul ( const GMat & src1, const GMat & src2, double scale = 1.0, int ddepth = -1 )

Calculates the per-element scaled product of two matrices.

The function mul calculates the per-element product of two matrices:

$\texttt{dst} (I)= \texttt{saturate} ( \texttt{scale} \cdot \texttt{src1} (I) \cdot \texttt{src2} (I))$

If src1.depth() == src2.depth(), ddepth can be set to the default -1. In this case, the output matrix will have the same depth as the input matrices. The matrices can be single or multi channel. Output matrix must have the same size as input matrices.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.mul"
Parameters
 src1 first input matrix. src2 second input matrix of the same size and the same depth as src1. scale optional scale factor. ddepth optional depth of the output matrix.
 GMat cv::gapi::mulC ( const GMat & src, double multiplier, int ddepth = -1 )

Multiplies matrix by scalar.

The function mulC multiplies each element of matrix src by given scalar value:

$\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I) \cdot \texttt{multiplier} )$

The matrices can be single or multi channel. Output matrix must have the same size as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.mulC"
Parameters
 src input matrix. multiplier factor to be multiplied. ddepth optional depth of the output matrix. If -1, the depth of output matrix will be the same as input matrix depth.
 GMat cv::gapi::mulC ( const GMat & src, const GScalar & multiplier, int ddepth = -1 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

 GMat cv::gapi::mulC ( const GScalar & multiplier, const GMat & src, int ddepth = -1 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

 GMat cv::gapi::phase ( const GMat & x, const GMat & y, bool angleInDegrees = false )

Calculates the rotation angle of 2D vectors.

The function cv::phase calculates the rotation angle of each 2D vector that is formed from the corresponding elements of x and y :

$\texttt{angle} (I) = \texttt{atan2} ( \texttt{y} (I), \texttt{x} (I))$

The angle estimation accuracy is about 0.3 degrees. When x(I)=y(I)=0 , the corresponding angle(I) is set to 0.

Parameters
 x input floating-point array of x-coordinates of 2D vectors. y input array of y-coordinates of 2D vectors; it must have the same size and the same type as x. angleInDegrees when true, the function calculates the angle in degrees, otherwise, they are measured in radians.
Returns
array of vector angles; it has the same size and same type as x.
 std::tuple cv::gapi::polarToCart ( const GMat & magnitude, const GMat & angle, bool angleInDegrees = false )

Calculates x and y coordinates of 2D vectors from their magnitude and angle.

The function polarToCart calculates the Cartesian coordinates of each 2D vector represented by the corresponding elements of magnitude and angle:

$\begin{array}{l} \texttt{x} (I) = \texttt{magnitude} (I) \cos ( \texttt{angle} (I)) \\ \texttt{y} (I) = \texttt{magnitude} (I) \sin ( \texttt{angle} (I)) \\ \end{array}$

The relative accuracy of the estimated coordinates is about 1e-6.

First output is a matrix of x-coordinates of 2D vectors. Second output is a matrix of y-coordinates of 2D vectors. Both output must have the same size and depth as input matrices.

Note
Function textual ID is "org.opencv.core.math.polarToCart"
Parameters
 magnitude input floating-point CV_32FC1 matrix (1xN) of magnitudes of 2D vectors; angle input floating-point CV_32FC1 matrix (1xN) of angles of 2D vectors. angleInDegrees when true, the input angles are measured in degrees, otherwise, they are measured in radians.
cartToPolar, exp, log, pow, sqrt
 GMat cv::gapi::sqrt ( const GMat & src )

Calculates a square root of array elements.

The function cv::gapi::sqrt calculates a square root of each input array element. In case of multi-channel arrays, each channel is processed independently. The accuracy is approximately the same as of the built-in std::sqrt .

Parameters
 src input floating-point array.
Returns
output array of the same size and type as src.
 GMat cv::gapi::sub ( const GMat & src1, const GMat & src2, int ddepth = -1 )

Calculates the per-element difference between two matrices.

The function sub calculates difference between two matrices, when both matrices have the same size and the same number of channels:

$\texttt{dst}(I) = \texttt{src1}(I) - \texttt{src2}(I)$

The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{src1} - \texttt{src2}$

The input matrices and the output matrix can all have the same or different depths. For example, you can subtract two 8-bit unsigned matrices store the result as a 16-bit signed matrix. Depth of the output matrix is determined by the ddepth parameter. If src1.depth() == src2.depth(), ddepth can be set to the default -1. In this case, the output matrix will have the same depth as the input matrices. The matrices can be single or multi channel.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.sub"
Parameters
 src1 first input matrix. src2 second input matrix. ddepth optional depth of the output matrix.
 GMat cv::gapi::subC ( const GMat & src, const GScalar & c, int ddepth = -1 )

Calculates the per-element difference between matrix and given scalar.

The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{src} - \texttt{c}$

Depth of the output matrix is determined by the ddepth parameter. If ddepth is set to default -1, the depth of output matrix will be the same as the depth of input matrix. The matrices can be single or multi channel. Output matrix must have the same size as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.subC"
Parameters
 src first input matrix. c scalar value to subtracted. ddepth optional depth of the output matrix.
 GMat cv::gapi::subRC ( const GScalar & c, const GMat & src, int ddepth = -1 )

Calculates the per-element difference between given scalar and the matrix.

The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{val} - \texttt{src}$

Depth of the output matrix is determined by the ddepth parameter. If ddepth is set to default -1, the depth of output matrix will be the same as the depth of input matrix. The matrices can be single or multi channel. Output matrix must have the same size as src.

Supported matrix data types are CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1.

Note
Function textual ID is "org.opencv.core.math.subRC"
Parameters
 c scalar value to subtract from. src input matrix to be subtracted. ddepth optional depth of the output matrix.