0

So in c# I have an image in a bitmap with different colors in it. Now i'm trying to only keep one single range of colors in the image and all the others can be removed (turn them into white pixels). Now the color I want to extract is yellow but just comparing the pixels their color with Color.Yellow won't suffice since the pixels can have different shades of yellow so I'm guessing I sort of need to filter out all the other colors but I can't seem to figure out on how to do it.

I've read something about convolution but I don't see a way to implement this directly into the program.

Is there a way that makes this possible that I only keep the color yellow and it's diffent shades in the image?

Thanks in advance.

DLP
  • 209
  • 4
  • 11
  • 1
    You can use GetHue with some range to determine a yellow color. For speed use LockBits! – TaW May 08 '19 at 15:07

2 Answers2

3

Here is a fast and simple solution.

It uses a function, which will plug-in with a post you can find here.

This is the function:

public Color ToWhiteExceptYellow(Color c, int range)
{
    float hueC = c.GetHue();
    float e = 1.5f * range;   // you can adapt this nuumber
    float hueY = Color.Yellow.GetHue();
    float delta = hueC - hueY;
    bool ok = (Math.Abs(delta) < e);
    //if (!ok) { ok = (Math.Abs(360 + delta) < e); }  // include these lines ..
    //if (!ok) { ok = (Math.Abs(360 - delta) < e); }  // for reddish colors!

    return ok ? c : Color.White;
}

It works well with yellow but as color hues is a wraparound number it will need more code to work with the wrap point color (red). I have included two lines to help out.

To make it work change these lines in the linked post:

// pick one of our filter methods
ModifyHue hueChanger = new ModifyHue(ToWhiteExceptYellow);

..and..

// we pull the bitmap from the image
Bitmap bmp = new Bitmap( (Bitmap)pictureBox1.Image);  // create a copy

..and..

c = hueChanger(c, trackBar1.Value);  // insert a number you like, mine go from 1-10

..and..:

// we need to re-assign the changed bitmap
pictureBox2.Image = (Bitmap)bmp;   // show in a 2nd picturebox

Don't forget to include the delegate:

public delegate Color ModifyHue(Color c, int ch);

and the using clause:

using System.Drawing.Imaging;

Note that one ought to dispose of the old content to avoid leaking the images, maybe like so:

Bitmap dummy = (Bitmap )pictureBox2.Image;
pictureBox2.Image = null;
if (dummy != null) dummy.Dispose;
// now assign the new image!

Let's see it at work:

enter image description here

Feel free to expand on this. You could change the function's signature to include a target color and add ranges for brightness and/or saturation..

TaW
  • 53,122
  • 8
  • 69
  • 111
1

very vague definition, if i understood what you want to do, i'd do it like this:

  1. Iterate over every pixel of the bitmap and compare it to the yellow range (if outside - assign white value)
  2. Convert every pixels RGB value to CMYK (search online for a conversion formula) [Y = (1-Blue-Black) / (1-Black)]
  3. Assign white value if YellowMin

Convolution wont help you, it acts in spacial domain not in color

Felix
  • 109
  • 2
  • 4
  • That is indeed what i'm trying to accomplish, I will, try this for sure thanks already. – DLP May 08 '19 at 14:59