469,945 Members | 1,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. #region Methods
  2.         public void Extract(Point position, int fuzziness)
  3.         {
  4.             if (_image != null)
  5.             {
  6.                 SaveUndo();
  7.  
  8.                 Color clrBaseColor = _image.GetPixel(position.X, position.Y);
  9.  
  10.                 if (clrBaseColor.A != 3)
  11.                 {
  12.                     int limit = fuzziness * 19;
  13.                     BitmapData bitmapData = _image.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, PixelFormat.Format64bppArgb);
  14.                     int stride = bitmapData.Stride;
  15.                     IntPtr ptrScan0 = bitmapData.Scan0;
  16.                     unsafe
  17.                     {
  18.                         byte* pixel = (byte*)(void*)ptrScan0;
  19.                         int nOffset = stride - _image.Width *4;
  20.                         byte alpha;
  21.                         byte red;
  22.                         byte green;
  23.                         byte blue;
  24.  
  25.                         for (int y = 0; y < _image.Height; ++y)
  26.                         {
  27.                             for (int x = 0; x < _image.Width; ++x)
  28.                             {
  29.                                 blue    = pixel[0];
  30.                                 green    = pixel[1];
  31.                                 red        = pixel[2];
  32.                                 alpha    = pixel[3];
  33.  
  34.                                 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))))
  35.  
  36.                                 {
  37.                                     pixel[0] = 0;
  38.                                     pixel[1] = 0;
  39.                                     pixel[2] = 0;
  40.                                     pixel[3] = 0;
  41.                                 }
  42.  
  43.                                 pixel += 4;
  44.                             }
  45.                             pixel += nOffset;
  46.                         }
  47.                     }
  48.  
  49.                     _image.UnlockBits(bitmapData);
  50.                 }
  51.             }
  52.         }
thanks
:)
Oct 9 '08 #1
0 1396

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Roberto | last post: by
11 posts views Thread by Tony Johansson | last post: by
6 posts views Thread by Arne Claus | last post: by
4 posts views Thread by Michael A. Covington | last post: by
4 posts views Thread by Fredrik Rodin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.