def worm_image_coords_in_lab_frame(lab_image_shape, worm_image_shape, center_tck, width_tck, standard_width=None, zoom=1, reflect_centerline=False): """Produce a map in the lab frame noting the coordinates of each worm pixel in the frame of reference of an image as generated by to_worm_frame(). The output coordinates are relative to a worm frame-of-reference image, as produced by to_worm_frame(). All parameters must be the same as those passed to to_worm_frame() for the coordinate transform to be correct. In particular, if a standard_width and/or a zoom factor were used to produce the image, those values must be used here as well. Areas outside of the worm will be nan. Parameters: lab_image_shape: shape of output image in lab frame worm_image_shape: shape of worm image in which the coordinates are defined center_tck: centerline spline of the worm in the lab frame. width_tck: spline width profile of the worm. standard_width: a width spline specifying the "standardized" width profile for the output image. zoom: zoom factor. reflect_centerline: reflect worm coordinates over the centerline Returns: (x_coords, y_coords), each of shape lab_image_shape """ triangle_strip = spline_geometry.triangle_strip(center_tck, width_tck) wtck = width_tck if standard_width is None else standard_width widths = interpolate.spline_interpolate(wtck, num_points=len(triangle_strip)//2) right = worm_image_shape[1]/2 - widths*zoom # worm_image_shape[1]/2 is the position of the centerline left = worm_image_shape[1]/2 + widths*zoom # worm_image_shape[1]/2 is the position of the centerline return _worm_coords(lab_image_shape, triangle_strip, x_max=worm_image_shape[0], right=right, left=left, reflect_centerline=reflect_centerline)
def abs_worm_coords_in_lab_frame(lab_image_shape, center_tck, width_tck, reflect_centerline=False): """Produce a map of the pixel-wise worm coordinates in the lab frame. Output x-coords run from 0 to the length of the worm, and y-coords from -width (right side) to width, which varies along the worm. Areas outside of the worm will be nan. Parameters: lab_image_shape: shape of output image in lab frame center_tck: centerline spline of the worm in the lab frame. width_tck: spline width profile of the worm. reflect_centerline: reflect worm coordinates over the centerline Returns: (x_coords, y_coords), each of shape lab_image_shape """ triangle_strip = spline_geometry.triangle_strip(center_tck, width_tck) x_max = spline_geometry.arc_length(center_tck) widths = interpolate.spline_interpolate(width_tck, num_points=len(triangle_strip) // 2) return _worm_coords(lab_image_shape, triangle_strip, x_max, right=-widths, left=widths, reflect_centerline=reflect_centerline)
def rel_worm_coords_in_lab_frame(lab_image_shape, center_tck, width_tck, reflect_centerline=False): """Produce a map of the relative worm coordinates in the lab frame. Output x-coords run from 0 to 1, and y-coords from -1 (right side) to 1. Areas outside of the worm will be nan. Parameters: lab_image_shape: shape of output image in lab frame center_tck: centerline spline of the worm in the lab frame. width_tck: spline width profile of the worm. reflect_centerline: reflect worm coordinates over the centerline Returns: (x_coords, y_coords), each of shape lab_image_shape """ triangle_strip = spline_geometry.triangle_strip(center_tck, width_tck) return _worm_coords(lab_image_shape, triangle_strip, x_max=1, right=-1, left=1, reflect_centerline=reflect_centerline)
def worm_coords_lab_frame_mask(lab_image_shape, center_tck, width_tck): """Produce a boolean image mask that is pixel accurate for lab-frame worm coordinate images. NB: lab_frame_mask() uses a different algorithm to draw an (optionally antialiased) mask that is not guaranteed to be in perfect pixel alignment with the worm coordinate images generated by the [abs|rel]_worm_coords[...] functions. Use this instead if such guarantees are needed but antialiasing is not. Parameters: lab_image_shape: shape of output image in lab frame center_tck: centerline spline of the worm in the lab frame. width_tck: spline width profile of the worm. Returns: boolean mask of shape lab_image_shape """ triangle_strip = spline_geometry.triangle_strip(center_tck, width_tck) return draw.mask_triangle_strip(triangle_strip, lab_image_shape)
def worm_self_intersection_map(lab_image_shape, center_tck, width_tck): """Produce a self-intersection map in the lab frame. The returned image contains a count of the number of worm pixels occupied by each lab-frame image pixel. Positions of self-intersection will have pixel values >1 indicating that the worm mask passes over that pixel multiple times. Parameters: lab_image_shape: shape of output image in lab frame center_tck: centerline spline of the worm in the lab frame. width_tck: spline width profile of the worm. Returns: intersection_map, intersection_fraction intersection_map: image of shape lab_image_shape with count of pixel occupancies intersection_fraction: fraction of worm pixels that are self-occluded """ triangle_strip = spline_geometry.triangle_strip(center_tck, width_tck) vertex_vals = numpy.ones(len(triangle_strip)) intersection_map = draw.gouraud_triangle_strip(triangle_strip, vertex_vals, lab_image_shape, accumulate=True) intersection_fraction = (intersection_map > 1).sum() / (intersection_map > 0).sum() return intersection_map, intersection_fraction