Sobel Edge Detection 的shader实现

// -----------------------------------------------------------------------------
//            Name: Sobel.fx
//          
//            Description: 
//                  
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
//  Effect File Variables
// -----------------------------------------------------------------------------

float4x4 worldViewProj : WorldViewProjection; 
//  This matrix will be loaded by the application

texture testTexture; 
//  This texture will be loaded by the application
const   float  Brightness  =   1.0f ;

sampler Sampler 
=  sampler_state
{
    Texture   
= (testTexture);
    MipFilter 
= LINEAR;
    MinFilter 
= LINEAR;
    MagFilter 
= LINEAR;
}
;

// -----------------------------------------------------------------------------
//  Vertex Definitions
// -----------------------------------------------------------------------------

//  Our sample application will send vertices 
//  down the pipeline laid-out like this...

struct  VS_INPUT
{
    float3 position    : POSITION;
    float2 texture0 : TEXCOORD0;
}
;

//  Once the vertex shader is finished, it will 
//  pass the vertices on to the pixel shader like this...

struct  VS_OUTPUT
{
    float4 hposition : POSITION;
    float2 texture0  : TEXCOORD0;
    float4 color     : COLOR0;
}
;

//  And finally, the pixel shader will send a single 
//  color value to the frame buffer like this...

struct  PS_OUTPUT
{
    float4 color : COLOR;
}
;

// -----------------------------------------------------------------------------
//  Simple Vertex Shader
// -----------------------------------------------------------------------------

VS_OUTPUT myvs( VS_INPUT IN )
{
    VS_OUTPUT OUT;

    OUT.hposition 
= mul( worldViewProj, float4(IN.position, 1) );

    OUT.color 
= float4( 1.01.01.01.0 ); // Pass white as a default color
    
//OUT.color = float4( 0.0, 1.0, 0.0, 1.0 ); // Pass green to test vertex shader

    OUT.texture0 
= IN.texture0;

    
return OUT;
}


// -----------------------------------------------------------------------------
//  Simple Pixel Shader
// -----------------------------------------------------------------------------

PS_OUTPUT myps( VS_OUTPUT IN )
{
    PS_OUTPUT OUT;

//    OUT.color = tex2D( Sampler, IN.texture0 ) * IN.color;

    
// If you uncomment the next line, the color passed to us by our
    
// vertex shader and the selected texture sampler will be ignored 
    
// completely and all output to the frame buffer will be blue regardless.

    
//OUT.color = float4( 0.0, 0.0, 1.0, 1.0);
    
    
    
const int NUM = 9;
    
const float threshold = 0.09;
    
const float texW = 140.f;
    
const float texH = 140.f;

//round texels
    const float2 c[NUM] ={
            float2( 
-1/texW,1/texH),
            float2( 
0.00,   1/texH),
            float2( 
1/texW, 1/texH),
            float2( 
-1/texW,0.00),
            float2( 
0.00,   0.00),
            float2( 
1/texW, 1/texH ),
            float2(
-1/texW, -1/texH),
            float2( 
0.00 ,  -1/texH),
            float2( 
1/texW, -1/texH),
    }
;
    
    float3 col[NUM];    
    
int i;

//it stores the samples of texture to col array.

    
for (i=0; i < NUM; i++{
      col[i] 
= tex2D(Sampler, IN.texture0 + c[i]);
    }


//now we start to compute the luminance with dot product and store them in lum array.
//lum = 0.30r + 0.59g + 0.11b;
    float3 rgb2lum = float3( 0.300.590.11);
    
    
float lum[NUM];
    
for (i = 0; i < NUM; i++{
        lum[i] 
=  dot(col[i].xyz, rgb2lum);
        
    }


//Sobel filter computes new value at the central position by sum the weighted neighbors.

    
float x = lum[2]+  lum[8]+2*lum[5]-lum[0]-2*lum[3]-lum[6];
    
float y = lum[6]+2*lum[7]+  lum[8]-lum[0]-2*lum[1]-lum[2];

//show the points which values are over the threshold and hide others. Final result is the 
//product of col[5] and edge detector value. Brightness adjusts the brightness of the image.

    
float edge =(x*+ y*< threshold)? 1.0:0.0;

//final output

    OUT.color.xyz 
= Brightness * col[5].xyz * edge;
    OUT.color.w 
= 1.0;

    
return OUT;
}


// -----------------------------------------------------------------------------
//  Simple Effect (1 technique with 1 pass)
// -----------------------------------------------------------------------------

technique Technique0
{
    pass Pass0
    
{
        Lighting 
= FALSE;

        Sampler[
0= (Sampler); // Needed by pixel shader

        VertexShader 
= compile vs_2_0 myvs();
        PixelShader  
= compile ps_2_0 myps();
    }

}


效果图:

 

参考文档:

 http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm

http://www.pages.drexel.edu/~weg22/edge.html

http://www.neatware.com/lbstudio/web/hlsl.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值