def generate_label_image(max_proj, nuclear_proj, segmentation, centroids_tuple, area_scale=1): border_image = scale_to_uint8( mark_boundaries(scale_to_uint8(max_proj), segmentation, color=(1, 0, 0))) border_image[:, :, 2] = scale_to_uint8(nuclear_proj) print(border_image.shape) annotated = Image.fromarray(border_image) draw = ImageDraw.Draw(annotated) font_size = 20 font = ImageFont.truetype(FONT_PATH, font_size) rids = set(np.unique(segmentation)) - set([0]) for n, rid in enumerate(rids): region_positions = set(zip(*np.where(segmentation == rid))) r, c = list(map(int, np.array(list(region_positions)).mean(axis=0))) draw.text((c - 10, r - 10), str(rid), fill=(255, 255, 255), font=font) annotated_as_array = np.array(annotated) return annotated_as_array.view(dbiImage)
def visualise_counts(maxproj, scaled_cell_regions, centroids): maxproj = scale_to_uint8(maxproj) centroids_by_cell = { idx: {tuple(p) for p in scaled_cell_regions.rprops[idx].coords} & set(centroids) for idx in scaled_cell_regions.labels } counts_by_cell = { idx: len(centroids) for idx, centroids in centroids_by_cell.items() } boundary_image = scale_to_uint8( skimage.segmentation.mark_boundaries(maxproj, scaled_cell_regions) ) boundary_pilimage = pilImage.fromarray(boundary_image) draw = ImageDraw.ImageDraw(boundary_pilimage) font = ImageFont.truetype("Microsoft Sans Serif.ttf", size=16) for idx, count in counts_by_cell.items(): r, c = map(int, scaled_cell_regions.rprops[idx].centroid) draw.text((c-20, r-12), f"[{idx}] {count}", font=font) return boundary_pilimage
def visualise_image_and_mask(im, mask): im_numpy = scale_to_uint8(np.transpose(im.numpy(), (1, 2, 0))) mask_rgb = np.dstack(3 * [scale_to_uint8(mask.numpy().squeeze())]) merged = 0.5 * im_numpy + 0.5 * mask_rgb return merged.view(dbiImage)
def force_to_2d_rgb(im): if len(im.shape) == 2: return scale_to_uint8(np.dstack(3 * [im])) rdim, cdim, zdim = im.shape if zdim == 3: return scale_to_uint8(im) raise ValueError("Can't handle that image type")
def visualise_image_and_mask(im, mask, mask_weight=0.5): im_numpy = scale_to_uint8(im) mask_rgb = np.dstack(3 * [scale_to_uint8(mask)]) im_weight = 1 - mask_weight merged = im_weight * im_numpy + mask_weight * mask_rgb return merged.view(dbiImage)
def thresh_and_merge(sms, z): measure_thresh = 0 wall_thresh = 3 measure_thresh_plane = scale_to_uint8(sms.measure_stack[:,:,z] > measure_thresh) wall_thresh_plane = scale_to_uint8(sms.wall_stack[:,:,z] > wall_thresh) blank_plane = np.zeros(sms.wall_stack[:,:,z].shape, dtype=np.uint8) return np.dstack((wall_thresh_plane, measure_thresh_plane, blank_plane)).view(dbiImage)
def visualise_segmentation_and_dn_measure(sms, z): seg_plane = get_borderless_seg_plane(sms, z) measure_plane = sms.measure_stack[:, :, z] dn2d = skimage.restoration.denoise_tv_chambolle(measure_plane) return (0.7 * np.dstack(3 * [scale_to_uint8(dn2d)]) + 0.3 * seg_plane.pretty_color_image).view(dbiImage)
def scale_and_convert_to_rgb(im): scaled = scale_to_uint8(im) if len(im.shape) == 2: return np.dstack(3 * [scaled]) return im
def visualise_marker_positions(dataitem): r = dataitem.bad_mask g = dataitem.good_mask b = dataitem.nuc_mask import numpy as np from dtoolbioimage import scale_to_uint8 merged = np.dstack([r, g, b]) scaled = scale_to_uint8(scale_segmentation(merged, dataitem.maxproj)) maxproj_rgb = scale_to_uint8(np.dstack(3 * [dataitem.maxproj])) scaled.view(dbiImage).save("scaled.png") v = 0.5 * scaled + 0.5 * maxproj_rgb v.view(dbiImage).save("v.png") visd = np.array(vis).view(dbiImage) (0.5 * visd + 0.5 * scaled).view(dbiImage).save("svis.png")
def extract_nuclei(annotated_im): ar1 = annotated_im[:, :, 1] ar2 = np.maximum(annotated_im[:, :, 0], annotated_im[:, :, 2]) result = clipped_image_difference_uint8(ar1, ar2) return scale_to_uint8(result > 30)
def file_position_image_r(wall_stack, file_coords): r = int(file_coords[0].mean()) file_mask_stack = scale_to_uint8(wall_stack.copy()) file_mask_stack[file_coords] = 255 return file_mask_stack[r, :, :].T.view(Image)
def get_masked_venus_stack(image_ds_uri, root_name): venus_stack = get_stack_by_name(image_ds_uri, root_name) wall_stack = get_stack_by_name(image_ds_uri, root_name, channel=1) base_mask = dilation(scale_to_uint8(wall_stack) > 100) venus_stack[np.where(base_mask)] = 0 return venus_stack
def visualise_masks(im, mask, pred_mask, strings): im_numpy = np.transpose(im.numpy(), (1, 2, 0)) mask_uint8 = scale_to_uint8(mask.numpy()) pred_mask_uint8 = scale_to_uint8(pred_mask) rdim, cdim = pred_mask.shape mask_vis = np.zeros((rdim, cdim, 3), dtype=np.uint8) mask_vis[:, :, 0] = pred_mask_uint8 mask_vis[:, :, 1] = mask_uint8 merged = 0.5 * mask_vis + 0.5 * scale_to_uint8(im_numpy) pilim = annotate_with_strings(merged, strings) return pilim
def find_probe_locations_3d(stack, thresh=100): selem = skimage.morphology.ball(1) th_stack = skimage.morphology.white_tophat(stack, selem) scaled_stack = scale_to_uint8(th_stack) labelled = skimage.measure.label(scaled_stack > thresh) rprops = skimage.measure.regionprops(labelled) centroids_int = [list(map(int, r.centroid)) for r in rprops] return centroids_int
def visualise_probe_locations(max_proj, centroids): canvas = np.dstack(3 * [scale_to_uint8(max_proj)]) for r, c in centroids: rr, cc = circle_perimeter(r, c, 3) try: canvas[rr, cc] = 255, 255, 0 except IndexError: pass return canvas.view(Image)
def generate_annotated_image(max_proj, segmentation, centroids_tuple, area_scale=1): border_image = scale_to_uint8( mark_boundaries(scale_to_uint8(max_proj), segmentation, color=(1, 0, 0))) annotated = Image.fromarray(border_image) draw = ImageDraw.Draw(annotated) font_size = 14 font = ImageFont.truetype(FONT_PATH, font_size) rids = set(np.unique(segmentation)) - set([0]) for rid in rids: region_positions = set(zip(*np.where(segmentation == rid))) region_centroids = region_positions & centroids_tuple n_probes = len(region_centroids) r, c = list(map(int, np.array(list(region_positions)).mean(axis=0))) area = len(region_positions) area_microns = int(area * area_scale) area_label = str(area_microns) + 'µm' area_label_offset = int(0.5 * font_size * len(area_label) / 2) r_offset = font_size / 2 draw.text((c - 10, r - r_offset), str(n_probes), fill=(255, 255, 0), font=font) draw.text((c - area_label_offset, r + r_offset), area_label, fill=(255, 255, 0), font=font) annotated_as_array = np.array(annotated) return annotated_as_array.view(dbiImage)
def diagnostics(dataitem, spec, config, params): import os import numpy as np from fishtools.segment import nuc_cell_mask_from_fishimage import skimage.measure from dtoolbioimage import scale_to_uint8 ncm = nuc_cell_mask_from_fishimage(dataitem.fishimage, params) ncm.view(dbiImage).save("ncm.png") template = "{expid}-{expid}.png" # template = "{expid}-good.png" fname = template.format(**spec) fpath = os.path.join(config.annotation_dirpath, fname) im = dbiImage.from_file(fpath) # template = "{expid}-{expid}.png" template = "{expid}-good.png" fname = template.format(**spec) fpath = os.path.join(config.annotation_dirpath, fname) imgood = dbiImage.from_file(fpath) im.save("floop.png") print(im[0, 0, :]) print(im[480, 360, :]) regionim = (im[:, :, 3] == 255) r = skimage.measure.regionprops(skimage.measure.label(regionim))[0] rmin, cmin, rmax, cmax = r.bbox sliceme = np.s_[rmin:rmax, cmin:cmax] imgood[sliceme].save("SLIGAES.png") dataitem.scaled_markers.view(dbiImage).save("scaled_markers.png") rdim, cdim = dataitem.maxproj.shape canvas = np.dstack(3 * [scale_to_uint8(dataitem.maxproj)]) canvas[np.where(dataitem.scaled_markers)] = 0, 255, 0 canvas.view(dbiImage).save("canvas.png")
def _repr_png_(self): b = io.BytesIO() scaled = scale_to_uint8(self) imageio.imsave(b, scaled, 'PNG', compress_level=0) return b.getvalue()
def get_wall_mask(image_ds_uri, root_name): wall_stack = get_stack_by_name(image_ds_uri, root_name, channel=1) mask = erosion(scale_to_uint8(wall_stack) < 100) return mask