HLSL着色器减去背景图像 [英] HLSL Shader to Subtract Background Image
问题描述
我正在尝试为Silverlight使用HLSL像素着色器,以从视频图像中减去背景图像.有人会提出比我正在使用的算法更复杂的算法,因为我的算法不能正确执行吗?
I am trying to get an HLSL Pixel Shader for Silverlight to work to subtract the background image from a video image. Can anyone suggest a more sophisticated algorithm than I am using because my algorithm isn't doing it correctly?
float Tolerance : register(C1);
SamplerState ImageSampler : register(S0);
SamplerState BackgroundSampler : register(S1);
struct VS_INPUT
{
float4 Position : POSITION;
float4 Diffuse : COLOR0;
float2 UV0 : TEXCOORD0;
float2 UV1 : TEXCOORD1;
};
struct VS_OUTPUT
{
float4 Position : POSITION;
float4 Color : COLOR0;
float2 UV : TEXCOORD0;
};
float4 PS( VS_OUTPUT input ) : SV_Target
{
float4 color = tex2D( ImageSampler, input.UV );
float4 background = tex2D( BackgroundSampler, input.UV);
if (abs(background.r - color.r) <= Tolerance &&
abs(background.g - color.g) <= Tolerance &&
abs(background.b - color.b) <= Tolerance)
{
color.rgba = 0;
}
return color;
}
要查看此示例,您需要一台装有网络摄像头的计算机:
To see an example of this, you need a computer with a webcam:
- 转到页面 http://xmldocs.net/alphavideo/background.html
- 按[开始录制].
- 将您的身体移出场景,然后按[捕获背景].
- 然后将您的身体移回场景,并使用滑块将Toleance值调整到着色器.
- Go to the page http://xmldocs.net/alphavideo/background.html
- Press [Start Recording].
- Move your body out of the the scene and press [Capture Background].
- Then move your body back into the scene and use the slider to adjust the Toleance value to the shader.
推荐答案
编辑
由于噪声,单个像素对此类任务无效.因此算法的本质应该是测量像素块之间的相似性.食谱伪代码(基于相关性测量):
Single pixel isn't useful for such task, because of noise. So algorithm essence should be to measure similarity between pixel blocks. Recipe pseudo-code (based on correlation measurement):
Divide image into N x M grid
For each N,M cell in grid:
correlation = correlation_between(signal_pixels_of(N,M),
background_pixels_of(N,M)
);
if (correlation > threshold)
show_background_cell(N,M)
else
show_signal_cell(N,M)
这是顺序的伪代码,但可以轻松地转换为HLSL着色器.简单地,每个像素检测到它属于哪个像素块,然后测量相应块之间的相关性.并基于此相关性显示或隐藏当前像素.
This is sequential pseudo code, but it could be easily converted to HLSL shader. Simply each pixel detects to which pixel block it belongs, and after that measures correlation between corresponding blocks. And based on that correlation shows or hides current pixel.
尝试这种方法, 祝你好运!
Try this approach, Good Luck !
这篇关于HLSL着色器减去背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!