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 img.at
3. Using method img.at
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.
|Salt Noise Trial|
- For the grayscale image, three conversions turned out to be different. Based on the images shown in the results above, the first method turns out to be the best, since the second one achieves more black pixels and the third one achieves more white pixels. However, these three performances may change with different images. The first method performed best in this case may because of the large amount of yellow pixels. Which solution has the best performance in general needs to be discussed further.
- For the blurred image, it was supposed to denoise the salt noise image. Comparing the output image of salt noise image against blurred image, it can be seen that denoising is successful. In the future, different ways other than mean filter to denoise images can be tried, such as median filter.
- The results show that my method is generally successful. However, the use of "img.at" to assign values doesn't seem to be the most efficient way. Using pointers to traverse all pixels may cost less time, which I may try in the future.
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.