Commit 996861ca authored by Taddeüs Kroes's avatar Taddeüs Kroes

improc ass3: Added support for alternative color model HSV.

parent 5dcf393f
...@@ -66,9 +66,7 @@ def hbp(image, environment, bins, model, radius, mask=None): ...@@ -66,9 +66,7 @@ def hbp(image, environment, bins, model, radius, mask=None):
b = zeros(environment.shape[:2]) b = zeros(environment.shape[:2])
use = environment.astype(float) * map(lambda x: x - 1, bins) use = environment.astype(float) * map(lambda x: x - 1, bins)
if model == 'rgb': if model == 'hsv':
use /= 255
elif model == 'hsv':
for p in domainIterator(image): for p in domainIterator(image):
use[p] = rgb_to_hsv(*use[p].tolist()) use[p] = rgb_to_hsv(*use[p].tolist())
...@@ -92,16 +90,16 @@ def exclude_color(color, image): ...@@ -92,16 +90,16 @@ def exclude_color(color, image):
if __name__ == '__main__': if __name__ == '__main__':
print 'Reading images...' print 'Reading images...'
waldo = imread('waldo.tiff') waldo = imread('waldo.tiff') / 255.
env = imread('waldo_env.tiff') env = imread('waldo_env.tiff') / 255.
# Use a mask to ignore the white background in the Waldo image # Use a mask to ignore the white background in the Waldo image
mask = exclude_color([255] * 3, waldo) mask = exclude_color([255] * 3, waldo)
# Execute the back projection algorithm # Execute the back projection algorithm
import pickle #import pickle
b = hbp(waldo, env, [64] * 3, 'rgb', 15, mask) 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')) #b = pickle.load(open('projection.dat', 'r'))
w, h = waldo.shape[:2] w, h = waldo.shape[:2]
......
#!/usr/bin/env python #!/usr/bin/env python
from numpy import zeros from numpy import zeros
from matplotlib.pyplot import imread from matplotlib.pyplot import imread
from colorsys import rgb_to_hsv
def col2bin(color): def col2bin(color):
"""Get the histogram bin coordinates of a color.""" """Get the histogram bin coordinates of a color."""
...@@ -21,18 +22,17 @@ def domainIterator(image, dim=2): ...@@ -21,18 +22,17 @@ def domainIterator(image, dim=2):
def colHist(image, bins, model, mask=None): def colHist(image, bins, model, mask=None):
"""Create the color histogram of an image.""" """Create the color histogram of an image."""
h = zeros(bins, dtype=int) h = zeros(bins, dtype=int)
use = image.astype(float)
# Multiply with the number of bins to normalize bin coordinates if model == 'hsv':
use = image.astype(float) * map(lambda x: x - 1, bins) for p in domainIterator(image):
use[p] = rgb_to_hsv(*use[p].tolist())
if model == 'rgb': elif model != 'rgb':
use /= 255
elif model == 'hsv':
# TODO: implement HSV color model
pass
else:
raise ValueError('Color model "%s" is not supported.' % model) 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): for p in domainIterator(image):
# Optionally use a mask over the image # Optionally use a mask over the image
if mask is None or mask[p]: if mask is None or mask[p]:
...@@ -55,12 +55,22 @@ def histogramIntersect(h1, h2): ...@@ -55,12 +55,22 @@ def histogramIntersect(h1, h2):
return float(match) / h2.sum() return float(match) / h2.sum()
if __name__ == '__main__': if __name__ == '__main__':
from sys import argv
count = 20
model = 'rgb'
bins = [64] * 3 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 # Create table of intersections of images in the database
print 'Creating histograms...' hist = [colHist(imread('database/%d.jpg' % i) / 255., \
hist = [colHist(imread('database/%d.jpg' % i), \ bins, model) for i in xrange(1, count + 1)]
bins, 'rgb') for i in xrange(1, 4)]
table = zeros((len(hist), len(hist)), dtype=int) table = zeros((len(hist), len(hist)), dtype=int)
...@@ -68,7 +78,6 @@ if __name__ == '__main__': ...@@ -68,7 +78,6 @@ if __name__ == '__main__':
table[i, i] = 100 table[i, i] = 100
for j in xrange(i + 1, len(hist)): for j in xrange(i + 1, len(hist)):
print 'comparing %d, %d' % (i, j)
table[i, j] = table[j, i] \ table[i, j] = table[j, i] \
= round(histogramIntersect(hist[i], hist[j]) * 100) = 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