Junxiang Yao
%pylab inline
rcParams['figure.figsize'] = (30, 12)
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
First, I loaded an image and tried to do the acromatic vertion of this homework. This is a game which players finding panda from a crowd of snow men on it.
f = imread('findingpanda.png')
imshow(f)
<matplotlib.image.AxesImage at 0x10be680d0>
f.shape
(425, 499, 3)
I added calculate the mean of clues from 3 channels of each pixel.
f = sum(f.astype(float), axis=2)/3.0
imshow(f)
colorbar()
<matplotlib.colorbar.Colorbar at 0x10d5b8a10>
And then I set a threshold and changed the value into 1 and -1 based on the threshold.
f = where(f > 0.9, -1, 1)
imshow(f, cmap='gray')
colorbar()
<matplotlib.colorbar.Colorbar at 0x10de79410>
a = imread('panda.png')
imshow(a)
colorbar()
<matplotlib.colorbar.Colorbar at 0x10e8b5710>
I cut off the panda from the original picture, and saved it as another picture. And I did what I had done to the original picture again.
a = sum(a.astype(float), axis=2)/3.0
imshow(a)
colorbar()
<matplotlib.colorbar.Colorbar at 0x10f93df10>
a = where(a > 0.9, -1, 1)
imshow(a, cmap='gray')
colorbar()
<matplotlib.colorbar.Colorbar at 0x110c96cd0>
I calculated the correlation of those two pictures and show it below.
from scipy.signal import correlate2d
v = correlate2d(f, a)
imshow(v)
colorbar()
gcf().set_figheight(8)
And the red dot is very obvious in the picture above. In order to set off the red dot, I set another threshold and changed the value into 1 and 0.
imshow(where(v > 1000, 1, 0), interpolation='nearest', cmap=cm.gray)
colorbar()
gcf().set_figheight(8)
from scipy.ndimage.filters import maximum_filter
subplot(121)
imshow(maximum_filter(v, (50,50)))
subplot(122)
imshow(f, cmap=cm.gray)
gcf().set_figheight(8)
I think the result is very clear in this attempt. In order to highlight the result, I drew a circle around the panda inthe picture below.
hold(True)
imshow(f,cmap=cm.gray)
y,x=unravel_index(argmax(v), v.shape)
circle1=Circle((x-size(a,axis=1)/2,y-size(a,axis=0)/2),size(a,axis=1),color='r',fill=False,linewidth=5)
gca().add_artist(circle1)
<matplotlib.patches.Circle at 0x114fbb590>
Since the first result acceptable, I tried the acromatic version of this homework to explore more. And this time, I used a picture from a very famouse game series called"Finding Wally".
p = imread('wally.png')
imshow(p)
<matplotlib.image.AxesImage at 0x114844310>
p.shape
(1515, 2048, 3)
My strategy is comparing two pictures chaanel by channel, so I saved the value of different channel of original pictures into three two dimentional arrays.
pr = zeros([1515,2048],uint8)
pg = zeros([1515,2048],uint8)
pb = zeros([1515,2048],uint8)
height = 1515
width = 2048
for j in range(0,width):
for i in range(0,height):
pr [i,j] = p[i,j,0]
pg [i,j] = p[i,j,1]
pb [i,j] = p[i,j,2]
Next, I set an threshold, to change the value into 1 and -1.
pr=where(pr > 0.5, 1, -1)
pg=where(pg > 0.5, 1, -1)
pb=where(pb > 0.5, 1, -1)
And then, I basicly did the same thing again to the picture of Wally, which I cut off from the original picture.
w = imread('w.png')
imshow(w)
<matplotlib.image.AxesImage at 0x121773190>
w.shape
(35, 47, 4)
wr = zeros([47,47],uint8)
wg = zeros([47,47],uint8)
wb = zeros([47,47],uint8)
height = 35
width = 47
for j in range(0,width):
for i in range(0,height):
wr [i,j] = w[i,j,0]
wg [i,j] = w[i,j,1]
wb [i,j] = w[i,j,2]
wr=where(wr > 0.5, 1, -1)
wg=where(wg > 0.5, 1, -1)
wb=where(wb > 0.5, 1, -1)
I calculated the correlation of those two pictures for three times. And save the result in to thre arrays.
from scipy.signal import correlate2d
r = correlate2d(pr, wr)
g = correlate2d(pg, wg)
b = correlate2d(pb, wb)
Next, I sumed up all the values and checked if there was any obvious result.
cc = r + g + b
imshow(cc)
colorbar()
gcf().set_figheight(8)
imshow(cc, cmap=cm.gray)
colorbar()
gcf().set_figheight(8)
Although I tried two kinds of colorbar to show the result, the position of wally was not clear.
File "<ipython-input-8-e13512c82e49>", line 1 Although I tried two kinds of colorbar to show the result, the position of wally was not clear. ^ SyntaxError: invalid syntax
imshow(where(cc > 6450, 1, 0), interpolation='nearest', cmap=cm.gray)
colorbar()
gcf().set_figheight(8)
Fortunately, after setting threshold properly and changing the value in the result, the result finally showed up.
from scipy.ndimage.filters import maximum_filter
subplot(121)
imshow(maximum_filter(cc, (50,50)))
subplot(122)
imshow(p, cmap=cm.gray)
gcf().set_figheight(8)
To highlight the result, I drew a red circle around the result.
newimg = where(cc > 6450, 1, 0)
y,x = nonzero( newimg )
hold(True)
imshow(p)
circle1=Circle((x-size(w,axis=1)/2,y-size(w,axis=0)/2),size(w,axis=1),color='r',fill=False,linewidth=5)
gca().add_artist(circle1)
<matplotlib.patches.Circle at 0x114b31c90>