saturate_cast
Template function for accurate conversion from one primitive type to another.
C++:template<…> _Tpsaturate_cast(_Tp2v)¶
Parameters:
v – Function parameter.
The functions saturate_cast resemble the standard C++ cast operations, such asstatic_cast() and others. They perform an efficient and accurate conversion from one primitive type to another (see the introduction chapter).saturate in the name means that when the input valuev is out of the range of the target type, the result is not formed just by taking low bits of the input, but instead the value is clipped. For example:
uchar a = saturate_cast(-100); // a = 0 (UCHAR_MIN)
short b = saturate_cast(33333.33333); // b = 32767 (SHRT_MAX)
Such clipping is done when the target type is unsignedchar ,signedchar ,unsignedshort orsignedshort . For 32-bit integers, no clipping is done.
When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), the floating-point value is first rounded to the nearest integer and then clipped if needed (when the target type is 8- or 16-bit).
This operation is used in the simplest or most complex image processing functions in OpenCV.