## MAT 201A Winter 2016
## HW 3
## Mark Hirsch
%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
from scipy.signal import correlate2d
from scipy.ndimage.filters import maximum_filter
from scipy.ndimage.filters import minimum_filter
Populating the interactive namespace from numpy and matplotlib
##import two images from same location with different orientation
img1 = imread('mezquita1.jpg')
img2 = imread('mezquita2 chunk.jpg')
imshow(img1)
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x119cc1dd0>
imshow(img2)
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x114d83690>
##check in on the data just to keep it in mind
img1.shape, img1.dtype
((1024, 1024, 3), dtype('uint8'))
##convert the image into 2D
img1 = sum(img1[:,:,:-1], axis=2)/3.0
imshow(img1)
colorbar()
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x119cc1f50>
##double check that last step
img1.shape, img1.dtype
((1024, 1024), dtype('float64'))
imshow(img1, cmap=cm.gray)
colorbar()
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x11ae11e10>
img2.shape,img2.dtype
((40, 242, 3), dtype('uint8'))
img2 = sum(img2[:,:,:-1], axis=2)/3.0
imshow(img2)
colorbar()
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x115854790>
##grayscale the image
imshow(img2, cmap=cm.gray)
colorbar()
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x11329ab90>
##run first autocorrelation in which a unique feature of the images are represented on opposing axis
##i.e. stripes in img2 run along "x-axis" while the stripes in img1 run more along a "z-axis"
cc = correlate2d(img1, img2)
imshow(cc)
colorbar()
gcf().set_figheight(8)
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x114ea1b10>
##use a maximum filter to highlight max values
imshow(maximum_filter(cc, (70,70)))
gcf().set_figheight(8)
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x11a75e590>
##run minimum filter to compare
imshow(minimum_filter(cc, (70,70)))
gcf().set_figheight(8)
xlabel('pixels')
ylabel('pixels')
<matplotlib.text.Text at 0x11581b7d0>
##the results of the autocorrelation above show that the unique feature (the arch stripes) were indeed correlated to
##a degree in which the general structure of the scene is somewhat apparent (i.e. where the striped arches are)
##this time, as a second test, load an image in which the axis of the stripes is more similar to img1
##repeat same processing as other two images
img3 = imread('mezquita2 chunk2.jpg')
imshow(img3)
<matplotlib.image.AxesImage at 0x11687d8d0>
img3 = sum(img3[:,:,:-1], axis=2)/3.0
imshow(img3)
colorbar()
<matplotlib.colorbar.Colorbar at 0x1177a5e90>
imshow(img3, cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar at 0x11790cb10>
##run autocorrelation between original img1 and the more aligned img3
##apply both max and min filters as before
cc = correlate2d(img1, img3)
imshow(cc)
colorbar()
gcf().set_figheight(8)
imshow(maximum_filter(cc, (70,70)))
gcf().set_figheight(8)
imshow(minimum_filter(cc, (70,70)))
gcf().set_figheight(8)
##the more aligned img3 resulted in a slightly higher correlation to img1...more areas of red in the top third of
##the matrix (especially around (200,150))