Face Image Manipulation

CS 585 HW 1
Ziliang ZHU
29/Jan/2020


Problem Definition

To manipulate a face image in multiple ways to get familiar with image operations.

CS585 Assignment 1

Algorithms used:

Calculate the average of RGB channels to get a grayscale image.

Flip the values along axis 1 to get a horizontally flipped image.

Calculate the distance ratio from each pixel to the horizontal central line and calculate the average value among larger range when the pixel is more distant from the center to get a horizontally boundary blurry image.


Method and Implementation

Using OpenCV and Python, load an image, convert it into grayscale, flip it horizontally, and eventually do a boundary blur.

Code: import cv2 as cv import numpy as np IMG = 'selfie.jpg' # convert color image to gray scale using average of RGB def ColorToGray(im): im2 = np.sum(im,axis=2) / (255 * 3) return im2 # save grayscale images as RGB def imwriteGray(filename, im): im2 = np.expand_dims(im,axis=2) * [[[1, 1, 1]]] * 255 cv.imwrite(filename, im2) # flip image by flipping values in axis 1 def FlipHor(im): # im2 = np.flip(im, axis=1) im2 = im.copy() # iterate through all columns for i in range(im2.shape[1] // 2): temp = im2[:,i,:].copy() im2[:,i,:] = im2[:,im2.shape[1] - i - 1,:] im2[:,im2.shape[1] - i - 1,:] = temp return im2 # make the left and right boundary of images blurry and maintain the middle part clear def BoundBlurHor(im): im2 = im.copy() mid = im2.shape[1] / 2 # iterate through columns for i in range(im2.shape[1]): norm_dist = np.absolute(mid - i) / mid # the closer to the edge, the more blurry the pixels gets if (norm_dist <= 0.2): WIDTH = 1 else: WIDTH = int(norm_dist * 50) im2[:,i,:] = np.average(im[:,np.maximum(0,i-WIDTH):np.minimum(im2.shape[1]-1,i+WIDTH),:],axis=1) return im2 def main(): im = cv.imread(IMG) cv.imshow('original',im) im2 = ColorToGray(im) cv.imshow('grayscale',im2) imwriteGray('selfie_grayscale.jpg',im2) im3 = FlipHor(im) cv.imshow('horizontal flip',im3) cv.imwrite('selfie_horizontal_flip.jpg',im3) im4 = BoundBlurHor(im) cv.imshow('horizontal boundary blur',im4) cv.imwrite('selfie_horizontal_boundary_blur.jpg',im4) if __name__ == '__main__': main()


Results

Image results are presented.

Original me in 2016 Grayscale
Horizontal flip Horizontal boundary blur

Discuss your method and results:


Conclusions

The project illustrates some interesting image operations and gets us more familiar with library functions.


Credits and Bibliography

The codes are solely written by myself. All contents are enlightened by lecture or assignment guidances.