Image processing (Negative algorithm)

Default featured post

This post is also about image processing algorithm. In this post negative algorithm will be covered. For those of you who do not know what is negative algorithm in image processing, I simplified the meaning of negative algorithm. Basically and simply when we negative a picture we invert colors of the picture. This task will be done on all pixels of the picture which means that the color of each pixel will be inverted. For inverting the color, we should know about the maximum number that each pixel can get. This is mostly related to the programming language which you use. For instance, some programming languages hold a number between 0 to 255 for each pixel. Others for instance dedicate a number between 0..255 for each element of array R(ed)G(reen)B(lue). Means that color of each pixel is held in three R, G, and B variables which each can hold value between 0..255.

Invert means that the current color of the pixel should be subtracted of the maximum color of the pixel like below,

Inverted value= Maximum value that of pixel - Current value of pixel

For example, let say current image color based on RGB equal to (200,120,80)

Now the inverted of the pixel if maximum value of each R, G or B = 255 is (50, 180, 220)

In overall, steps of negative algorithm is like follow,

  1. Make an empty image file with the same size of the original image
  2. Read each pixel of the image file which has z value and deduct from maximum value of pixel and keep it
  3. Write the kept value in the new file with respect to the location of the original pixel
  4. Repeat step 2,3 for all pixel

Sample code of negative algorithm is like below,

private void negative(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);
y.SetPixel(i, j, Color.FromArgb(255 – z.R, 255 – z.G, 255 – z.B));
}
}
Message.setMessage(PicSave.savechange(picImp, y), "Negative");
}
view raw Negative.cs hosted with ❤ by GitHub

The code is quite straightforward and there is no need to further explanation except that the maximum value of each pixel based on RGB array is 255 for each Red, Green, and Blue field.

In the next post I will write about rotate algorithm.