This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Convert an image to black and white with some thresholding

I have an indexed bitmap that I need to convert to a black and white (monochrome, not grayscale). How can I apply some custom threshold to it? For example, i want all pixels that are less than some thresholds to be white, others black?
Hi Wolszhang,

You can implement this as follows:

//Setting desired threshold
int threshold = 170;
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(@"input.jpg");
for(int i = 0; i < bitmap.Palette.Count; i++)
{
     // RGB-to-brightness calculation taken from https://stackoverflow.com/a/596243
     var brightness = (0.299 * bitmap.Palette[i].R + 0.587 * bitmap.Palette[i].G + 0.114 * bitmap.Palette[i].B);
     if (brightness > threshold)
     {
          bitmap.Palette[i] = Aurigma.GraphicsMill.RgbColor.White;
     }
     else
     {
          bitmap.Palette[i] = Aurigma.GraphicsMill.RgbColor.Black;
     }
}
bitmap.ColorManagement.Convert(PixelFormat.Format1bppIndexed);
bitmap.Save(@"output.tif"); 

Best regards,
Julia

Hi,
I also indexed bitmap that I need to convert to a black and white and want all pixels that are less than some thresholds to be black.I implemented the code as shown below,but it does not work well.What can I do for this and how can I fix the issue?
Thanks!
Hi daniel12,

Please post your code that doesn't work and we will take a look. Could you also add some details on the issue?

Regards,
Andrew
Could you post an image you would like to convert?