Commit 4769c728 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ImProc ass3: Added some comments.

parent f0c1703f
...@@ -3,14 +3,17 @@ from numpy import zeros ...@@ -3,14 +3,17 @@ from numpy import zeros
from matplotlib.pyplot import imread from matplotlib.pyplot import imread
def col2bin(color): def col2bin(color):
"""Get the histogram bin coordinates of a color."""
return tuple(map(lambda x: round(x - 1), color)) return tuple(map(lambda x: round(x - 1), color))
def domainIterator(image): def domainIterator(image):
"""Pixel iterator for colHist."""
for x in xrange(image.shape[0]): for x in xrange(image.shape[0]):
for y in xrange(image.shape[1]): for y in xrange(image.shape[1]):
yield x, y yield x, y
def colHist(image, bins, model): def colHist(image, bins, model):
"""Create the color histogram of an image."""
h = zeros(tuple(bins)) h = zeros(tuple(bins))
use = image.astype(float) * bins use = image.astype(float) * bins
...@@ -25,20 +28,22 @@ def colHist(image, bins, model): ...@@ -25,20 +28,22 @@ def colHist(image, bins, model):
return h return h
def histogramIntersect(h1, h2): def histogramIntersect(h1, h2):
"""Calculate the intersection between two color histograms."""
if h1.shape != h2.shape: if h1.shape != h2.shape:
raise ValueError('Shape mismatch between h1 and h2.') raise ValueError('Histograms h1 and h2 are not aligned.')
match = 0 match = 0
#print h1, h2
# Add the minimum of each bin to the result
for r in xrange(h1.shape[0]): for r in xrange(h1.shape[0]):
for g in xrange(h1.shape[1]): for g in xrange(h1.shape[1]):
for b in xrange(h1.shape[2]): for b in xrange(h1.shape[2]):
match += min(h1[r, g, b], h2[r, g, b]) match += min(h1[r, g, b], h2[r, g, b])
# Normalize by dividing by the number of pixels
return match / h2.sum() return match / h2.sum()
# Create table of intersections # Create table of intersections of images in the database
if __name__ == '__main__': if __name__ == '__main__':
bins = [8] * 3 bins = [8] * 3
db = map(lambda x: imread('database/%d.jpg' % x), range(1, 21)) db = map(lambda x: imread('database/%d.jpg' % x), range(1, 21))
...@@ -46,7 +51,7 @@ if __name__ == '__main__': ...@@ -46,7 +51,7 @@ if __name__ == '__main__':
for i, im in enumerate(db): for i, im in enumerate(db):
for j in xrange(i + 1, len(db)): for j in xrange(i + 1, len(db)):
#print 'comparing', i, j print 'comparing', i, j
table[i, j] = histogramIntersect(colHist(im, bins, 'rgb'), table[i, j] = histogramIntersect(colHist(im, bins, 'rgb'),
colHist(db[j], bins, 'rgb')) colHist(db[j], bins, 'rgb'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment