def unwrap_ellipse(image, params, rad_range, num_points=None, spline_order=3):
""" Unwraps an circular or ellipse-shaped feature into elliptic coordinates.
Transforms an image in (y, x) space to (theta, r) space, using elliptic
coordinates. The theta coordinate is tangential to the ellipse, the r
coordinate is normal to the ellipse. r=0 at the ellipse: inside the ellipse,
r < 0.
Parameters
----------
image : ndarray, 2d
params : (yr, xr, yc, xc)
rad_range : tuple
A tuple defining the range of r to interpolate.
num_points : number, optional
The number of ``theta`` values. By default, this equals the
ellipse circumference: approx. every pixel there is an interpolation.
spline_order : number, optional
The order of the spline interpolation. Default 3.
Returns
-------
intensity : the interpolated image in (theta, r) space
pos : the (y, x) positions of the ellipse grid
normal : the (y, x) unit vectors normal to the ellipse grid
"""
yr, xr, yc, xc = params
# compute the r coordinates
steps = np.arange(rad_range[0], rad_range[1] + 1, 1)
# compute the (y, x) positions and unit normals of the ellipse
pos, normal = ellipse_grid((yr, xr), (yc, xc), n=num_points, spacing=1)
# calculate all the (y, x) coordinates on which the image interpolated.
# this is a 3D array of shape [n_theta, n_r, 2], with 2 being y and x.
coords = normal[:, :, np.newaxis] * steps[np.newaxis, np.newaxis, :] + \
pos[:, :, np.newaxis]
# interpolate the image on computed coordinates
intensity = map_coordinates(image, coords, order=spline_order,
output=np.float)
return intensity, pos, normal
评论列表
文章目录