Created
August 11, 2011 22:27
-
-
Save alexalemi/1140971 to your computer and use it in GitHub Desktop.
Alternative Moebius Transformation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" An alternative way to do the moebius transformation, | |
using scipy.ndimage.geometric_transform, | |
that interpolates the points for a smoother transformation """ | |
from pylab import * | |
from numpy import * | |
zp=[157+148j, 78+149j, 54+143j]; # (zs) the complex point zp[i] | |
wa=[147+143j, 78+140j, 54+143j]; # (ws) will be in wa[i] | |
# transformation parameters | |
a = linalg.det([[zp[0]*wa[0], wa[0], 1], | |
[zp[1]*wa[1], wa[1], 1], | |
[zp[2]*wa[2], wa[2], 1]]); | |
b = linalg.det([[zp[0]*wa[0], wa[0], wa[0]], | |
[zp[1]*wa[1], wa[1], wa[1]], | |
[zp[2]*wa[2], wa[2], wa[2]]]); | |
c = linalg.det([[zp[0], wa[0], 1], | |
[zp[1], wa[1], 1], | |
[zp[2], wa[2], 1]]); | |
d = linalg.det([[zp[0]*wa[0], zp[0], 1], | |
[zp[1]*wa[1], zp[1], 1], | |
[zp[2]*wa[2], zp[2], 1]]); | |
img = fliplr(imread('mond.jpg')) # load an image | |
from scipy.ndimage import geometric_transform | |
def shift_func(coords): | |
""" Define the moebius transformation, though backwards """ | |
#turn the first two coordinates into an imaginary number | |
z = coords[0] + 1j*coords[1] | |
w = (d*z-b)/(-c*z+a) #the inverse mobius transform | |
return real(w),imag(w),coords[2] #take the color along for the ride | |
newimg = geometric_transform(img,shift_func,cval=255) #make the outside white | |
figure(2) | |
subplot(121) | |
title('Original Mondrian') | |
imshow(img) | |
subplot(122) | |
title('Mondrian after the transformation') | |
imshow(newimg) | |
show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by the Glowing Python post here: http://glowingpython.blogspot.com/2011/08/applying-moebius-transformation-to.html
This version uses scipy.ndimage.geometric_transform to interpolate the in-between points.
Result can be seen here: http://i.imgur.com/qz7P0.png