#MAT 201A Winter 2016
#HW 3
#Zhenyu Yang
#import libraries
%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
from scipy.signal import freqz
from scipy.stats import mode
from PIL import Image,ImageDraw
from scipy.signal import correlate2d
from scipy.ndimage.filters import maximum_filter
Populating the interactive namespace from numpy and matplotlib
inputImage = imread('simpsons.jpg') #read image
imshow(inputImage) #show the read image
<matplotlib.image.AxesImage at 0x2cc3bcf8>
inputImageAfterSum = sum(inputImage[:,:,:-1], axis=2)/3.0 #mix coclor
inputImageAfterSum = where(inputImageAfterSum > 95, 1, 0) #filter data
imshow(inputImageAfterSum, cmap='gray')#show the filtered data
<matplotlib.image.AxesImage at 0x1bbffeb8>
imageToBeCompared = imread('face2.jpg')# load image to be compared
imshow(imageToBeCompared) # show the loaded image
<matplotlib.image.AxesImage at 0x1beb1748>
imageToBeComparedSum = sum(imageToBeCompared[:,:,:-1], axis=2)/3.0 #mix coclor
imageToBeComparedSum = where(imageToBeComparedSum >90 , 1, 0)#filter data
imshow(imageToBeComparedSum, cmap='gray')#show the filtered data
<matplotlib.image.AxesImage at 0x1ed634e0>
#This step is to get the size of compared image, this size data will be used to
#determin the offset when we draw a circle on the original image
widthOfFace,LengthOfFace = imageToBeComparedSum.shape
#Due to the picture size, the correlation calculation takes a long time
cc = correlate2d(inputImageAfterSum, imageToBeComparedSum) #do the correlation
imshow(cc) #show the correlation result, use colorbar to show different value
colorbar()
gcf().set_figheight(8)
subplot(121) #sunbplots
#Show the points whose value is larger than a certain threshold(here is 13500)
#For satisfied points, draw a square on their positions
imshow(maximum_filter(where(cc > 13500, 1, 0), (70,70)))
subplot(122)
hold(True)
imshow(inputImage) # show the original image on the right side, compare
gcf().set_figheight(8)
#the following part is to find the x and y value of satisfied points, and draw
#circles on the original image to mark those satisfied points.
#Certain offset is introduced to correct the positions of circles.
mf = maximum_filter(cc, (50,50))
y,x = unravel_index(argmax(mf), mf.shape)
circleDetect = Circle((x-LengthOfFace/2.0,y-widthOfFace/2.0),100,color = "r",fill=False, linewidth = 4)
gca().add_artist(circleDetect)
<matplotlib.patches.Circle at 0x2ceba550>
# Show the right side image alone so that it looks bigger.
hold(True)
imshow(inputImage, cmap=cm.gray)
gcf().set_figheight(8)
mf = maximum_filter(cc, (50,50))
y,x = unravel_index(argmax(mf), mf.shape)
circleDetect = Circle((x-LengthOfFace/2.0,y-widthOfFace/2.0),100,color = "r",fill=False, linewidth = 10)
gca().add_artist(circleDetect)
<matplotlib.patches.Circle at 0x3abf59e8>