Ejemplo n.º 1
0
def get_worm_frame_image(timepoint, downscale=1, image_size=(960, 512)):
    bf = preprocess_image(timepoint, downscale)
    annotations = timepoint.annotations
    center_tck, width_tck = annotations['pose']
    image_shape = (image_size[0] / downscale, image_size[1] / downscale)
    #deal with downscaling
    new_center_tck = (center_tck[0], center_tck[1] / downscale, center_tck[2])
    new_width_tck = (width_tck[0], width_tck[1] / downscale, width_tck[2])
    avg_widths = (AVG_WIDTHS_TCK[0], AVG_WIDTHS_TCK[1] / downscale,
                  AVG_WIDTHS_TCK[2])

    reflect = False
    """if 'keypoints' in annotations and 'vulva' in annotations['keypoints']:
                    x, y = annotations['keypoints']['vulva']
                    reflect = y < 0"""

    image_width, image_height = image_shape
    worm_frame = worm_spline.to_worm_frame(bf,
                                           new_center_tck,
                                           new_width_tck,
                                           standard_width=avg_widths,
                                           zoom=1,
                                           order=1,
                                           sample_distance=image_height // 2,
                                           standard_length=image_width,
                                           reflect_centerline=reflect)
    mask = worm_spline.worm_frame_mask(avg_widths, worm_frame.shape)
    worm_frame[mask == 0] = 0
    return worm_frame
Ejemplo n.º 2
0
def output_aligned_image(image,
                         center_tck,
                         width_tck,
                         keypoints,
                         output_file,
                         t_out=[0.07, 0.15, 0.5, 0.95]):
    """ Make warped pictures and align the keypoints
    """
    #get the alignments for longitudinal_warp_spline
    t_in = calculate_keypoints(keypoints, center_tck)
    aligned_center, aligned_width = worm_spline.longitudinal_warp_spline(
        t_in, t_out, center_tck, width_tck=width_tck)

    #output the image
    warps = warps = worm_spline.to_worm_frame(image,
                                              aligned_center,
                                              width_tck=aligned_width,
                                              width_margin=0,
                                              standard_width=aligned_width)
    mask = worm_spline.worm_frame_mask(aligned_width, warps.shape)

    #change warps to an 8-bit image
    bit_warp = colorize.scale(warps).astype('uint8')
    #make an rgba image, so that the worm mask is applied
    rgba = np.dstack([bit_warp, bit_warp, bit_warp, mask])
    #save the image
    freeimage.write(rgba, output_file)
Ejemplo n.º 3
0
    def save_annotations(self):
        """Save the pose annotations as pickle files into the parent directory.
        A pickle file is created for each page in the flipbook with the name of the first image in the
        flipbook_page list as the base for the pickle file name.
        """
        for fp in self.ris_widget.flipbook_pages:
            if len(fp) == 0:
                # skip empty flipbook pages
                continue
            annotations = getattr(fp, 'annotations', {})
            pose = annotations.get('pose', (None, None))
            if pose is not None:
                center_tck, width_tck = pose
                if center_tck is not None:
                    path = pathlib.Path(fp[0].name)
                    with path.with_suffix('.pickle').open('wb') as f:
                        pickle.dump(dict(pose=pose), f)

                    # warp and save images from all flipbook pages
                    for lab_frame in fp:
                        lab_frame_image = lab_frame.data
                        path = pathlib.Path(lab_frame.name)
                        warp = worm_spline.to_worm_frame(lab_frame_image, center_tck, width_tck)
                        warp_save_path = path.parent / (path.stem + '-straight.png')
                        freeimage.write(warp, warp_save_path)

                        # If the widths are drawn, then create a mask that allows the user to make an alpha channel later.
                        # We create one mask for each flipbook page, in case the images were saved in different places.
                        # If we wind up redundantly writing the same mask a few times, so be it.
                        if width_tck is not None:
                            mask = worm_spline.worm_frame_mask(width_tck, warp.shape)
                            mask_save_path = path.parent / (path.stem + '-mask.png')
                            freeimage.write(mask, mask_save_path)
Ejemplo n.º 4
0
def generate_worm_masks(lab_frame_image, center_tck, width_tck):
    #make a lab frame mask
    lab_mask = worm_spline.lab_frame_mask(center_tck, width_tck, lab_frame_image.shape)
    lab_frame_mask = lab_mask>0

    #get worm_frame image/mask
    worm_frame_image = worm_spline.to_worm_frame(lab_frame_image, center_tck, width_tck=width_tck)
    worm_mask = worm_spline.worm_frame_mask(width_tck, worm_frame_image.shape)
    worm_frame_mask = worm_mask>0

    return (lab_frame_mask, worm_frame_image, worm_frame_mask)
Ejemplo n.º 5
0
 def worm_frame_image(self, i, image_shape):
     bf = self.normalized_bf_image(i)
     annotations = self.timepoint_list.timepoint_annotations(i)
     center_tck, width_tck = annotations['pose']
     reflect = False
     if 'keypoints' in annotations and 'vulva' in annotations['keypoints']:
         x, y = annotations['keypoints']['vulva']
         reflect = y > 0
     reflect = False
     image_width, image_height = image_shape
     worm_frame = worm_spline.to_worm_frame(bf, center_tck, width_tck,
         sample_distance=image_height//2, standard_length=image_width, reflect_centerline=reflect)
     mask = worm_spline.worm_frame_mask(width_tck, worm_frame.shape)
     worm_frame[mask == 0] = 0
     return worm_frame
Ejemplo n.º 6
0
def lab_to_lab(lab_frame_image, center_tck, width_tck):
    """Generate an image of the full transform (lab -> worm -> lab).
    This is supposed to help us prove that warping doesn't really do anything
    to the resolution of the image
    """
    lab_mask = worm_spline.lab_frame_mask(center_tck, width_tck, lab_frame_image.shape)
    lab_frame_mask = lab_mask>0

    #go lab->worm->lab
    worm_frame_image = worm_spline.to_worm_frame(lab_frame_image, center_tck, width_tck=width_tck)
    worm_to_lab_image = worm_spline.to_lab_frame(worm_frame_image, lab_frame_image.shape, center_tck, width_tck)

    worm_mask = worm_spline.lab_frame_mask(center_tck, width_tck, worm_to_lab_image.shape)
    worm_frame_mask = worm_mask>0

    return (lab_frame_mask, worm_to_lab_image, worm_frame_mask)
def standardized_worm_frame_image(lab_frame_image, center_tck, width_tck, pixel_height, 
        pixel_width, useMask=False, average_widths_tck=AVG_WIDTHS_TCK):
    WORM_WIDTH= 64
    WORM_PAD = 38

    worm_width_factor = (pixel_height - WORM_PAD) / WORM_WIDTH
    new_avg_width_tck = (AVG_WIDTHS_TCK[0], AVG_WIDTHS_TCK[1] * worm_width_factor, AVG_WIDTHS_TCK[2])
    worm_frame_image = worm_spline.to_worm_frame(lab_frame_image, center_tck, width_tck, 
                                standard_length=pixel_width, standard_width=new_avg_width_tck)
    
    if useMask:
        mask = worm_spline.worm_frame_mask(new_avg_width_tck, worm_frame_image.shape)
        worm_frame_image[mask < 10] = 0
        return worm_frame_image

    else:
        return worm_frame_image
Ejemplo n.º 8
0
def standardized_worm_frame_image(lab_frame_image,
                                  center_tck,
                                  width_tck,
                                  pixel_height,
                                  pixel_width,
                                  average_widths_tck=AVG_WIDTHS_TCK):
    WORM_WIDTH = 64
    WORM_PAD = 38

    worm_width_factor = (pixel_height - WORM_PAD) / WORM_WIDTH
    new_avg_width_tck = (AVG_WIDTHS_TCK[0],
                         AVG_WIDTHS_TCK[1] * worm_width_factor,
                         AVG_WIDTHS_TCK[2])
    return worm_spline.to_worm_frame(lab_frame_image,
                                     center_tck,
                                     width_tck,
                                     standard_length=pixel_width,
                                     standard_width=new_avg_width_tck)
Ejemplo n.º 9
0
 def __call__(self, timepoint):
     bf = normalized_bf_image(timepoint)
     annotations = timepoint.annotations
     center_tck, width_tck = annotations['pose']
     reflect = False
     if 'keypoints' in annotations and 'vulva' in annotations['keypoints']:
         x, y = annotations['keypoints']['vulva']
         reflect = y < 0
     image_width, image_height = self.image_shape
     worm_frame = worm_spline.to_worm_frame(bf,
                                            center_tck,
                                            width_tck,
                                            sample_distance=image_height //
                                            2,
                                            standard_length=image_width,
                                            reflect_centerline=reflect)
     mask = worm_spline.worm_frame_mask(width_tck, worm_frame.shape)
     worm_frame[mask == 0] = 0
     return worm_frame
Ejemplo n.º 10
0
    def worm_frame_image(self, i):
        downscale = self.downscale
        bf = self.preprocess_image(i)
        annotations = self.timepoint_list[i].annotations
        center_tck, width_tck = annotations['pose']
        image_size = self.image_size

        image_shape = (image_size[0]/downscale, image_size[1]/downscale)
        
        new_center_tck = (center_tck[0], center_tck[1]/downscale, center_tck[2])
        new_width_tck = (width_tck[0], width_tck[1]/downscale, width_tck[2])
        avg_widths = (self.AVG_WIDTHS_TCK[0], self.AVG_WIDTHS_TCK[1]/downscale, self.AVG_WIDTHS_TCK[2])
        
        reflect = False

        image_width, image_height = image_shape
        worm_frame = worm_spline.to_worm_frame(bf, new_center_tck, new_width_tck,
            standard_width=avg_widths, zoom=1, order=1, sample_distance=image_height//2, standard_length=image_width, reflect_centerline=reflect)
        mask = worm_spline.worm_frame_mask(new_width_tck, worm_frame.shape)
        worm_frame[mask == 0] = 0
        return worm_frame