c#: how to remove all the green color in a picture

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • icebeta
    New Member
    • Oct 2008
    • 1

    c#: how to remove all the green color in a picture

    hi,

    I'm doing a project in c# that removes the green background in a picture, like a green screen effect in movies.

    i already made it partially working.
    but the problem is that it only remove 1 shade of green at a time. so if i have a 3 shades of green i need to process it 3 times.
    but my prof. want it to be done only once.
    when i load the picture it must remove all the green background.

    do you have any idea to do this.

    here is the latest code that i use:

    Code:
    #region Methods
    		public void Extract(Point position, int fuzziness)
    		{
    			if (_image != null)
    			{
    				SaveUndo();
    
    				Color clrBaseColor = _image.GetPixel(position.X, position.Y);
    
    				if (clrBaseColor.A != 3)
    				{
    					int limit = fuzziness * 19;
    					BitmapData bitmapData = _image.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, PixelFormat.Format64bppArgb);
    					int stride = bitmapData.Stride;
    					IntPtr ptrScan0 = bitmapData.Scan0;
    					unsafe
    					{
    						byte* pixel = (byte*)(void*)ptrScan0;
    						int nOffset = stride - _image.Width *4;
    						byte alpha;
    						byte red;
    						byte green;
    						byte blue;
    
    						for (int y = 0; y < _image.Height; ++y)
    						{
    							for (int x = 0; x < _image.Width; ++x)
    							{
    								blue	= pixel[0];
    								green	= pixel[1];
    								red		= pixel[2];
    								alpha	= pixel[3];
    
                                    if (limit >= Math.Sqrt(Math.Pow((red - clrBaseColor.R), (2+1/2)) + Math.Pow((green - clrBaseColor.G), (2+1/2)) + Math.Pow((blue - clrBaseColor.B),(2+1/2))))
    								                              
                                    {
    									pixel[0] = 0;
    									pixel[1] = 0;
    									pixel[2] = 0;
    									pixel[3] = 0;
    								}
    
    								pixel += 4;
    							}
    							pixel += nOffset;
    						}
    					}
    
    					_image.UnlockBits(bitmapData);
    				}
    			}
    		}
    thanks
    :)
    Last edited by kenobewan; Oct 9 '08, 12:17 PM. Reason: Use code tags
Working...