Introduction to image processing (Zoom in algorithm)

Default featured post

Image processing is one of the interesting and obviously important area of the computer science. Image processing also is one of the oldest and ubiquitous area in the computer science which is employed by other areas as well. This blog just explains about the practical side of the image processing or generally every matter in programming and does not cover academic subjects. It means that just image processing algorithms will be introduced and implemented with the sample code.

The first basic and widely used algorithm in image processing is zoom algorithm. This algorithm can be found in all photo editing software such as Gimp, Photoshop, etc. We can break zoom to two separate algorithm as zoom in and zoom out. This post is dedicated to zoom in algorithm.

In zoom in the size of image will be increased. In other word the image will be bigger and user can see the details of the image (picture) better. Zoom in has a really simple algorithm which is described as follow,

  1. Make an empty image file with the double size of the original image. For instance, if the current size of the picture is 200x200, the new image size should be 400x400
  2. Read each pixel of the image file and write the pixel in four neighborhoods of the new image file. It means that each pixel will be written four times in the file
  3. Repeat step 2 for all pixel

The sample code of the zoom in algorithm is like below,

private void zoomIn(PictureBox picImp) {
  y = new System.Drawing.Bitmap(2 * (x.Width), 2 * (x.Height));
  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 * 2, j * 2, z);
      y.SetPixel(i * 2 + 1, j * 2, z);
      y.SetPixel(i * 2, j * 2 + 1, z);
      y.SetPixel(i * 2 + 1, j * 2 + 1, z);
    }
  }
  Message.setMessage(PicSave.savechange(picImp, y), "ZoomIn");
}

As mentioned in the previous paragraph, firstly a new file with the double size of the original file will be created which here the variable name of the new file is y. Then, we need two loops to read all pixels of the file and meanwhile add pixels in the new file as well. The first loop traverses the width of the picture and j loop traverses the height of the picture. Inside of the second loop is the main part of the code and it firstly reads the pixel which is located in (x,y) and then writes the pixel four times into new file.

Note that in all of the graphics algorithms have two above loops to read all pixel of the image and mostly the inside of the loops are difference with each other which does the main task.

In the next post I discuss about zoom out algorithm.