OpenCV  4.6.0-dev 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. More...

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

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

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

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

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

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

Applies a mask to a matrix. More...

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

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. More...

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

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. More...

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

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

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

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

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

## Function Documentation

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

#include <opencv2/gapi/core.hpp>

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
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 )
Python:

#include <opencv2/gapi/core.hpp>

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
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 )
Python:

#include <opencv2/gapi/core.hpp>

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

## ◆ cartToPolar()

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

#include <opencv2/gapi/core.hpp>

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

## ◆ div()

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

#include <opencv2/gapi/core.hpp>

Performs per-element division of two matrices.

The function divides one matrix by another:

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

For integer types when src2(I) is zero, dst(I) will also be zero. Floating point case returns Inf/NaN (according to IEEE).

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().

## ◆ divC()

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

#include <opencv2/gapi/core.hpp>

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.

## ◆ divRC()

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

#include <opencv2/gapi/core.hpp>

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 )

#include <opencv2/gapi/core.hpp>

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 otherwise.

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

Note
Parameters
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.

## ◆ mean()

 GScalar cv::gapi::mean ( const GMat & src )
Python:
cv.gapi.mean(src) -> retval

#include <opencv2/gapi/core.hpp>

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.
countNonZero, min, max

## ◆ mul()

 GMat cv::gapi::mul ( const GMat & src1, const GMat & src2, double scale = 1.0, int ddepth = -1 )

#include <opencv2/gapi/core.hpp>

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.

## ◆ mulC() [1/3]

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

#include <opencv2/gapi/core.hpp>

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.

## ◆ mulC() [2/3]

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

#include <opencv2/gapi/core.hpp>

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

## ◆ mulC() [3/3]

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

#include <opencv2/gapi/core.hpp>

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

## ◆ phase()

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

#include <opencv2/gapi/core.hpp>

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.

## ◆ polarToCart()

 std::tuple cv::gapi::polarToCart ( const GMat & magnitude, const GMat & angle, bool angleInDegrees = false )

#include <opencv2/gapi/core.hpp>

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

## ◆ sqrt()

 GMat cv::gapi::sqrt ( const GMat & src )

#include <opencv2/gapi/core.hpp>

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.

## ◆ sub()

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

#include <opencv2/gapi/core.hpp>

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.

## ◆ subC()

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

#include <opencv2/gapi/core.hpp>

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.

## ◆ subRC()

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

#include <opencv2/gapi/core.hpp>

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

The function can be replaced with matrix expressions:

$\texttt{dst} = \texttt{c} - \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.