边缘检测:
图像处理和计算机视觉中的基本问题,边缘检测的目的是标识数字图像中亮度变化明显的点。图像属性中的显著变化通常反映了属性的重要事件和变化。这些包括(i)深度上的不连续、(ii)表面方向不连续、(iii)物质属性变化和(iv)场景照明变化。 边缘检测是图像处理和计算机视觉中,尤其是特征检测中的一个研究领域。(维基百科)
乍一看维基百科的上面的解释,有点懵,我们具体来看一下几张图片:
这就是两种用了Sobel算法的边缘检测,注意其中白色的边缘,为了与场景有很大的差别.
图像的卷积:
一副数字图像可以看作一个二维空间的离散函数可以表示为f(x, y), 假设有对于二维卷积操
作函数C(u, v) ,则会产生输出图像g(x, y) = f(x, y) *C(u,v), 利用卷积可以实现对图像模糊处理,边缘检测,产生轧花效果的图像。(引自lanbing510)
一.基于Sobel实现的边缘检测
这是Sobel的卷积因子:
实现计算的方法是下图,该算子包含两组3x3的矩阵,分别为横向及纵向,将之与图像作平面卷积,即可分别得出横向及纵向的亮度差分近似值。如果以A代表原始图像,Gx及Gy分别代表经横向及纵向边缘检测的图像灰度值(引自维基百科)
图像的每一个像素的横向及纵向梯度近似值可用以下的公式结合,来计算梯度的大小。
然后可用以下公式计算梯度方向。
Gx = (-1)*f(x-1, y-1) + 0*f(x,y-1) + 1*f(x+1,y-1)
+(-2)*f(x-1,y) + 0*f(x,y)+2*f(x+1,y)
+(-1)*f(x-1,y+1) + 0*f(x,y+1) + 1*f(x+1,y+1)
= [f(x+1,y-1)+2*f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2*f(x-1,y)+f(x-1,y+1)]
Gy =1* f(x-1, y-1) + 2*f(x,y-1)+ 1*f(x+1,y-1)
+0*f(x-1,y) 0*f(x,y) + 0*f(x+1,y)
+(-1)*f(x-1,y+1) + (-2)*f(x,y+1) + (-1)*f(x+1, y+1)
= [f(x-1,y-1) + 2f(x,y-1) + f(x+1,y-1)]-[f(x-1, y+1) + 2*f(x,y+1)+f(x+1,y+1)]
这个就是前面的矩阵乘法,f(x,y)为A的中间点,f(x-1,y-1)为其f(x,y)的左上角的点,f(x,y-1)为其f(x,y)的点,构造了两个个3*3矩阵的乘法运算.
float4 MainPS_Screen( VS_OUTPUT In ):COLOR
{
// 临近像素点的颜色
float s00 = tex2D( ColorMapSamplerIn , In.tex + float2( -off, -off)).r;
float s01 = tex2D( ColorMapSamplerIn , In.tex + float2( 0, -off)).r;
float s02 = tex2D( ColorMapSamplerIn , In.tex + float2( off, -off)).r;
float s10 = tex2D( ColorMapSamplerIn , In.tex + float2( -off, 0)).r;
float s12 = tex2D( ColorMapSamplerIn , In.tex + float2( off, 0)).r;
float s20 = tex2D( ColorMapSamplerIn , In.tex + float2( -off, off)).r;
float s21 = tex2D( ColorMapSamplerIn , In.tex + float2( 0, off)).r;
float s22 = tex2D( ColorMapSamplerIn , In.tex + float2( off, off)).r;
// Sobel filter计算在X轴的方向
float sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22;
// Sobel filter 计算在Y轴的方向
float sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22;
// 计算边缘
float edgeSqr = ( sobelX * sobelX + sobelY * sobelY );
// ... and threshold against a squared value instead.
float4 Color = float4( 0.0 , 0.0 , 0.0 , 0.0 );
if( edgeSqr > 0.00999 )
{
Color = float4( 1.0 , 1.0 , 1.0 , 1.0 );
}
else
{
Color = Color;
}
return Color ;
}