Description: In this homework, what I'm trying to do is use cross-correlation to find features and difference in images. My idea comes from a game in which you are suppose to find differences between two very similar images. So I imported two similar pictures, and try to use cross-correlation to determine if which part exists only in one image and which part exists both in two images.
Firstly, I picked small piece from image1 with size of 50 * 50. Then I do cross-correlation to both of the images withi this piece. Apparently, the result with image1 is the correct answer since the piece is came from img1. Therefore we can compare the result with img2 so that we could know if img2 include this piece.
I tested 3 different times in this homework. The first two is common piece that two images both have. In the last test, only img1 contain the piece. And satisfactorily, all 3 results can correctly reflect the truthes. So my algorithm works as I expect.
The code are as follow:
%pylab inline
from __future__ import print_function
from __future__ import division
from scipy.signal import correlate2d
import numpy as np
from scipy.ndimage.filters import maximum_filter
Populating the interactive namespace from numpy and matplotlib
Import libraries
img1 = imread('littlegirl1.png')
img2 = imread('littlegirl2.png')
figure(figsize = (10, 25))
subplot(121)
imshow(img1)
title("Little Girl 1")
subplot(122)
imshow(img2)
title("Little Girl 2");
img1.shape
(1000, 754, 4)
img2.shape
(1000, 754, 4)
Print out the size of two picture and make sure they are in same size
img1.dtype
dtype('float32')
img1r = img1[:,:,0]
img1b = img1[:,:,1]
img1g = img1[:,:,2]
img2r = img2[:,:,0]
img2b = img2[:,:,1]
img2g = img2[:,:,2]
img1Ave = sum(img1[:,:,:-1], axis=2)/3.0
img2Ave = sum(img2[:,:,:-1], axis=2)/3.0
Seperate each layers and to average on all 3 layers
img1Ave = where(img1Ave < 0.32, -1, 1)
imshow(img1Ave, cmap=cm.gray)
colorbar();
I tried several different numbers as threshold, and find that 0.32 perform the best for this case
img1r = where(img1r < 0.32, -1, 1)
img1b = where(img1b < 0.32, -1, 1)
img1g = where(img1g < 0.32, -1, 1)
img2r = where(img2r < 0.32, -1, 1)
img2b = where(img2b < 0.32, -1, 1)
img2g = where(img2g < 0.32, -1, 1)
img2Ave = where(img2Ave < 0.32, -1, 1)
#imshow(img1g, cmap=cm.gray)
#colorbar()
clean all other data
1. Pick a common piece that both img1 and img2 have, and test
size = 50
piece = img1Ave[0:0+size,0:0+size]
imshow(img1[0:0+size,0:0+size]);
cc1 = correlate2d(img1Ave, piece)
cc2 = correlate2d(img2Ave, piece)
mf1 = maximum_filter(cc1, (50,50))
mf2 = maximum_filter(cc2, (50,50))
print(argmax(mf1), ", ", argmax(mf2))
print(unravel_index(argmax(mf1), mf1.shape), ", ", unravel_index(argmax(mf2), mf2.shape))
print("Found similar pattern in image2!")
subplot(121)
imshow(cc1)
subplot(122)
imshow(cc2)
colorbar();
743080 , 743078 (925, 305) , (925, 303) Found similar pattern in image2!
The out put have similar max value and similar coodinate, which means we found similar pattern in image2.
2. Pick another common piece that both img1 and img2 have, and test
size = 50
piece = img1Ave[900:900+size,280:280+size]
imshow(img1[900:900+size,280:280+size])
cc1 = correlate2d(img1Ave, piece)
cc2 = correlate2d(img2Ave, piece)
mf1 = maximum_filter(cc1, (50,50))
mf2 = maximum_filter(cc2, (50,50))
print(argmax(mf1), ", ", argmax(mf2))
print(unravel_index(argmax(mf1), mf1.shape), ", ", unravel_index(argmax(mf2), mf2.shape))
print("Found similar pattern in image2!")
subplot(121)
imshow(cc1)
subplot(122)
imshow(cc2)
colorbar();
743080 , 743078 (925, 305) , (925, 303) Found similar pattern in image2!
The out put have similar max value and similar coodinate, which means we found similar pattern in image2.
3. Pick a piece that only img1 has but does not exist in img2, and test
size = 50
piece = img1Ave[360:360+size,250:250+size]
imshow(img1[360:360+size,250:250+size])
cc1 = correlate2d(img1Ave, piece)
cc2 = correlate2d(img2Ave, piece)
mf1 = maximum_filter(cc1, (50,50))
mf2 = maximum_filter(cc2, (50,50))
print(argmax(mf1), ", ", argmax(mf2))
print(unravel_index(argmax(mf1), mf1.shape), ", ", unravel_index(argmax(mf2), mf2.shape))
print("Couldn't find this pattern in image2...")
subplot(121)
imshow(cc1)
subplot(122)
imshow(cc2)
colorbar();
309430 , 773380 (385, 275) , (963, 91) Couldn't find this pattern in image2...
The out put have extremely different max values and totally different coodinates, which the pattern we pick in image1 does not exist in image2. This is right.