Assignment 1 Part 2

CS585 HW1
Tong Li

Problem Definition

The problem is to process images in different ways with my own functions instead of those given in the openCV. The program needs to input an image of my face, turn it into grayscale image, flipped image, salt noise image and blurred image, and then output these images. The goal is to get more and more familiar with programming with images based on what I've learned in class. The difficulties might be having few knowledge about operations that deal with images.

Method and Implementation

The main idea is to create a grayscale version of my face, flip the image of my face horizontally, create a salt noise version of the image and denoise the salted image by mean filter. So, I wrote four functions to manipulate images in order that they all turn out to be what are required.

For the grayscale image, I created the grayscale function in which I used two for loops to go through every row and column to get every pixel. The for loops were also used in other two functions. Once getting to a new pixel, convert three values to one by dividing the sum value of red, green and blue channels by three and assign the value to the pixel of output image in the same position.

For the horizontal flipped image, I created the flip function in which I used the same way as mentioned above to get every pixel. Once getting to a new pixel, assign three values to the symmetry pixel of output image.

For the salt noise image, I created the saltNoise function in which I chose pixels randomly and set their values of three channels to 255. It could make these random pixels turn white.

For the blurred image, I created the blur function in which I took the output of salt noise image as the input. If getting to a pixel that is not on the edge, assign to the red channel the average pixel value of red channel of itself and its eight neighbors, so does green and blue channels. If getting to an edge pixel, assign to it the same pixel value.


"boston.jpg" given in the lab section is the test image for the functions I wrote, since it could be shown perfectly. After the functions were proved to be executable, replacing it with an image of my face "li.jpg". Though the new image is 1079 * 721, a little bit larger than the test one, it can also be processed and shown correctly.

1. Using Mat to turn the input image into a matrix and define rows, columns and types of output images. "CV_8UC1" means it has only one single channel while "CV_8UC3" means it has three channels.
2. Using method, j)[0] to get the value of three channels in which 0 stands for green channel, 1 stands for red channel and 2 stands for blue channel.
3. Using method, j) to get this value of single channel.


For the grayscale image, I implemented three conversions discussed in class and they turned out differently. As shown below, the first grayscale image is achieved by dividing the sum value of red, green and blue channels by three. The second grayscale image is achieved by multiplying three channels by given coefficients. The third grayscale image is achieved by using the maximum value of three channels. Based on their performances, I chose to put the first method into final execution.


Source Image
Grayscale Trial
Flip Trial
Salt Noise Trial
Blur Trial



The overall performance of output images is pretty good, successfully taking the image of my face as an input, manipulating it in four ways and outputting the processed face. I've learned a lot more about the operations, such as defining varieties of matrices and assigning values of images during the programming process. I am able to manipulate images to produce different outputs, which can be used to solve problems in image processing, just like what I did with salt noise image in this experiment. Also, other solutions discussed above can be further implemented to improve performances.

Credits and Bibliography