手机版

Javascript图像处理-阈值函数的示例应用

时间:2021-10-05 来源:互联网 编辑:宝哥软件园 浏览:

前言在上一篇文章中,我们解释了图像处理中亮度和对比度的变化。在本文中,我们将做一个阈值函数。最简单的图像分割方法阈值是最简单的图像分割方法。例如,为了从下图中分割苹果,我们使用前景和背景之间的灰度差,并设置阈值。如果像素大于此阈值,则用黑色表示,如果像素较小,则用灰色表示。Threshold  simple  example

五种阈值类型像OpenCV一样,我们将提供五种阈值类型以便于使用。下面是原始图像的波形表示,纵坐标表示像素的灰度值,蓝线表示阈值。Threshold  Binary

二进制阈值公式表示为:\texttt{dst} (x,y) = \fork{\texttt{maxVal}}{if  $\texttt{src}(x,y) >。
</p>
<p>
\ text  TT  { thresh  } $ } { 0 } { other  } ' src='http://img%20%20.%20cppcns.com/pic%20%20.%20PHP?URL=/file%20%20_%20images/article/201301/201301031434053%20.%20png%20%20'图片为:<IMG%20%20class=align-center%20%20style=

可以看出,如果超过阈值,则它变成最大值(即255),否则它变成最小值(即0)。我们需要一个函数来实现这个函数:复制代码如下: varcv _ THRESH _ binary=function(_ _ value,_ _ THRESH,_ _ MaxVal){ return _ _ value _ _ THRESH?_ _ MaxVal : 0;};逆阈值公式表示为:\texttt{dst} (x,y) = \fork{0}{if  $\texttt{src}(x,y) >。
</p>
<p>
\ text  TT  { thresh  } $ } { \ text  TT  { MaxVal  } } { others  } ' src='http://img%20%20.%20cppcns.com/pic%20%20.%20PHP?URL=/file%20%20_%20images/article/201301/201301031434055%20.%20png%20%20'图片为:<IMG%20%20class=align-center%20%20style=

如果超过阈值,这又变成最小值,否则变成最大值。函数实现如下:复制代码如下: varcv _ thresh _ binary _ inv=function(_ value,_ _ thresh,_ _ max val){ return _ _ value _ _ thresh?0 : _ _ maxVal};截断阈值公式表示为:\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if  $\texttt{src}(x,y) >。
</p>
<p>
\ text  TT  { thresh  } $ } { \ text  TT  { src  }(x,y)} { other  } ' src='http://img%20%20.%20cppcns.com/pic%20%20.%20PHP?URL=/file%20%20_%20images/article/201301/201301031434057%20.%20png%20%20'图片为:<IMG%20%20class=align-center%20%20style=

可以看出,如果超过阈值,这将被截断。函数实现如下:复制代码如下: varcv _ thresh _ trunc=function(_ value,_ _ thresh,_ _ maxval){ return _ _ value _ _ thresh?_ _ thresh : 0;};阈值设置为0的公式为:\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if  $\texttt{src}(x,y) >。
</p>
<p>
\ text  TT  { thresh  } $ } { 0 } { other  } ' src='http://img%20%20.%20cppcns.com/pic%20%20.%20PHP?URL=/file%20%20_%20images/article/201301/2013010314344059%20.%20png%20%20'图片为:<IMG%20%20class=align-center%20%20style=

那么这是小于阈值的全零处理。函数实现:复制代码如下: varcv _ thresh _ to zero=function(_ value,_ _ thresh,_ _ max val){ return _ _ value _ _ thresh?_ _值: 0;};逆阈值化为0的公式为:\texttt{dst} (x,y) = \fork{0}{if  $\texttt{src}(x,y) >。
</p>
<p>
\ text  TT  { thresh  } $ } { \ text  TT  { src  }(x,y)} { other  } ' src='http://img%20%20.%20cppcns.com/pic%20%20.%20PHP?URL=/file%20%20_%20images/article/201301/201301031434061%20.%20png%20%20'图片为:<IMG%20%20class=align-center%20%20style=

然后当超过阈值时这个设置为0,函数实现如下:复制代码如下: varcv _ THRESH _ to zero _ INV=function(_ VALUE,_ _ THRESH,_ _ MaxVal){ return _ _ VALUE _ _ THRESH?0 : _ _值;};实现了阈值处理功能,然后我们做一个功能,对整个画面进行上述类型的阈值处理。复制的代码如下: var Threshold=Function(_ src,_ _ thresh,_ _ maxval,_ _ threshtype,_ _ dst) {(_ _ src _ _ thresh) | | Error(参数。被调用者,是_ undefined _ or _ null/* {。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,threshTYPe=_ _ thresholdType | | CV _ THRESH _ BInary;var i,j,偏移量;for(i=高度;I-;){ for(j=宽度;j-;){ offset=I * width j;dData[offset]=thresholdtype(sData[offset],__thresh,MaxVal);} } } else { error(arguments . caller,UNSPORT _ DATA _ TYPE/* { line } */);}返回dst};这个函数比较简单,就是给每个像素赋值,作为复制代码返回的值,如下所示: thresholdtype(sdata[offset],_ _ thresh,maxval)。

版权声明:Javascript图像处理-阈值函数的示例应用是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。