Commit e19fada3 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ImProc ass3: Found Waldo.

parent aa0dbb3f
#!/usr/bin/env python #!/usr/bin/env python
from numpy import zeros from numpy import zeros
from matplotlib.pyplot import imread, imshow, show from matplotlib.pyplot import subplot, imread, imshow, show, plot
from intersect import col2bin, domainIterator, colHist, histogramIntersect from intersect import col2bin, domainIterator, colHist, histogramIntersect
from scipy.ndimage import correlate
def convolution(image, radius): def convolution(image, radius):
"""Calculate the convolution of an image with a specified circle radius.""" """Calculate the convolution of an image with a specified circle radius."""
c = zeros(image.shape)
r_sq = radius ** 2
w, h = image.shape[:2]
# Loop to the square that surrounds the circle, and check if the pixel # Loop to the square that surrounds the circle, and check if the pixel
# is inside the disk # is inside the circle
for x, y in domainIterator(image): r_sq = radius ** 2
circle_sum = 0. mask = zeros((2 * radius + 1, 2 * radius + 1), dtype=int)
pixels = 0
for dx in xrange(-radius, radius + 1):
for dy in xrange(-radius, radius + 1):
cx = x + dx
cy = y + dy
if cx >= 0 and cy >= 0 and cx < w and cy < h \
and dx ** 2 + dy ** 2 < r_sq:
circle_sum += image[cx, cy]
pixels += 1
if pixels: for x, y in domainIterator(mask):
c[x, y] = circle_sum / pixels if (x - radius) ** 2 + (y - radius) ** 2 < r_sq:
mask[x, y] = 1
return c return correlate(image, mask, mode='nearest')
def hbp(image, environment, bins, model, radius): def hbp(image, environment, bins, model, radius):
"""Create the histogram back projection of two images.""" """Create the histogram back projection of two images."""
...@@ -41,59 +28,59 @@ def hbp(image, environment, bins, model, radius): ...@@ -41,59 +28,59 @@ def hbp(image, environment, bins, model, radius):
R = zeros(bins) R = zeros(bins)
for c in domainIterator(R, 3): for c in domainIterator(R, 3):
if (I[c] != 0).all(): if I[c] != 0:
R[c] = M[c] / I[c] R[c] = float(M[c]) / float(I[c])
# Create back projection # Create back projection
print 'Creating back projection...' print 'Creating back projection...'
b = zeros(environment.shape[:2]) b = zeros(environment.shape[:2])
use = environment.astype(float) * bins use = environment.astype(float) * map(lambda x: x - 1, bins)
if model == 'rgb': if model == 'rgb':
use /= 255 use /= 255
elif model == 'hsv': elif model == 'hsv':
# TODO: implement HSV color model for p in domainIterator(image):
pass use[p] = rgb_to_hsv(*use[p].tolist())
for p in domainIterator(b): for p in domainIterator(b):
b[p] = min(R[col2bin(use[p])], 1) b[p] = min(R[col2bin(use[p])], 1.)
# Create convolution to create a peak value # Create convolution to create a peak value
print 'Creating convolution...' print 'Creating convolution...'
return b return convolution(b, radius)
#return convolution(b, radius)
def loc(image, color): def loc(image, color):
pass for p in domainIterator(image):
if (image[p] == color).all():
return p
return (0, 0)
def find_image(image, environment, bins, model, radius): def find_image(image, environment, bins, model, radius):
"""Find the location of the peak value of a back projection histogram.""" """Find the location of the peak value of a back projection histogram."""
b = hbp(image, environment, bins, model, radius) b = hbp(image, environment, bins, model, radius)
return loc(environment, b.max()) return loc(b, b.max())
if __name__ == '__main__': if __name__ == '__main__':
print 'Reading images...' print 'Reading images...'
waldo = imread('waldo.tiff') waldo = imread('waldo.tiff')
env = imread('waldo_env.tiff') env = imread('waldo_env.tiff')
b = hbp(waldo, env, [8] * 3, 'rgb', 8) b = hbp(waldo, env, [64] * 3, 'rgb', 10)
imshow(b, cmap='gray', origin='lower') subplot(121)
show() imshow(env, origin='lower')
import sys subplot(122)
sys.exit(0) imshow(b, origin='lower')
print 'Mapping projection over original image...'
result = env.copy()
for p in domainIterator(result):
result[p] *= b[p]
print 'done'
imshow(result, origin='lower')
#imshow(b * env)
# Draw a rectangle around the found center pixel # Draw a rectangle around the found center pixel
#x, y = find_image(waldo, env, [8] * 3, 'rgb') #x, y = find_image(waldo, env, [64] * 3, 'rgb', 10)
#w, h = waldo.shape[:2] #w, h = waldo.shape[:2]
#plot([x - w / 2, x + w / 2], [y - h / 2, y + h / 2], 'r-') #l = x - w / 2
#imshow(env) #r = x + w / 2
#b = y - h / 2
#t = y + h / 2
#plot([t, t, b, b], [l, r, r, l], 'r-')
#imshow(env, origin='lower')
show() show()
...@@ -4,8 +4,7 @@ from matplotlib.pyplot import imread ...@@ -4,8 +4,7 @@ from matplotlib.pyplot import imread
def col2bin(color): def col2bin(color):
"""Get the histogram bin coordinates of a color.""" """Get the histogram bin coordinates of a color."""
#return tuple(map(lambda x: round(x - 1), color)) return tuple(color.astype(int))
return tuple(color.astype(int) - 1)
def domainIterator(image, dim=2): def domainIterator(image, dim=2):
"""Pixel iterator for arrays of with 2 or 3 dimensions.""" """Pixel iterator for arrays of with 2 or 3 dimensions."""
...@@ -21,8 +20,8 @@ def domainIterator(image, dim=2): ...@@ -21,8 +20,8 @@ def domainIterator(image, dim=2):
def colHist(image, bins, model): def colHist(image, bins, model):
"""Create the color histogram of an image.""" """Create the color histogram of an image."""
h = zeros(bins) h = zeros(bins, dtype=int)
use = image.astype(float) * bins use = image.astype(float) * map(lambda x: x - 1, bins)
if model == 'rgb': if model == 'rgb':
use /= 255 use /= 255
......
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