improc: WIP on ass2.

parent 1a0c8b6c
from interpolation import in_image, pv
from numpy import array, zeros
from numpy.linalg import lstsq
# url: http://www.leptonica.com/affine.html
def affine_transform(image, p1, p2, p3, width, height):
"""Warps the parallelogram defined by the three points (x1,y1), (x2,y2) and
(x3,y3) onto a new image of size MxN."""
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
M = array([[x1, y1, 1, 0, 0, 0],
[0, 0, 0, x1, y1, 1],
[x2, y2, 1, 0, 0, 0],
[0, 0, 0, x2, y2, 1],
[x3, y3, 1, 0, 0, 0],
[0, 0, 0, x3, y3, 1]])
q = array([[x1],
[y1],
[x2],
[y2],
[x3],
[y3]])
# Calculate transform matrix
p = lstsq(M, q)[0]
T = p.reshape(2, 3).T
# Construct the transformed image
result = zeros((width, height))
for y in xrange(height):
for x in xrange(width):
# TODO: multiplication using '*' or 'dot()' ?
# XXX: where is |0 0 1| in T, since p.reshape(2, 3) ?
# |x'| |a b c| |x|
# |y'| = |d e f| |y|
# |1 | |0 0 1| |1|
orig_pos = array([x, y, 1]).reshape(3, 1) * T
#print x,y, tmp
#result[y][x] =
return result
if __name__ == '__main__':
from pylab import imread, imshow
image = imread('cameraman.png')
half_width = image.shape[0] / 2
half_height = image.shape[1] / 2
M, N = image.shape
# TODO: validate MxN -> width x height
result = affine_transform(image, (0., 0.), (0., half_height),
(half_width, half_height), M, N)
imshow(result)
...@@ -2,11 +2,11 @@ from math import floor, ceil ...@@ -2,11 +2,11 @@ from math import floor, ceil
bgcolor = 0 bgcolor = 0
def inImage(image, x, y): def in_image(image, x, y):
return x >= 0 and y >= 0 and x < image.shape[0] and y < image.shape[1] return x >= 0 and y >= 0 and x < image.shape[0] and y < image.shape[1]
def pV(image, x, y, method): def pv(image, x, y, method):
if not inImage(image, x, y): if not in_image(image, x, y):
return bgcolor return bgcolor
if method == 'nearest': if method == 'nearest':
...@@ -28,10 +28,10 @@ def pV(image, x, y, method): ...@@ -28,10 +28,10 @@ def pV(image, x, y, method):
if __name__ == '__main__': if __name__ == '__main__':
from pylab import linspace, show, imread, plot, array from pylab import linspace, show, imread, plot, array
"""Profile of an image along line in n points."""
def profile(image, x0, y0, x1, y1, n, method): def profile(image, x0, y0, x1, y1, n, method):
"""Profile of an image along line in n points."""
dataset = zip(linspace(x0, x1, n), linspace(y0, y1, n)) dataset = zip(linspace(x0, x1, n), linspace(y0, y1, n))
return array([pV(image, x, y, method) for x, y in dataset]) return array([pv(image, x, y, method) for x, y in dataset])
a = imread('cameraman.png') a = imread('cameraman.png')
for method in ['nearest', 'linear']: for method in ['nearest', 'linear']:
......
...@@ -40,7 +40,7 @@ static void init_pluck(double pluck, double length, double dx) { ...@@ -40,7 +40,7 @@ static void init_pluck(double pluck, double length, double dx) {
} }
for( ; i < STRING_PARTS; i++ ) { for( ; i < STRING_PARTS; i++ ) {
SET_Y(i, 0, ((STRING_PARTS - i) * dx) / (STRING_PARTS - ipos)); SET_Y(i, 0, ((length - i) * dx) / (length - ipos));
} }
} }
...@@ -90,13 +90,13 @@ int main(int argc, char **argv) { ...@@ -90,13 +90,13 @@ int main(int argc, char **argv) {
// Initialise time indices, iterations and string length. // Initialise time indices, iterations and string length.
int t_prev = 0, t_cur = 1, t_next = 2, int t_prev = 0, t_cur = 1, t_next = 2,
iterations = atoi(argv[2]), iterations = atoi(argv[2]);
length = atoi(argv[3]);
double dx = atof(argv[4]), double length = atof(argv[3]),
dx = atof(argv[4]),
tau = atof(argv[5]); tau = atof(argv[5]);
STRING_PARTS = length / dx; STRING_PARTS = ceil(length / dx) + 1;
if( !(Y = (double *) malloc(3 * STRING_PARTS * sizeof(double))) ) if( !(Y = (double *) malloc(3 * STRING_PARTS * sizeof(double))) )
return 2; return 2;
...@@ -114,11 +114,13 @@ int main(int argc, char **argv) { ...@@ -114,11 +114,13 @@ int main(int argc, char **argv) {
// The string is connected at both ends. // The string is connected at both ends.
connect_string_ends(); connect_string_ends();
plot(t_prev);
for( int i = 0; i < iterations; i++ ) { for( int i = 0; i < iterations; i++ ) {
iterate(tau, t_prev, t_cur, t_next); iterate(tau, t_prev, t_cur, t_next);
SWAP(t_cur, t_next); SWAP(t_cur, t_next);
SWAP(t_prev, t_next); SWAP(t_prev, t_next);
plot(t_cur); //plot(t_cur);
} }
return 0; return 0;
......
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