On Fri, Feb 10, 2012 at 9:26 AM, Hugo Gagnon
<[email protected]> wrote:
> Hello,
>
> Say I have four corner points a = (X0, Y0), b = (X1, Y1), c = (X2, Y2)
> and d = (X3, Y3):
>
> a----------b
>  \        /
>  \      /
>   c----d
>
> Is there a function like meshgrid that would return me a grid of points
> linearly interpolating those four corner points?

It depends on what you mean by "linearly interpolating".  For example,
you can construct a regular grid and simply discard points outside the
trapesium, or assume that the trapesium is a "warped perspective" on a
regular grid.  For the latter case, you can construct a grid like
this:

http://mentat.za.net/refer/np_warped_grid.png

(code attached)

Stéfan
import numpy as np
import matplotlib.pyplot as plt

rx, ry = np.array([[0, 0],
                   [1, 0],
                   [0, 1],
                   [1, 1]]).T

tx, ty = np.array([[7, 10],
                   [20, 10],
                   [4, 20],
                   [25, 20]]).T

N = len(rx)
U = np.zeros((2 * N, 8))

U[:N, 0] = rx
U[:N, 1] = ry
U[:N, 2] = 1
U[:N, 6] = -rx * tx
U[:N, 7] = -ry * tx

U[N:, 3] = rx
U[N:, 4] = ry
U[N:, 5] = 1
U[N:, 6] = -rx * ty
U[N:, 7] = -ry * ty

b = np.concatenate((tx, ty))[:, np.newaxis]

M, res, rank, s = np.linalg.lstsq(U, b)
M = np.append(M, 1).reshape((3, 3))

y, x = np.mgrid[:1:10j, :1:10j]
I = np.ones(x.size)
square_grid = np.column_stack((x.ravel(), y.ravel(), I))
warp_grid = M.dot(square_grid.T).T
warp_grid /= warp_grid[:, 2][:, None]

plt.scatter(tx, ty, c='b', s=50)
plt.scatter(warp_grid[:, 0], warp_grid[:, 1], c='r', marker='x')
plt.show()

_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to