Image processing (Gray mode algorithm)

Default featured post

In the last post, I discussed about black and white algorithm in image processing and there I mentioned that gray scale algorithm is different and would be explained in another post. Now, this post is about gray scale algorithm.

As mentioned before, in black and white algorithm all pixels colors are changed either to black or white. Additionally, user needs to set threshold for this conversion to decide which range of colors should be turned black and which to white.

By contrast, in gray algorithm all pixels should be set with the same range which is gray but with different colors numbers. In other word, the range is gray but as we have different gray colors (like light gray and dark gray), the color of each pixel could be different. In fact, the new color of the pixel acquires from the sum of three RGB fields of the current pixel and then it is divided to three as well. This color is called as temp color since possibly it changes each time when each pixel is read from picture. Temp color holds color that is in gray range but the exact number (gray color) is different and is set on the original pixel.

The following sample code of gray mode algorithm expresses aforementioned things better and clearer.

private void Gray_Mode(PictureBox picImp) {
  for (int i = 0; i <= x.Width - 1; i++) {
    for (int j = 0; j <= x.Height - 1; j++) {
      z = x.GetPixel(i, j);
      int tmp = (z.R + z.G + z.B) / 3;
      y.SetPixel(i, j, Color.FromArgb(tmp, tmp, tmp));
  Message.setMessage(PicSave.savechange(picImp, y), "GrayMode");