The purpose for this assignment is to compare the orginal image, a randomlly picked traffic image from the internet, to three basic shapes: rectangle, circle, and triangle.
%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
** The Original Image**
img = imread('traffic.jpg')
imshow(img)
title('The Orignal Image')
img.shape
(1152L, 1600L, 3L)
Turn the original image into a grayscle image by forming a weighted sum of the R, G, and B layers.
img_gray = 0.2989 * img[:, :, 0] + 0.5870 * img[:, :, 1] + 0.1140 * img[:, :, 2]
imshow(img_gray, cmap = cm.gray)
colorbar()
title('The Original Image in Grayscale')
img_gray.shape
(1152L, 1600L)
Convert the original image into a binary image using 0.7 * median as the threshold.
med = median(img_gray)
thld = 0.7 * med
img_bi = where(img_gray > thld, 1, -1)
imshow(img_bi, cmap = cm.gray)
colorbar()
title('The Original Image in Binary')
<matplotlib.text.Text at 0xd4ba0b8>
From the above binary image, it can be seen that details above line 200 are basically all gone. Therefore, chop the top 200 lines to reduce calculation.
img_bi = img_bi[200:1152, :]
imshow(img_bi, cmap = cm.gray)
colorbar()
title('The Original Image in Binary, Chopped')
<matplotlib.text.Text at 0xd7bc860>
** The Patterns **
** 1. Rectangle: **
rect = imread('Rectangle.png')
imshow(rect)
title('The Rectangle Pattern')
rect.shape
(152L, 172L, 3L)
subplot(131)
imshow(rect[:,:,0])
colorbar()
subplot(132)
imshow(rect[:,:,1])
colorbar()
subplot(133)
imshow(rect[:,:,2])
colorbar()
<matplotlib.colorbar.Colorbar at 0x12db4898>
Merge the three layers into one, and generate a binary rectangle pattern. Since the pattern is already in a binary form, the threshold is set to 0.5.
rect_gray = 1/3 * sum(rect[:,:,:], axis = 2)
rect_bi = where(rect_gray > 0.5, 1, -1)
imshow(rect_bi, cmap = cm.gray)
colorbar()
title('The Rectangle Pattern in Binary')
<matplotlib.text.Text at 0x132a1358>
** 2. Circle: **
circl = imread('Circle.png')
circl_gray = 1/3 * sum(circl[:,:,:], axis = 2)
circl_bi = where(circl_gray > 0.5, 1, -1)
imshow(circl_bi, cmap = cm.gray)
colorbar()
title('The Circle Pattern in Binary')
<matplotlib.text.Text at 0x13682400>
** 3. Triangle: **
trngl = imread('Triangle.png')
trngl_gray = 1/3 * sum(trngl[:,:,:], axis = 2)
trngl_bi = where(trngl_gray > 0.5, 1, -1)
imshow(trngl_bi, cmap = cm.gray)
colorbar()
title('The Triangle Pattern in Binary')
<matplotlib.text.Text at 0x13a55780>
** Compare the original image with the three patterns to find similarities **
from scipy.signal import correlate2d
from scipy.ndimage.filters import maximum_filter
cc_rect = correlate2d(img_bi, rect_bi)
subplot(121)
imshow( maximum_filter( cc_rect, (50,50) ), interpolation = 'nearest')
title('The Correlation between the Original Image in Binary and the Rectangle Pattern')
subplot(122)
imshow(img_bi, cmap = cm.gray)
<matplotlib.image.AxesImage at 0x289ea2e8>
cc_circl = correlate2d(img_bi, circl_bi)
subplot(121)
imshow( maximum_filter( cc_circl, (50,50) ), interpolation = 'nearest')
title('The Correlation between the Original Image in Binary and the Circle Pattern')
subplot(122)
imshow(img_bi, cmap = cm.gray)
<matplotlib.image.AxesImage at 0x289ea2b0>
cc_trngl = correlate2d(img_bi, trngl_bi)
subplot(121)
imshow( maximum_filter( cc_trngl, (50,50) ), interpolation = 'nearest')
title('The Correlation between the Original Image in Binary and the Trangle Pattern')
subplot(122)
imshow(img_bi, cmap = cm.gray)
<matplotlib.image.AxesImage at 0x28ded390>
From the above three correlation figures, it can be seen that the cars that are larger and closer to the bottom are better recognized by the rectangle pattern, while both the circle and the triangle pattern spotted the smaller figures but with plenty of useless information. All the three patterns show quite small relation with the tree, while the shadows of the the cars can be detected with all the three patterns.