opencv之saturate_cast模板函数

saturate_cast是OpenCV中用于精确转换不同类型的数据的模板函数,确保在转换过程中超出目标类型范围的值被正确地截断。在图像处理中,如加减乘除或拉普拉斯算子等操作可能会导致像素值超出0-255范围,此时saturate_cast会将结果限制在有效范围内,避免丢失精度。该函数在OpenCV的图像处理函数中广泛应用。
摘要由CSDN通过智能技术生成

saturate_cast

Template function for accurate conversion from one primitive type to another.

C++: template<...> _Tp saturate_cast (_Tp2 v )
Parameters:
  • v – Function parameter.

The functions saturate_cast resemble the standard C++ cast operations, such asstatic_cast<T>() 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<uchar>(-100); // a = 0 (UCHAR_MIN)
short b = saturate_cast<short>(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.

See also

add(),subtract(),multiply(),divide(),Mat::convertTo()

考虑:为什么上面的函数会用到saturate_cast呢,因为无论是加是减,乘除,都会超出一个像素灰度值的范围(0~255)所以,所以当运算完之后,结果为负,则转为0,结果超出255,则为255。

另外在梯度锐化的函数里,也会涉及到saturate_cast

如:

from:   http://ggicci.blog.163.com/blog/static/210364096201262123236955/

OpenCV图像处理 空间域图像增强(图像锐化 1 基于拉普拉斯算子)  

2012-07-21 14:32:36|  分类:OpenCV/EmguCV|  标签:opencv图像处理  opencv图像锐化  拉普拉斯算子  空间域图像增强  opencv  |字号 订阅

Title :

  • OpenCV
  • OpenCV 图像锐化
  • 拉普拉斯算子 ( Laplacian operator )

Quote :

It is indeed a well-known result in image processing that if you subtract its Laplacian from an image, the image edges are amplified giving a sharper image. [FromOpenCV 2 Computer Vision Application Programming Cookbook]

  • 对于求一个锐化后的像素点(sharpened_pixel),这个基于拉普拉斯算子的简单算法主要是遍历图像中的像素点,根据领域像素确定其锐化后的值
  • 计算公式:sharpened_pixel = 5 * current – left – right – up – down ; [见Code1]
  • OpenCV图像处理 空间域图像增强(图像锐化 1 基于拉普拉斯算子) - ___________杰 - __________Ggicci
     
  • 当一个运算是通过领域像素进行的时候,我们通常用一个矩阵来表示这种运算关系,也就是我们经常所说的核 (Kernel)。那么上面的锐化滤波器 (Sharpening Filter) 就可以用这个矩阵表示为它的核:
  -1  
-1 5 -1
  -1  
  • 因为 滤波 在图像处理中是一个非常普通且常用的操作,所以OpenCV里面已经定义了一个特殊的函数用来执行这个操作。要使用它的话只需要定义一个,然后作为参数传递就行了。[见Code2]

Code 1 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值