源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Javascript图像处理—阈值函数实例应用

  • 时间:2022-04-18 19:03 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Javascript图像处理—阈值函数实例应用
[b]前言[/b] [url=http://www.1sucai.cn/article/32982.htm]上一篇文章[/url],我们讲解了图像处理中的亮度和对比度的变化,这篇文章我们来做一个阈值函数。 [b]最简单的图像分割方法[/b] 阈值是最简单的图像分割方法。 比如为了从下图中分割出苹果,我们利用前景与背景的灰度差值,通过设定一个阈值,对于该像素大于这个阈值时就以黑色表示,小于便以灰色表示。 [img]http://files.jb51.net/file_images/article/201301/2013010314344051.jpg[/img] [b] 五种阈值类型[/b] 和OpenCV一样,我们将提供五种阈值类型,方便使用。 下面是原图像的波形表示,纵坐标表示像素点的灰度值大小,蓝线是阈值大小。 [img]http://files.jb51.net/file_images/article/201301/2013010314344052.png[/img] [b]二进制阈值化[/b] 公式表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344053.png[/img] 图像表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344054.png[/img] 可见超过该阈值的就变成最大值(即255),否则变成最小值(也就是0)。我们需要一个函数来实现这个功能:
[u]复制代码[/u] 代码如下:
var CV_THRESH_BINARY = function(__value, __thresh, __maxVal){ return __value > __thresh ? __maxVal : 0; };
[b]反二进制阈值化[/b] 公式表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344055.png[/img] 图像表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344056.png[/img] 这个则反过来,超过阈值的变成最小值,否则变成最大值。函数实现是:
[u]复制代码[/u] 代码如下:
var CV_THRESH_BINARY_INV = function(__value, __thresh, __maxVal){ return __value > __thresh ? 0 : __maxVal; };
[b]截断阈值化[/b] 公式表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344057.png[/img] 图像表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344058.png[/img] 可见这个是超过阈值的就被截断。函数实现是:
[u]复制代码[/u] 代码如下:
var CV_THRESH_TRUNC = function(__value, __thresh, __maxVal){ return __value > __thresh ? __thresh : 0; };
[b]阈值化为0[/b] 公式表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344059.png[/img] 图像表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344060.png[/img] 这个则是小于阈值的都化为0处理。函数实现:
[u]复制代码[/u] 代码如下:
var CV_THRESH_TOZERO = function(__value, __thresh, __maxVal){ return __value > __thresh ? __value : 0; };
[b]反阈值化为0[/b] 公式表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344061.png[/img] 图像表示是: [img]http://files.jb51.net/file_images/article/201301/2013010314344062.png[/img] 这个则在超过阈值时候置为0,函数实现是:
[u]复制代码[/u] 代码如下:
var CV_THRESH_TOZERO_INV = function(__value, __thresh, __maxVal){ return __value > __thresh ? 0 : __value; };
[b]阈值处理函数实现[/b] 然后我们做一个函数对整幅图进行上面这几种类型的阈值处理。
[u]复制代码[/u] 代码如下:
var threshold = function(__src, __thresh, __maxVal, __thresholdType, __dst){ (__src && __thresh) || error(arguments.callee, IS_UNDEFINED_OR_NULL/* {line} */); if(__src.type && __src.type == "CV_GRAY"){ var width = __src.col, height = __src.row, sData = __src.data, dst = __dst || new Mat(height, width, CV_GRAY), dData = dst.data, maxVal = __maxVal || 255, threshouldType = __thresholdType || CV_THRESH_BINARY; var i, j, offset; for(i = height; i--;){ for(j = width; j--;){ offset = i * width + j; dData[offset] = threshouldType(sData[offset], __thresh, maxVal); } } }else{ error(arguments.callee, UNSPPORT_DATA_TYPE/* {line} */); } return dst; };
这个函数比较简单,就是对每个像素点赋值为
[u]复制代码[/u] 代码如下:
threshouldType(sData[offset], __thresh, maxVal)
返回的数值。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部