This programming assignment is an introduction to OpenCV and its operations to images. Instead of using built-in functions of OpenCV, this assignment directly reads RGB value of pixels from input image and convert it to grayscale value. Expected challenges are type conversions and inaccurate pixel assignment.
Method and Implementation
The program reads face input image in color. By averaging R, G, and B components of each pixel, the generated grayscale values is written into grayscale Mat. The result is saved as "grayscale.png".
Second, using the grayscale result, I iterate through the pixels row by row and flip the image by assigning pixels from right the left. The result is saved as "flipped.png".
Third, I create a blur function which takes a Mat input and its size and output a blurred image. The blurring operation assigns each pixel to the average grayscale pixel value of itself and its neighbors. An if statement is used to handle the varying number of neighbors, for instance, corner of image only has 3 neighbors, border has 5 neighbors, and the rest has 8. By running the blur function 11 times, a significantly blurred output is saved as "blurred.png".
Finally, the 3 outputs are saved and displayed in 3 separate windows along with the input image.
Both grayscale and flipped image outputs meet expectation, so no extra tests are conducted. However, in order to create a significantly blurred image from the flipped image, I ran the blurring function with different number of iterations. Results are shown in the next section.
The program was tested on OSX machines with OpenCV installed only. Run /Applications/CMake.app/Contents/bin/cmake . in directory containing main program file and make to compile. Then run ./A1, as configured in CMakeLists.txt file, to run program.
The first table show the input image and 3 output results. The second table show outputs of flipped image blurred once, 4 times, and 11 times. Images are stored inside "html/img" folder.
|Input image||Grayscale image||Flipped image||Blurred image|
Blurred variable number of times
|Original image||Blurred once||Blurred 4 times||Blurred 11 times|
Discuss your method and results:
- One main strength of the program is its speed. All three operations are O(n), n referring to the number of pixels in input image, and hence the overall time complexity of the program is also O(n). Because both flipping and blurring operations use grayscale images as input, the speed of program is enhanced by using unsigned char as data type instead of relatively larger data types such as integer. Instead of reading from a 3-dimensional vector containing R, G, and B component, only brightness is evaluated when doing flipping and blurring operations.
- One weakness of the program could be found in the blur function. A large nested if statement is used to handle different parts of the image - corner vs. border vs. otherwise. It is written this way to save time, but the algorithm could be optimized by replacing the if statement with a better pixel-mapping model.
This programming assignment was very useful for me to brush up my OpenCV programming practices. I was successful in setting up OpenCV on my own machine and completing all tasks given in programming assignment. I look forward to writing more advanced computer vision algorithms, such as object detection, edge detection, and cascade classifiers.
Credits and Bibliography
Wired Watershed, HOWTO: Install, Build and Use openCV (MacOSX 10.10). https://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/
This is an independent work by Ellen Lo.