def cell_pearsons_test(self, imagemanager, cellmanager): """Placeholder""" pcc_scores = {} for key in cellmanager.cells.keys(): cell = cellmanager.cells[key] x0, y0, x1, y1 = cell.box channel_1_cell = imagemanager.channel_1_image[x0:x1 + 1, y0:y1 + 1] channel_2_cell = imagemanager.channel_2_image[x0:x1 + 1, y0:y1 + 1] c1_overlay = mark_boundaries(img_as_int(channel_1_cell), img_as_int(cell.cell_mask), color=(1, 0, 1), outline_color=None) c2_overlay = mark_boundaries(img_as_int(channel_2_cell), img_as_int(cell.cell_mask), color=(1, 0, 1), outline_color=None) cell.cell_image = np.concatenate((c1_overlay, c2_overlay), axis=1) cell.cell_image_overlays = np.concatenate((c1_overlay, c2_overlay), axis=1) pcc_scores[key] = self.pearsons_score(channel_1_cell, channel_2_cell, cell.cell_mask) return pcc_scores
def phys_dist2boundary(self): print("######################################") print("Add Physics Param: dist_to_boundary") print("######################################") phys_df = pd.read_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-physData.csv') if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-dist2boundaryMask.tif'): dist2boundary_masks = imread(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-dist2boundaryMask.tif') # boundary_masks = boundary_masks // 255 else: boundary_masks = imread(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-boundaryMask.tif') boundary_masks = boundary_masks // 255 print("######################################") print("Generate dist2boundary_mask") print("######################################") dist2boundary_masks = get_dist2boundary_mask_batch(boundary_masks) imsave(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-dist2boundaryMask.tif', img_as_int(dist2boundary_masks)) phys_df = add_dist_to_boundary_batch_2(phys_df, dist2boundary_masks) phys_df.round(6).to_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \ '-physData.csv', index=False)
def set_image(self, params, images, background): """ creates a strip with the cell in different images images is a list of rgb images background is a grayscale image to use for the masks """ x0, y0, x1, y1 = self.box img = color.gray2rgb(np.zeros((x1 - x0 + 1, (len(images) + 4) * (y1 - y0 + 1)))) bx0 = 0 bx1 = x1 - x0 + 1 by0 = 0 by1 = y1 - y0 + 1 for im in images: img[bx0:bx1, by0:by1] = im[x0 : x1 + 1, y0 : y1 + 1] by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 perim = self.perim_mask axial = self.sept_mask cyto = self.cyto_mask img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * self.cell_mask) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * perim) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * cyto) if params.find_septum: by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * axial) self.image = img_as_int(img)
def myfilter(self, data): if self.filter == 'sobel': return util.img_as_int(filters.sobel(data)) elif self.filter == 'otsu': thresh = filters.threshold_otsu(data) return util.img_as_ubyte(data > thresh) elif self.filter == '阈值分割': thresh = self.thresholdvalue * data.max() / 100.0 return util.img_as_ubyte(data > thresh) elif self.filter == 'canny edge': temp = util.img_as_ubyte( feature.canny(data, low_threshold=30, high_threshold=40)) return temp elif self.filter == 'watershed': mask = util.img_as_ubyte(filters.gaussian_filter(data, 0.4)) markers = feature.canny(data, low_threshold=30, high_threshold=40) markers = ndi.label(markers)[0] idata = filters.rank.gradient(data, morphology.disk(1)) temp = morphology.watershed(data, markers, mask=mask) # hsv=color.convert_colorspace(temp,'L','RGB') # io.imshow(hsv) return temp elif self.filter == 'test': data = util.img_as_ubyte(filters.median(data, morphology.disk(2))) return data
def preprocess(img): """ Apply image processing functions to return a binary image """ # Apply thresholds cv2.imshow('unprocessed', img) img = filters.threshold_local(img, 3) threshold = 0.3 idx = img > img.max() * threshold idx2 = img < img.max() * threshold img[idx] = 0 img[idx2] = 255 # undistorting img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) # Crop the pictures as for raw images. img = crop(img) cv2.imshow('proced', img) struct = ndimage.generate_binary_structure(2, 3) # img = ndimage.binary_dilation(img, structure=struct) img = ndimage.binary_erosion(img, ndimage.generate_binary_structure(2, 9)) img = ndimage.binary_dilation(img, structure=struct) # cv2.imshow('proced', util.img_as_int(img)) return util.img_as_int(img)
def generate_report(self, image_manager, cells_manager, fret_manager, path=None): if path is None: path = tkFileDialog.askdirectory() path = path + os.sep + "Report Experiment" if not os.path.exists(path + os.sep + "_fret_images"): os.makedirs(path + os.sep + "_fret_images") if not os.path.exists(path + os.sep + "_donor_images"): os.makedirs(path + os.sep + "_donor_images") if not os.path.exists(path + os.sep + "_acceptor_images"): os.makedirs(path + os.sep + "_acceptor_images") if not os.path.exists(path + os.sep + "_control_images"): os.makedirs(path + os.sep + "_control_images") if not os.path.exists(path + os.sep + "_wt_images"): os.makedirs(path + os.sep + "_wt_images") if not os.path.exists(path + os.sep + "_discarded_images"): os.makedirs(path + os.sep + "_discarded_images") self.generate_report_experiment(image_manager, cells_manager, fret_manager, path) imsave(path + os.sep + "heatmap.png", img_as_int(fret_manager.fret_heatmap))
def overlay_cells(cells, image, colors): "Overlay the edges of each individual cell in the provided image" tmp = color.gray2rgb(image) for k in cells.keys(): c = cells[k] if c.selection_state == 1: col = colors[c.color_i][:3] for px in c.outline: x, y = px tmp[x, y] = col if c.sept_mask is not None: try: x0, y0, x1, y1 = c.box tmp[x0:x1, y0:y1] = mark_boundaries(tmp[x0:x1, y0:y1], img_as_int( c.sept_mask), color=col) except IndexError: c.selection_state = -1 return tmp
def overlay_cells(cells, image, colors): "Overlay the edges of each individual cell in the provided image" tmp = color.gray2rgb(image) for k in cells.keys(): c = cells[k] if c.selection_state == 1: col = colors[c.color_i][:3] for px in c.outline: x, y = px tmp[x, y] = col if c.sept_mask is not None: try: x0, y0, x1, y1 = c.box tmp[x0:x1 + 1, y0:y1 + 1] = mark_boundaries(tmp[x0:x1 + 1, y0:y1 + 1], img_as_int(c.sept_mask), color=col) except IndexError: c.selection_state = -1 return tmp
def preprocess_algae(img): # Crop the pictures as for raw images. # Apply thresholds img = filters.threshold_adaptive(img,25) threshold = 0.3 idx = img > img.max() * threshold idx2 = img < img.max() * threshold img[idx] = 255 img[idx2] = 0 return util.img_as_int(img)
def preprocess_algae_fill(img): # Crop the pictures as for raw images. # Apply thresholds img = filters.threshold_adaptive(img,25) threshold = 0.3 idx = img > img.max() * threshold idx2 = img < img.max() * threshold img[idx] = 255 img[idx2] = 0 img = ndimage.binary_erosion(img) img = ndimage.binary_closing(img) return util.img_as_int(img)
def generate_thres_mask(self): print("######################################") print("Generate thres mask") print("######################################") masks_thres = self.get_thres_mask() masks_thres_255 = np.rint(masks_thres / \ masks_thres.max() * 255).astype(np.uint8) imsave( self.settings['Output path'] + self.root_name + '-thresMask.tif', masks_thres_255) print("######################################") print("Generate dist2thresMask") print("######################################") dist_masks = img_as_int(get_dist2boundary_mask_batch(masks_thres)) imsave(self.settings['Output path'] + self.root_name + \ '-dist2thresMask.tif', dist_masks)
def mask_boundary(self): print("######################################") print("Generate mask_boundary") print("######################################") boundary_masks = self.get_boundary_mask() # Save it using 255 and 0 boundary_masks_255 = np.rint(boundary_masks / \ boundary_masks.max() * 255).astype(np.uint8) imsave(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-boundaryMask.tif', boundary_masks_255) print("######################################") print("Generate dist2boundary_mask") print("######################################") dist2boundary_masks = img_as_int(get_dist2boundary_mask_batch(boundary_masks)) imsave(self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-dist2boundaryMask.tif', dist2boundary_masks)
def generate_blob_mask(self): print("######################################") print("Generate blob mask") print("######################################") masks_blob = self.get_blob_mask() masks_blob_255 = np.rint(masks_blob / \ masks_blob.max() * 255).astype(np.uint8) imsave(self.settings['Output path'] + self.root_name + '-blobMask.tif', masks_blob_255) if self.settings['Generate dist2blobMask']: print("######################################") print("Generate dist2blob_mask") print("######################################") dist2blob_masks = img_as_int( get_dist2boundary_mask_batch(masks_blob)) imsave(self.settings['Output path'] + self.root_name + \ '-dist2blobMask.tif', dist2blob_masks)
def preprocess_foam(img): """ Apply image processing functions to return a binary image """ # Crop the pictures as for raw images. img = crop(img) # Apply thresholds block_size = 5 img = filters.threshold_local(img, block_size) threshold = 0.30 idx = img > img.max() * threshold idx2 = img < img.max() * threshold img[idx] = 0 img[idx2] = 255 # Dilatate to get a continous network # of liquid films img = ndimage.binary_dilation(img) img = ndimage.binary_dilation(img) return util.img_as_int(img)
def preprocess_foam(img): """ Apply image processing functions to return a binary image """ # adapt to greyscale # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply thresholds img = filters.threshold_local(img, 299) threshold = 0.80 idx = img > img.max() * threshold idx2 = img < img.max() * threshold img[idx] = 0 img[idx2] = 255 # Dilatate to get a continous network # of liquid films n_dilat = 1 for _ in range(n_dilat): img = ndimage.binary_dilation(img) return util.img_as_int(img)
def set_image(self, params, images): """ creates a strip with the cell in different images images is a list of rgb images background is a grayscale image to use for the masks """ x0, y0, x1, y1 = self.box img = gray2rgb( np.zeros((x1 - x0 + 1, (len(images) + 4) * (y1 - y0 + 1)))) bx0 = 0 bx1 = x1 - x0 + 1 by0 = 0 by1 = y1 - y0 + 1 for im in images: img[bx0:bx1, by0:by1] = im[x0:x1 + 1, y0:y1 + 1] by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 self.image = img_as_int(img)
def mask_53bp1_blob(self): print("######################################") print("Generate mask_53bp1_blob") print("######################################") masks_53bp1_blob = self.get_53bp1_blob_mask() # Save it using 255 and 0 masks_53bp1_blob_255 = np.rint(masks_53bp1_blob / \ masks_53bp1_blob.max() * 255).astype(np.uint8) imsave( self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-53bp1BlobMask.tif', masks_53bp1_blob_255) print("######################################") print("Generate dist253bp1blob_mask") print("######################################") dist253bp1blob_masks = img_as_int( get_dist2boundary_mask_batch(masks_53bp1_blob)) imsave( self.config.OUTPUT_PATH + self.config.ROOT_NAME + '-dist253bp1blobMask.tif', dist253bp1blob_masks)
def set_image(self, params, images, background): """ creates a strip with the cell in different images images is a list of rgb images background is a grayscale image to use for the masks """ x0, y0, x1, y1 = self.box img = color.gray2rgb( np.zeros((x1 - x0 + 1, (len(images) + 4) * (y1 - y0 + 1)))) bx0 = 0 bx1 = x1 - x0 + 1 by0 = 0 by1 = y1 - y0 + 1 for im in images: img[bx0:bx1, by0:by1] = im[x0:x1 + 1, y0:y1 + 1] by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 perim = self.perim_mask axial = self.sept_mask cyto = self.cyto_mask img[bx0:bx1, by0:by1] = color.gray2rgb( background[x0:x1 + 1, y0:y1 + 1] * self.cell_mask) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0:x1 + 1, y0:y1 + 1] * perim) by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0:x1 + 1, y0:y1 + 1] * cyto) if params.find_septum: by0 = by0 + y1 - y0 + 1 by1 = by1 + y1 - y0 + 1 img[bx0:bx1, by0:by1] = color.gray2rgb( background[x0:x1 + 1, y0:y1 + 1] * axial) self.image = img_as_int(img)
def readInImage(path): image = img_as_int(io.imread(path, as_grey=True)) #image = img_as_uint(io.imread(path, as_grey=True)) return image
def cell_pearsons_test_percentile(self, imagemanager, cellmanager, percentile): pcc_scores = {} for key in cellmanager.cells.keys(): cell = cellmanager.cells[key] x0, y0, x1, y1 = cell.box channel_1_cell = imagemanager.channel_1_image[x0:x1 + 1, y0:y1 + 1] channel_2_cell = imagemanager.channel_2_image[x0:x1 + 1, y0:y1 + 1] c1_overlay = mark_boundaries(img_as_int(channel_1_cell), img_as_int(cell.cell_mask), color=(1, 0, 1), outline_color=None) c2_overlay = mark_boundaries(img_as_int(channel_2_cell), img_as_int(cell.cell_mask), color=(1, 1, 0), outline_color=None) cell.cell_image = np.concatenate((c1_overlay, c2_overlay), axis=1) channel_1_pxs = channel_1_cell * cell.cell_mask channel_1_pxs = channel_1_pxs.flatten() channel_1_pxs = sorted(channel_1_pxs) channel_1_pxs = np.trim_zeros(channel_1_pxs)[::-1] channel_1_pxs = channel_1_pxs[:int( len(channel_1_pxs) * percentile)] channel_1_threshold = channel_1_pxs[-1] channel_1_mask = channel_1_threshold <= (channel_1_cell * cell.cell_mask) channel_2_pxs = channel_2_cell * cell.cell_mask channel_2_pxs = channel_2_pxs.flatten() channel_2_pxs = sorted(channel_2_pxs) channel_2_pxs = np.trim_zeros(channel_2_pxs)[::-1] channel_2_pxs = channel_2_pxs[:int( len(channel_2_pxs) * percentile)] channel_2_threshold = channel_2_pxs[-1] channel_2_mask = channel_2_threshold <= (channel_2_cell * cell.cell_mask) c1_overlay_1 = mark_boundaries(img_as_int(c1_overlay), img_as_int(channel_1_mask), color=(1, 1, 0), outline_color=None) c2_overlay_1 = mark_boundaries(img_as_int(c2_overlay), img_as_int(channel_1_mask), color=(1, 1, 0), outline_color=None) c1_overlay_2 = mark_boundaries(img_as_int(c1_overlay), img_as_int(channel_2_mask), color=(1, 1, 0), outline_color=None) c2_overlay_2 = mark_boundaries(img_as_int(c2_overlay), img_as_int(channel_2_mask), color=(1, 1, 0), outline_color=None) cell.cell_image_overlays = np.concatenate( (c1_overlay_1, c2_overlay_1, c1_overlay_2, c2_overlay_2), axis=1) #missing creation of mask for each channel and changes to allow the #computation of two pcc's percentile_mask = None pcc_scores[key] = (self.pearsons_score(channel_1_cell, channel_2_cell, channel_1_mask), self.pearsons_score(channel_1_cell, channel_2_cell, channel_2_mask)) self.cells_pcc = pcc_scores
def haralick_features(im, prop_names=None, distances=[2, 4, 8], angles=np.arange(8) * np.pi / 4, levels=256, symmetric=False, normed=False): """Compute Haralick texture features of a grayscale image. Parameters ---------- im : 2D np.ndarray of float or uint8. The input image. prop_names : list of strings, optional Texture properties of a gray level co-occurence matrix. By default prop_names=None, which means all properties are computed. Available texture properties include: 'contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', and 'correlation'. distances : array_like, optional List of pixel pair distance offsets, used for grey covariance matrix. angles : array_like, optional List of pixel pair angles in radians, used for grey covariance matrix. levels : int, optional The input image should contain integers in [0, levels-1], where levels indicate the number of grey-levels counted (typically 256 for an 8-bit image). This argument is required for 16-bit images or higher and is typically the maximum of the image. As the output matrix is at least levels x levels, it might be preferable to use binning of the input image rather than large values for levels. symmetric : bool, optional If True, the output matrix P[:, :, d, theta] is symmetric. This is accomplished by ignoring the order of value pairs, so both (i, j) and (j, i) are accumulated when (i, j) is encountered for a given offset. The default is False. normed : bool, optional If True, normalize each matrix P[:, :, d, theta] by dividing by the total number of accumulated co-occurrences for the given offset. The elements of the resulting matrix sum to 1. The default is False. Returns ------- fs : 1D np.ndarray of float The feature vector. names : list of string The list of feature names. References ---------- .. [1] The GLCM Tutorial Home Page, http://www.fp.ucalgary.ca/mhallbey/tutorial.htm """ if np.issubdtype(im.dtype, np.floating): im = img_as_int(im) available_prop_names = [ 'contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', 'correlation' ] if prop_names is None: prop_names = available_prop_names else: # do not allow invalid input in prop_names prop_names = [ prop for prop in prop_names if prop.lower() in map(str.lower, available_prop_names) ] glcm = greycomatrix(im, distances=distances, angles=angles, levels=levels, symmetric=symmetric, normed=normed) fs = [] names = [] for prop in prop_names: texture_properties = greycoprops(glcm, prop) for dist, theta in it.product(distances, angles): name = 'haralick-%s-distance%d-angle%d' % (prop, dist, theta) names.append(name) fs.append(texture_properties[distances.index(dist), angles.index(theta)]) return np.array(fs), names
def readInImage(path): image = img_as_int(io.imread(path, as_grey=True)) # image = img_as_uint(io.imread(path, as_grey=True)) return image
def haralick_features(im, prop_names=None, distances=[2, 4, 8], angles=np.arange(8) * np.pi/4, levels=256, symmetric=False, normed=False): """Compute Haralick texture features of a grayscale image. Parameters ---------- im : 2D np.ndarray of float or uint8. The input image. prop_names : list of strings, optional Texture properties of a gray level co-occurence matrix. By default prop_names=None, which means all properties are computed. Available texture properties include: 'contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', and 'correlation'. distances : array_like, optional List of pixel pair distance offsets, used for grey covariance matrix. angles : array_like, optional List of pixel pair angles in radians, used for grey covariance matrix. levels : int, optional The input image should contain integers in [0, levels-1], where levels indicate the number of grey-levels counted (typically 256 for an 8-bit image). This argument is required for 16-bit images or higher and is typically the maximum of the image. As the output matrix is at least levels x levels, it might be preferable to use binning of the input image rather than large values for levels. symmetric : bool, optional If True, the output matrix P[:, :, d, theta] is symmetric. This is accomplished by ignoring the order of value pairs, so both (i, j) and (j, i) are accumulated when (i, j) is encountered for a given offset. The default is False. normed : bool, optional If True, normalize each matrix P[:, :, d, theta] by dividing by the total number of accumulated co-occurrences for the given offset. The elements of the resulting matrix sum to 1. The default is False. Returns ------- fs : 1D np.ndarray of float The feature vector. names : list of string The list of feature names. References ---------- .. [1] The GLCM Tutorial Home Page, http://www.fp.ucalgary.ca/mhallbey/tutorial.htm """ if np.issubdtype(im.dtype, np.floating): im = img_as_int(im) available_prop_names = ['contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', 'correlation'] if prop_names is None: prop_names = available_prop_names else: # do not allow invalid input in prop_names prop_names = [prop for prop in prop_names if prop.lower() in map(str.lower, available_prop_names)] glcm = greycomatrix(im, distances=distances, angles=angles, levels=levels, symmetric=symmetric, normed=normed) fs = [] names = [] for prop in prop_names: texture_properties = greycoprops(glcm, prop) for dist, theta in it.product(distances, angles): name = 'haralick-%s-distance%d-angle%d' % (prop, dist, theta) names.append(name) fs.append(texture_properties[distances.index(dist), angles.index(theta)]) return np.array(fs), names