Welcome to MSDN Blogs Sign in | Join | Help

Need Help with my Chroma Key Pixel Shader Algorithm

I’ve just uploaded a simple demo application that shows the Silverlight 3 Chroma Key Effect in action.  If you don’t have the Silverlight 3 beta installed you will need to install it from here (the install link in the application will not work). 

ChromaDemo.png

If you watch the demo, you’ll notice ghosting along the edges of the dancers – obviously I need help with my pixel shader algorithm:

float4 PS( VS_OUTPUT input ) : SV_Target
{
     float4 color = tex2D( ImageSampler, input.UV );
   
     if (abs(InputColor.r - color.r) <= Tolerance && 
         abs(InputColor.g - color.g) <= Tolerance &&
         abs(InputColor.b - color.b) <= Tolerance)
     {
      color.rgba = 0;
     }
  
   return color;
 }

If anyone has suggestions on how to make this algorithm better – or has a better algorithm, please share it with me.

Thanks in advance.

Published Friday, May 15, 2009 3:26 PM by Michael S. Scherotter
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Need Help with my Chroma Key Pixel Shader Algorithm | Microsoft Share Point

# re: Need Help with my Chroma Key Pixel Shader Algorithm

I would try converting to HSV color space in the pixel shader and having a separate tolerance for each component (use a float3 constant and there's no perf diff). You can either calculate it or use a texture as a lookup table (not sure about Silverlight 3's texture/shader compat). HSV is more intuitive and should give a better distance function. There are other color spaces you could try. YUV might work well but it's designed to deemphasize chrominance.

http://en.wikipedia.org/wiki/HSV_color_space

You should probably use a continuous function for alpha. Anything but a step should give a nice improvement.

Saturday, May 16, 2009 6:31 AM by Ted Howard

# Silverlight Cream for May 18, 2009 -- #595

In this Issue: Erik Mork , Mark Monster , Steve Strong , Rishi , Kirupa Chinnathambi ( 2 ), Timmy Kokke

Tuesday, May 19, 2009 12:34 AM by Community Blogs

# re: Need Help with my Chroma Key Pixel Shader Algorithm

Very very nice! :)  I attempted a WPF version a while ago, but HLSL / shader model 2.0 didn't allow enough statements to accomplish RGB -> HSV -> RGB conversion.  Wonder if it could be done with multiple shaders + inputs somehow? (or maybe some close approximation of HSV?).  I'll be watching this with great interest :)

Tuesday, May 19, 2009 2:21 AM by Chris Cavanagh

# re: Need Help with my Chroma Key Pixel Shader Algorithm

Wednesday, May 20, 2009 5:21 AM by Mark

# re: Need Help with my Chroma Key Pixel Shader Algorithm

Relative luminance (http://en.wikipedia.org/wiki/Luminance_%28relative%29) is a linear conversion from RGB, but it just makes the threshold more intuitive.

Preprocessing is the way to go ideally, but I'm sure that's not an option. In general, for big calculations in shaders, you try using a texture as a look-up-table for parts of the calculations that won't fit in shader limits, but I don't think that helps here.

Monday, May 25, 2009 3:18 PM by Ted Howard

# re: Need Help with my Chroma Key Pixel Shader Algorithm

Wednesday, July 22, 2009 8:30 PM by slyi

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker