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

ImProc ass3: Added some debug lines.

parent 45ce1a1b
...@@ -3,15 +3,18 @@ from numpy import zeros ...@@ -3,15 +3,18 @@ from numpy import zeros
from matplotlib.pyplot import imread, imshow, show from matplotlib.pyplot import imread, imshow, show
from intersect import col2bin, domainIterator, colHist, histogramIntersect from intersect import col2bin, domainIterator, colHist, histogramIntersect
def D(image, radius, x, y): def convolution(image, radius):
"""Get the average value of a circle around a pixel.""" """Calculate the convolution of an image with a specified circle radius."""
disk_sum = 0. c = zeros(image.shape)
pixels = 0
r_sq = radius ** 2 r_sq = radius ** 2
w, h = image.shape[: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 disk
for x, y in domainIterator(image):
circle_sum = 0.
pixels = 0
for dx in xrange(-radius, radius + 1): for dx in xrange(-radius, radius + 1):
for dy in xrange(-radius, radius + 1): for dy in xrange(-radius, radius + 1):
cx = x + dx cx = x + dx
...@@ -19,17 +22,11 @@ def D(image, radius, x, y): ...@@ -19,17 +22,11 @@ def D(image, radius, x, y):
if cx >= 0 and cy >= 0 and cx < w and cy < h \ if cx >= 0 and cy >= 0 and cx < w and cy < h \
and dx ** 2 + dy ** 2 < r_sq: and dx ** 2 + dy ** 2 < r_sq:
disk_sum += image[cx, cy] circle_sum += image[cx, cy]
pixels += 1 pixels += 1
return disk_sum / pixels if pixels else 0 if pixels:
c[x, y] = circle_sum / pixels
def convolution(image, radius):
"""Calculate the convolution of an image with a specified disk radius."""
c = zeros(image.shape)
for x, y in domainIterator(image):
c[x, y] = D(image, radius, x, y)
return c return c
...@@ -37,11 +34,11 @@ def hbp(image, environment, bins, model, radius): ...@@ -37,11 +34,11 @@ def hbp(image, environment, bins, model, radius):
"""Create the histogram back projection of two images.""" """Create the histogram back projection of two images."""
# Create image histograms # Create image histograms
print 'Creating histograms...' print 'Creating histograms...'
M = colHist(image, bins, 'rgb') M = colHist(image, bins, model)
I = colHist(environment, bins, 'rgb') I = colHist(environment, bins, model)
# Create ratio histogram # Create ratio histogram
R = zeros(I.shape) R = zeros(bins)
for c in domainIterator(R, 3): for c in domainIterator(R, 3):
if (I[c] != 0).all(): if (I[c] != 0).all():
...@@ -49,8 +46,8 @@ def hbp(image, environment, bins, model, radius): ...@@ -49,8 +46,8 @@ def hbp(image, environment, bins, model, radius):
# Create back projection # Create back projection
print 'Creating back projection...' print 'Creating back projection...'
b = zeros(environment.shape) b = zeros(environment.shape[:2])
use = environment * bins use = environment.astype(float) * bins
if model == 'rgb': if model == 'rgb':
use /= 255 use /= 255
...@@ -63,9 +60,10 @@ def hbp(image, environment, bins, model, radius): ...@@ -63,9 +60,10 @@ def hbp(image, environment, bins, model, radius):
# Create convolution to create a peak value # Create convolution to create a peak value
print 'Creating convolution...' print 'Creating convolution...'
return convolution(b, radius) return b
#return convolution(b, radius)
def loc(x, y): def loc(image, color):
pass pass
def find_image(image, environment, bins, model, radius): def find_image(image, environment, bins, model, radius):
...@@ -79,9 +77,18 @@ if __name__ == '__main__': ...@@ -79,9 +77,18 @@ if __name__ == '__main__':
waldo = imread('waldo.tiff') waldo = imread('waldo.tiff')
env = imread('waldo_env.tiff') env = imread('waldo_env.tiff')
p = hbp(waldo, env, [8] * 3, 'rgb', 5) b = hbp(waldo, env, [8] * 3, 'rgb', 8)
imshow(b, cmap='gray', origin='lower')
show()
import sys
sys.exit(0)
print 'Mapping projection over original image...'
result = env.copy()
for p in domainIterator(result):
result[p] *= b[p]
print 'done' print 'done'
imshow(p, cmap='gray') 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, [8] * 3, 'rgb')
......
...@@ -4,7 +4,8 @@ from matplotlib.pyplot import imread ...@@ -4,7 +4,8 @@ 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(map(lambda x: round(x - 1), color))
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."""
...@@ -20,11 +21,13 @@ def domainIterator(image, dim=2): ...@@ -20,11 +21,13 @@ 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(tuple(bins)) h = zeros(bins)
use = image.astype(float) * bins use = image.astype(float) * bins
if model == 'rgb': if model == 'rgb':
use /= 255 use /= 255
elif model == 'rgba':
pass
elif model == 'hsv': elif model == 'hsv':
# TODO: implement HSV color model # TODO: implement HSV color model
pass pass
......
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