opencv之saturate_cast模板函数

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 :

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
saturate_cast是一个OpenCV库中的函数,用于将数据类型转换为指定的数据类型,并在转换过程中进行饱和处理。它的作用是确保转换后的值在指定的数据类型范围内,避免溢出或超出范围的情况发生。\[1\] 在图像处理中,saturate_cast通常用于对像素进行线性操作。例如,在对图像进行加减乘除等运算时,结果可能会超出像素灰度值的范围(0~255)。使用saturate_cast函数可以确保运算结果不会超出指定的数据类型范围,如果结果为负数,则转为0,如果结果超出最大值,则为最大值。\[2\] 举个例子,如果我们对像素进行线性操作,不使用saturate_cast函数,结果可能会超出范围。而使用saturate_cast函数,可以确保结果在指定的数据类型范围内,避免溢出或超出范围的情况发生。\[3\] #### 引用[.reference_title] - *1* [opencv函数saturate_cast(防止溢出)](https://blog.csdn.net/piaoxuezhong/article/details/60570919)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【OpenCV】中saturate_cast的含义和用法是什么?](https://blog.csdn.net/qq_15698613/article/details/84557588)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值