-1

I am sorry if the question in the header is not descriptive enough. But, basically what my problem is the following. I am taking Bitmap and making it gray scale. It works nice if i do not reduce the number of bits and I still use 8 bits. However, the point of the hw I have is to show how the image changes when I reduce the number of bits holding the information. In the example bellow I am reducing the binary string to 4 bits and then rebuilding the image again. The problem is that the image becomes black. I think is because the image has mostly gray values (in the 80's range) and when I am reducing the binary string I am left with black image only. It seems to me that i heave to check for lower and high gray scale values and then make the more light-gray go to white and dark gray go to black. In the end with 1 bit representation I should only have black and white image.Any idea how can i do that separation?

Thanks

 Bitmap bmpIn = (Bitmap)Bitmap.FromFile("c:\\test.jpg");
            var grayscaleBmp = MakeGrayscale(bmpIn);

  public Bitmap MakeGrayscale(Bitmap original)
    {
        //make an empty bitmap the same size as original
        Bitmap newBitmap = new Bitmap(original.Width, original.Height);
        for (int i = 0; i < original.Width; i++)
        {
            for (int j = 0; j < original.Height; j++)
            {
                //get the pixel from the original image
                Color originalColor = original.GetPixel(i, j);
                //create the grayscale version of the pixel
                int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                + (originalColor.B * .11));

                //now turn it into binary and reduce the number of bits that hold information 
                byte test = (byte) grayScale;
                string binary = Convert.ToString(test, 2).PadLeft(8, '0');
                string cuted = binary.Remove(4);
                var converted = Convert.ToInt32(cuted, 2);

                //create the color object
                Color newColor = Color.FromArgb(converted, converted, converted);

                //set the new image's pixel to the grayscale version
                newBitmap.SetPixel(i, j, newColor);
            }
        }
        return newBitmap;
    }
setlio
  • 726
  • 2
  • 13
  • 32
  • possible duplicate of [What would be a good TRUE black and white colormatrix?](http://stackoverflow.com/questions/2746103/what-would-be-a-good-true-black-and-white-colormatrix) – mbeckish Jan 21 '15 at 18:09
  • No is not duplicate if you read the post carefully. I am asking completely different thing. That post only talks about black and white and not about the issue i am having. – setlio Jan 21 '15 at 18:22
  • Explain the difference and I might be convinced. – mbeckish Jan 21 '15 at 18:23
  • I want eventually to to land on black and white, but how would i do the intermediate steps? – setlio Jan 21 '15 at 18:29
  • Do it using [ImageAttributes.SetThreshold](https://msdn.microsoft.com/en-us/library/6464csbe(v=vs.110).aspx), as recommended in the linked solution. it will be much faster than looping through every pixel and calling SetPixel, which is almost never recommended. – mbeckish Jan 21 '15 at 18:34

1 Answers1

0

As mbeckish said, it is easier and much faster to use ImageAttributes.SetThreshold.

One way to do it manually is to get the median value of the grayscale pixels in the image, and use that for the threshold between black and white.

xpda
  • 15,585
  • 8
  • 51
  • 82