Image processing (Black and white algorithm)

Default featured post

Last image processing post was about rotation which on that post I described about three various types of rotation which were 180 degree rotation, 90 L rotation and 90 R rotation. This post is about black and white algorithm. Simply, this algorithm is about changing the color of the picture in either black or white. Therefore, for reaching to this purpose all pixels of the picture must be read and should be converted in either black (0) or white (1). It is exactly like binary numbers everything is 0 and 1.

Now the question is that how we can identify which pixels should be white and which pixels black. More precisely which colors (colors numbers) should be considered as black and which as white. The answer is really simple, we need to identify the range or threshold manually.

Each read pixel contains three parameters. One is, pixel X axis location, the other is, pixel Y axis location and finally it is the color of the pixel. Most of the programming languages keep those parameters as long type which is numerical type. Therefore, defining threshold for the pixel is simple and can be done by defining just one if condition.

With manipulating threshold you will achieve to various results. Thus, you can get advantage of this matter and define a seek bar to facilitate this option to users to define threshold base on their needs.

The following code is sample code of mentioned algorithm.

private void Black_white(PictureBox picImp, bool Bw_G) {
  for (int i = 0; i <= x.Width - 1; i++) {
    for (int j = 0; j <= x.Height - 1; j++) {
      z = x.GetPixel(i, j);
      if (((z.R + z.G + z.B) / 3) <= 127) {
        y.SetPixel(i, j, Color.FromArgb(0, 0, 0));
      else {
        y.SetPixel(i, j, Color.FromArgb(255, 255, 255));
  Message.setMessage(PicSave.savechange(picImp, y), "Black & White");

In above code threshold value is 127 and you can change this value based on your need.

Note that black and white algorithm is totally different from gray mode algorithm and it does not do the same task and has different algorithm. Gray mode algorithm will be discussed later in a separate post.