Commit 9fd61674 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ImProc ass2: Finished assignment.

parent 98fbfd2e
from interpolation import pv from interpolation import pv
from pylab import array, matrix, zeros, show, dot, lstsq, inv, svd, subplot, \ from numpy import array, matrix, zeros, dot
plot, figure from numpy.linalg.linalg import lstsq, inv, svd
# url: http://www.leptonica.com/affine.html # url: http://www.leptonica.com/affine.html
...@@ -37,7 +37,7 @@ def affine_transform(image, p1, p2, p3, width, height): ...@@ -37,7 +37,7 @@ def affine_transform(image, p1, p2, p3, width, height):
for x in xrange(width): for x in xrange(width):
for y in xrange(height): for y in xrange(height):
orig_pos = (A * array([[x, y, 1]]).T).T.tolist()[0] orig_pos = A * array([[x, y, 1]]).T
result[x][y] = pv(image, orig_pos[0], orig_pos[1], 'linear') result[x][y] = pv(image, orig_pos[0], orig_pos[1], 'linear')
return result return result
...@@ -61,7 +61,7 @@ def perspective_transform(image, p1, p2, p3, p4, width, height): ...@@ -61,7 +61,7 @@ def perspective_transform(image, p1, p2, p3, p4, width, height):
[0, 0, 0, x4, y4, 1, -height * x4, -height * y4, -height]]) [0, 0, 0, x4, y4, 1, -height * x4, -height * y4, -height]])
# Calculate transform matrix # Calculate transform matrix
A = inv(svd(M)[2].T[:,-1].reshape(3, 3)) A = inv(svd(M)[2][-1].reshape(3, 3))
# Construct the transformed image # Construct the transformed image
result = zeros((width, height, 4)) result = zeros((width, height, 4))
...@@ -69,32 +69,36 @@ def perspective_transform(image, p1, p2, p3, p4, width, height): ...@@ -69,32 +69,36 @@ def perspective_transform(image, p1, p2, p3, p4, width, height):
for x in xrange(width): for x in xrange(width):
for y in xrange(height): for y in xrange(height):
p = dot(A, array([[x], [y], [1]])) p = dot(A, array([[x], [y], [1]]))
result[x][y] = pv(image, p[0] / p[2], p[1] / p[2], 'linear') result[x][y] = pv(image, p[1] / p[2], p[0] / p[2], 'linear')
return result return result
if __name__ == '__main__': if __name__ == '__main__':
from pylab import imread, imshow from pylab import imread, imshow, plot, subplot, show, axis
#image = imread('cameraman.png')
image = imread('flyeronground.png')
# Affine transformation
subplot(221)
image = imread('cameraman.png')
half_width = image.shape[0] / 2 half_width = image.shape[0] / 2
half_height = image.shape[1] / 2 half_height = image.shape[1] / 2
M, N = image.shape[:2]
#result = affine_transform(image, (0., half_height), (half_width, 0.),
# (image.shape[0], half_height), 128, 128)
result = perspective_transform(image, (149, 590), (101, 376), (301, 209),
(393, 392), 64, 64)
figure(1)
subplot(121)
x = [0, half_width, image.shape[0], half_width, 0] x = [0, half_width, image.shape[0], half_width, 0]
y = [half_height, 0, half_height, image.shape[1], half_height] y = [half_height, 0, half_height, image.shape[1], half_height]
plot(x, y, '-') plot(x, y, 'r-')
axis([0, image.shape[0], 0, image.shape[1]])
imshow(image, cmap='gray') imshow(image, cmap='gray')
subplot(122) subplot(222)
result = affine_transform(image, (0., half_height), (half_width, 0.),
(image.shape[0], half_height), 128, 128)
imshow(result, cmap='gray') imshow(result, cmap='gray')
# Perspective transformation
subplot(223)
image = imread('flyeronground.png')
x, y = zip((147, 588), (100, 370), (300, 205), (392, 392), (147, 588))
plot(x, y, 'r-')
imshow(image)
subplot(224)
result = perspective_transform(image, (147, 588), (392, 392), (100, 370), \
(300, 205), 100, 150)
imshow(result)
show() show()
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