Sobel Edge Detection 的shader实现
2007-12-23 22:01
676 查看
//-----------------------------------------------------------------------------
// 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.0, 1.0, 1.0, 1.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.30, 0.59, 0.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*x + y*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
相关文章推荐
- Sobel edge detection
- [caffe笔记007]:在新版caffe中实现Holistically-Nested Edge Detection
- 边缘检测(edge detection)_Sobel导数 Laplace算子 Canny边缘检测
- 每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)
- Android BitmapShader 实战 实现圆形、圆角图片
- 一些通过uv纹理坐标实现的简单shader(二)
- (原創) 如何實現Sobel Edge Detector? (Image Processing) (C/C++) (C++/CLI) (C)
- Android BitmapShader 实战 实现圆形、圆角图片
- Opengl及D3D以及Shader实现的特效
- CNN边缘检测--Richer Convolutional Features for Edge Detection
- UIScreenEdgePanGestureRecognizer 边缘触发手势 MVC模式下实现
- 边缘检测效果动画Shader实现
- Mean Shift Code for the Edge Detection and Image SegmentatiON system
- 【浅墨Unity3D Shader编程】之十 深入理解Unity5中的Standard Shader(二)&屏幕油画特效的实现
- 【Shader】通过Shader实现森林树木随风摆动效果
- 火云开发课堂 - 《Shader从入门到精通》系列 第二十五节:在Shader中实现3DALPHA混合与测试
- Android学习研究(五)通过BitmapShader实现圆形图片
- Shader特效——“Simple RayTrace”的实现 【GLSL】
- Gradient Operation for Edge Detection
- Android使用BitmapShader图形渲染实现圆形、圆角和椭圆自定义图片View