473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1544

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

Similar topics

3
by: Girish | last post by:
I have this XML FILE where I am reading data from and it has this node doctype "< ! D O C T Y P E a d f S Y S T E M " h t t p : / / w h o s c a l l i n g . c o m / d t d / a d f d t d . d t d...
9
by: Roberto | last post by:
Hi everyone, I'm new here... I have a frameset header/contents. It's necessary to force the header's scrollbar to be visible to preserve the alignment between the two frames (see...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
4
by: Michael A. Covington | last post by:
Greetings, I am working on a program that is in beta testing and goes through a new version every week or so. When there's a new version, go into the setup project change the Product Code but...
4
by: Fredrik Rodin | last post by:
Hi! I want to basicaööy run a Session.Abandon() on logout but keep one session. In order to do this I'm iterating through my session collection by runing the follwoing code: Dim iSessionCount...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
4
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
80
by: Andrew R | last post by:
Hi I'm creating a series of forms, each with with around 15-20 text boxes. The text boxes will show data from tables, but are unbound to make them more flexible. I want the form to be used...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.