Commit 5433f88d authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of vo20.nl:/git/uva

parents 582a56d8 d87980d0
......@@ -66,9 +66,7 @@ def hbp(image, environment, bins, model, radius, mask=None):
b = zeros(environment.shape[:2])
use = environment.astype(float) * map(lambda x: x - 1, bins)
if model == 'rgb':
use /= 255
elif model == 'hsv':
if model == 'hsv':
for p in domainIterator(image):
use[p] = rgb_to_hsv(*use[p].tolist())
......@@ -92,16 +90,16 @@ def exclude_color(color, image):
if __name__ == '__main__':
print 'Reading images...'
waldo = imread('waldo.tiff')
env = imread('waldo_env.tiff')
waldo = imread('waldo.tiff') / 255.
env = imread('waldo_env.tiff') / 255.
# Use a mask to ignore the white background in the Waldo image
mask = exclude_color([255] * 3, waldo)
# Execute the back projection algorithm
import pickle
#import pickle
b = hbp(waldo, env, [64] * 3, 'rgb', 15, mask)
pickle.dump(b, open('projection.dat', 'w'))
#pickle.dump(b, open('projection.dat', 'w'))
#b = pickle.load(open('projection.dat', 'r'))
w, h = waldo.shape[:2]
......
#!/usr/bin/env python
from numpy import zeros
from matplotlib.pyplot import imread
from colorsys import rgb_to_hsv
def col2bin(color):
"""Get the histogram bin coordinates of a color."""
......@@ -21,18 +22,17 @@ def domainIterator(image, dim=2):
def colHist(image, bins, model, mask=None):
"""Create the color histogram of an image."""
h = zeros(bins, dtype=int)
use = image.astype(float)
# Multiply with the number of bins to normalize bin coordinates
use = image.astype(float) * map(lambda x: x - 1, bins)
if model == 'rgb':
use /= 255
elif model == 'hsv':
# TODO: implement HSV color model
pass
else:
if model == 'hsv':
for p in domainIterator(image):
use[p] = rgb_to_hsv(*use[p].tolist())
elif model != 'rgb':
raise ValueError('Color model "%s" is not supported.' % model)
# Multiply with the number of bins to normalize bin coordinates
use *= map(lambda x: x - 1, bins)
for p in domainIterator(image):
# Optionally use a mask over the image
if mask is None or mask[p]:
......@@ -55,12 +55,22 @@ def histogramIntersect(h1, h2):
return float(match) / h2.sum()
if __name__ == '__main__':
from sys import argv
count = 20
model = 'rgb'
bins = [64] * 3
# Usage: python intersect.py [ count [ model ] ]
if len(argv) > 1:
count = int(argv[1])
if len(argv) > 2:
model = argv[2]
# Create table of intersections of images in the database
print 'Creating histograms...'
hist = [colHist(imread('database/%d.jpg' % i), \
bins, 'rgb') for i in xrange(1, 4)]
hist = [colHist(imread('database/%d.jpg' % i) / 255., \
bins, model) for i in xrange(1, count + 1)]
table = zeros((len(hist), len(hist)), dtype=int)
......@@ -68,7 +78,6 @@ if __name__ == '__main__':
table[i, i] = 100
for j in xrange(i + 1, len(hist)):
print 'comparing %d, %d' % (i, j)
table[i, j] = table[j, i] \
= round(histogramIntersect(hist[i], hist[j]) * 100)
......
[[100 36 50 22 25 35 20 28 38 29 32 37 17 38 47 28 39 34 16 30]
[ 36 100 48 49 43 48 34 61 52 12 37 55 12 51 50 47 52 57 11 49]
[ 50 48 100 30 33 52 18 45 49 37 32 58 22 38 48 30 51 42 19 45]
[ 22 49 30 100 41 34 33 40 36 5 32 36 7 30 37 26 27 54 10 28]
[ 25 43 33 41 100 42 25 43 38 4 36 39 4 37 35 31 30 40 5 34]
[ 35 48 52 34 42 100 22 48 42 13 34 53 12 42 43 33 41 46 10 40]
[ 20 34 18 33 25 22 100 25 22 4 25 22 4 25 24 27 21 32 15 24]
[ 28 61 45 40 43 48 25 100 41 4 33 56 5 41 40 36 44 47 6 43]
[ 38 52 49 36 38 42 22 41 100 17 35 46 12 44 40 35 48 35 7 43]
[ 29 12 37 5 4 13 4 4 17 100 6 17 45 14 20 5 18 11 42 7]
[ 32 37 32 32 36 34 25 33 35 6 100 35 4 33 34 27 31 35 5 37]
[ 37 55 58 36 39 53 22 56 46 17 35 100 12 48 55 37 51 46 9 48]
[ 17 12 22 7 4 12 4 5 12 45 4 12 100 12 12 4 16 12 58 5]
[ 38 51 38 30 37 42 25 41 44 14 33 48 12 100 46 45 44 40 13 40]
[ 47 50 48 37 35 43 24 40 40 20 34 55 12 46 100 35 44 47 14 38]
[ 28 47 30 26 31 33 27 36 35 5 27 37 4 45 35 100 49 35 5 41]
[ 39 52 51 27 30 41 21 44 48 18 31 51 16 44 44 49 100 39 13 52]
[ 34 57 42 54 40 46 32 47 35 11 35 46 12 40 47 35 39 100 14 36]
[ 16 11 19 10 5 10 15 6 7 42 5 9 58 13 14 5 13 14 100 5]
[ 30 49 45 28 34 40 24 43 43 7 37 48 5 40 38 41 52 36 5 100]]
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