%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
img1 = imread('building.jpg')
imshow(img1)
<matplotlib.image.AxesImage at 0x13d3d470>
img1.shape
(490, 600, 3)
img1.dtype
dtype('uint8')
img2 = sum(img1[:,:,:], axis=2)/3.0 #Obtain the heat map - The image now does not have a third dimension
imshow(img2)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x13FE2E40>
img2.shape
(490, 600)
imshow(img2, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x14A84C60>
img3 = where(img2 >230 , 0, 1) #Invert the image
imshow(img3, cmap='gray')
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x13A5EDA0>
i1 = imread('cross.png')
imshow(i1)
<matplotlib.image.AxesImage at 0x14fc7b90>
i1.shape
(55, 45, 3)
i1.dtype
dtype('float32')
i2 = sum(i1[:,:,:], axis=2)/3.0 #Obtaining the heat map
imshow(i2)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x1489D5F8>
imshow(i2, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x13771F30>
i3 = where(i2 > 0.9, 0, 1) #Invert the image
imshow(i3, cmap=cm.gray, interpolation='nearest')
<matplotlib.image.AxesImage at 0x17e8b330>
from scipy.signal import correlate2d
cc = correlate2d(img3, i3)
imshow(cc)
colorbar()
gcf().set_figheight(8)
imshow(where(cc >2000 , 0 ,1),interpolation='nearest', cmap=cm.gray) colorbar() gcf().set_figheight(8)
from scipy.ndimage.filters import maximum_filter
#Maximum filter is used for better readability of the correlation output
imshow(maximum_filter(cc, (10,10)))
gcf().set_figheight(8)
subplot(121)
imshow(maximum_filter(cc, (10,10)))
gcf().set_figheight(8)
subplot(122)
imshow(img1, cmap=cm.gray)
gcf().set_figheight(8)
img1 = imread('snowmen-panda-600.jpg') #Reading the original image, we need to find the panda amongst the snowmen
imshow(img1)
<matplotlib.image.AxesImage at 0x183f8670>
img1.shape
(600, 600, 3)
img1.dtype
dtype('uint8')
img2 = sum(img1[:,:,:], axis=2)/3.0
imshow(img2)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x152E27B0>
imshow(img2, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x14ADBC10>
img3 = where(img2 > 230, 0, 1)
imshow(img3, cmap='gray')
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x183DFDF0>
i1 = imread('panda1.png') #Reading the sub image to be correlated with (This is what we need to find)
imshow(i1)
<matplotlib.image.AxesImage at 0x1833cc10>
i1.shape
(47, 46, 3)
i1.dtype
dtype('float32')
i2 = sum(i1[:,:,:], axis=2)/3.0
imshow(i2)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x18700940>
imshow(i2, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar instance at 0x189BD4B8>
i3 = where(i2 > 0.9, 0, 1)
imshow(i3, cmap=cm.gray, interpolation='nearest')
<matplotlib.image.AxesImage at 0x1b03ae70>
from scipy.signal import correlate2d #Performing 2-Dcorrelation between the two images to produce regions of good match.
cc = correlate2d(img3, i3)
imshow(cc)
colorbar()
gcf().set_figheight(8)
imshow(cc, cmap=cm.gray)
colorbar()
gcf().set_figheight(8)
imshow(where(cc >100 , 0 ,1),interpolation='nearest', cmap=cm.gray)
colorbar()
gcf().set_figheight(8)
from scipy.ndimage.filters import maximum_filter
imshow(maximum_filter(cc, (10,10)))
gcf().set_figheight(8)
subplot(121)
imshow(maximum_filter(cc, (10,10)))
gcf().set_figheight(8)
subplot(122)
imshow(img1, cmap=cm.gray)
gcf().set_figheight(8)
mf = maximum_filter(cc, (10,10))
argmax(mf) # extract the highest value from filtered cross correlaton matrix
208825
mf.shape
(646, 645)
unravel_index(argmax(mf), mf.shape) #the index position at which highest correlation occured.
(323, 490)
argmax(cc) #Similarly extract the highest value from cross correlaton matrix, also of size (646,645)
211409
unravel_index(argmax(cc), cc.shape)
(327, 494)