# MAT 201A Winter 2016
# Homework 3 : Use cross-correlation (or auto-correlation) to find features or similarities/differences in images.
# Treat this as an exploratory excersice where you bring in different things to compare, and then analyze/discuss
# the results. You should also explore analyzing the cross-correlation output matrix to extract the highest/lowest
# values, in a way that makes sense with your particular material.
# ======================================================================================================================
__author__ = 'Yitian Shao'
# yitianshao@umail.ucsb.edu
# Created on 01/30/2016
# ======================================================================================================================
%pylab inline
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
from scipy.ndimage.filters import maximum_filter
from IPython.display import Audio
from scipy.signal import correlate2d
import scipy.ndimage as ndimage
matplotlib.rcParams.update({'font.size': 30}) # Resize the font in figures
# ======================================================================================================================
# The following code aims to find the name "Manville" in an image of a newspaper published in November 28, 1941.
# ======================================================================================================================
# Import and display the image of newspaper
theNewspaper = imread('newspaper_Nov_28_1941.png')
# Keep only the Blue channel since the newspaper image has a yellow and red background.
theNewspaper = theNewspaper[:,:,2]
# Display the image
figure(figsize=(50,100))
imshow(theNewspaper, cmap=cm.gray)
axis('off')
title('The newspaper')
print("Size of the image: ", theNewspaper.shape, " with type:", theNewspaper.dtype)
# Import and display the image of the name "Manville"
manville = imread('Manville.png')
# Keep only the Blue channel, same reason.
manville = manville[:,:,2]
# Display the image
figure(figsize=(2,6))
imshow(manville, cmap=cm.gray)
axis('off')
title('The name need to be identified')
print("Size of the image: ",manville.shape, " with type:", manville.dtype)
Populating the interactive namespace from numpy and matplotlib Size of the image: (985L, 1667L) with type: float32 Size of the image: (22L, 83L) with type: float32
# Determine the threshold to covert the gray-scale image to binary image
threhold = median(theNewspaper) # choose the median as the threhold
# Convert the newspaper image to binary image
biNewspaper1 = where(theNewspaper > 0.8*threhold, 1, -1)
# Convert the name image to binary image
biManville = where(manville > 0.8*threhold, 1, -1)
# Display the newspaper image
figure(figsize=(50,100))
imshow(biNewspaper1, cmap=cm.gray)
axis('off')
title('The newspaper as binary image')
# Display the name image
figure(figsize=(2,6))
imshow(biManville, cmap=cm.gray)
axis('off')
title('The name need to be identified as binary image')
<matplotlib.text.Text at 0x150f5240>
# Convert gray-scale image of the newspaper to binary image by local threshold found in blocks
binSize = 300 # Size of the block to compute local threhold
rowNum = size(theNewspaper, axis = 0) # The height of the newspaper image
colNum = size(theNewspaper, axis = 1) # The width of the newspaper image
rowIndices=append(range(0, rowNum, binSize), rowNum - 1) # The row indices of blocks
colIndices=append(range(0, colNum, binSize), colNum - 1) # The col indices of blocks
biNewspaper = copy(theNewspaper)
for i in range(len(rowIndices)-1):
for j in range(len(colIndices)-1):
block = theNewspaper[rowIndices[i]:rowIndices[i+1], colIndices[j]:colIndices[j+1]]
# Determine the threshold to covert the gray-scale image to binary image
threhold = 0.8*median(block) # choose 0.8 median as the threhold
# Convert the newspaper image to binary image
biNewspaper[rowIndices[i]:rowIndices[i+1], colIndices[j]:colIndices[j+1]] = where(block > threhold, 1, -1)
# Display the image
figure(figsize=(50,100))
subplot(1,2,1)
imshow(biNewspaper1, cmap=cm.gray)
axis('off')
title('Before')
subplot(1,2,2)
imshow(biNewspaper, cmap=cm.gray)
axis('off')
title('After')
<matplotlib.text.Text at 0x2285bf60>
The above figure demostrates the difference between method using unique threshold and multiple local thresholds. Converting the orginal image to binary type by thresholding it with locally calculated threshod successully removed the "shadow". However, it also introduced some distortion to the figure in the newspaper, though it did not affect the performance of word identification.
# Compute the correlation between the newspaper image and the word image
resultImg = correlate2d(biNewspaper, biManville)
figure(figsize=(30,12))
imshow(maximum_filter(resultImg, (10,10)), interpolation = 'nearest') # Using maximum filter to clarify the results
colorbar()
axis('off')
title('The correlation between the newspaper image and word image')
<matplotlib.text.Text at 0x22a625c0>
# Identify the position of the matched word
x, y = nonzero(where(resultImg > resultImg.max()-300, 1, 0))
# Plot the original image as background
figure(figsize=(30,12))
imshow(theNewspaper, cmap=cm.gray)
axis('off')
title('The newspaper with the identified name marked')
# Draw red rectanulars around the identified word
wordWidth = size(biManville, axis = 0) # Width of the word
wordHeight = size(biManville, axis = 1) # Height of the word
for i in range(len(x)):
rect1 = Rectangle((y[i] - wordHeight,x[i] - wordWidth), wordHeight, wordWidth, color='r', fill = False)
gcf().gca().add_artist(rect1)
Identifying the name "Manville" was successful with 100% accuracy.
# ======================================================================================================================
# The following code aims to find the word "LOCAL NEWS" in an image of a newspaper published in November 28, 1941.
# The code is the same above.
# ======================================================================================================================
# Import and display the image of the word "LOCAL NEWS"
localnews = imread('LOCALNEWS.png')
# Keep only the Blue channel, same reason.
localnews = localnews[:,:,2]
# Display the image
figure(figsize=(2,6))
imshow(localnews, cmap=cm.gray)
axis('off')
title('The word need to be identified')
print("Size of the image: ",localnews.shape, " with type:", localnews.dtype)
# Convert the name image to binary image
biLocalnews = where(localnews > 0.8*threhold, 1, -1)
# Compute the correlation between the newspaper image and the word image
resultImg2 = correlate2d(biNewspaper, biLocalnews)
# Identify the position of the matched word
x, y = nonzero(where(resultImg2 > resultImg2.max()-300, 1, 0))
# Plot the original image as background
figure(figsize=(30,12))
imshow(theNewspaper, cmap=cm.gray)
axis('off')
title('The newspaper with the identified word marked')
# Draw red rectanulars around the identified word
wordWidth = size(biLocalnews, axis = 0) # Width of the word
wordHeight = size(biLocalnews, axis = 1) # Height of the word
for i in range(len(x)):
rect1 = Rectangle((y[i] - wordHeight,x[i] - wordWidth), wordHeight, wordWidth, color='r', fill = False)
gcf().gca().add_artist(rect1)
Size of the image: (30L, 163L) with type: float32
Identifying the word "Local News" was successful with 100% accuracy.
# The following code aims to find the word "in" in an image of a newspaper published in November 28, 1941.
# The code is the same above.
# ======================================================================================================================
# Import and display the image of the word "in"
wordIn = imread('in.png')
# Keep only the Blue channel, same reason.
wordIn = wordIn[:,:,2]
# Display the image
figure(figsize=(0.6,0.6))
imshow(wordIn, cmap=cm.gray)
axis('off')
title('The word need to be identified')
print("Size of the image: ",wordIn.shape, " with type:", wordIn.dtype)
# Convert the name image to binary image
biWordIn = where(wordIn > 0.8*threhold, 1, -1)
# Compute the correlation between the newspaper image and the word image
resultImg3 = correlate2d(biNewspaper, biWordIn)
# Identify the position of the matched word
x, y = nonzero(where(resultImg3 > resultImg3.max()-30, 1, 0))
# Plot the original image as background
figure(figsize=(30,12))
imshow(theNewspaper, cmap=cm.gray)
axis('off')
title('The newspaper with the identified word marked')
# Draw red rectanulars around the identified word
wordWidth = size(biWordIn, axis = 0) # Width of the word
wordHeight = size(biWordIn, axis = 1) # Height of the word
for i in range(len(x)):
rect1 = Rectangle((y[i] - wordHeight,x[i] - wordWidth), wordHeight, wordWidth, color='r', fill = False)
gcf().gca().add_artist(rect1)
Size of the image: (21L, 25L) with type: float32
As the word became smaller in size, the identify accuracy decreased. The wrongly identified words include "to" and suprisingly "a".