def test_connected_components_labeling_box_blobs(): import pyclesperanto_prototype as cle from skimage.io import imread, imsave # initialize GPU cle.select_device("GTX") print("Used GPU: " + cle.get_device().name) # load data image = imread('https://imagej.nih.gov/ij/images/blobs.gif') print("Loaded image size: " + str(image.shape)) # push image to GPU memory input = cle.push(image) print("Image size in GPU: " + str(input.shape)) # process the image inverted = cle.subtract_image_from_scalar(image, scalar=255) blurred = cle.gaussian_blur(inverted, sigma_x=1, sigma_y=1) binary = cle.threshold_otsu(blurred) labeled = cle.connected_components_labeling_box(binary) # The maxmium intensity in a label image corresponds to the number of objects num_labels = cle.maximum_of_all_pixels(labeled) # print out result print("Num objects in the image: " + str(num_labels)) assert num_labels == 63
def test_exclude_labels_on_edges_blobs(): import pyclesperanto_prototype as cle from skimage.io import imread, imsave # initialize GPU cle.select_device("GTX") print("Used GPU: " + cle.get_device().name) # load data root = Path(cle.__file__).parent filename = str(root / '..' / 'data' / 'blobs.tif') image = imread(filename) print("Loaded image size: " + str(image.shape)) # push image to GPU memory input = cle.push(image) print("Image size in GPU: " + str(input.shape)) # process the image blurred = cle.gaussian_blur(image, sigma_x=1, sigma_y=1) binary = cle.threshold_otsu(blurred) labeled = cle.connected_components_labeling_box(binary) wo_edges = cle.exclude_labels_on_edges(labeled) # The maxmium intensity in a label image corresponds to the number of objects num_labels = cle.maximum_of_all_pixels(wo_edges) # print out result print("Num objects in the image: " + str(num_labels)) assert num_labels == 45
def test_threshold_otsu_against_scikit_image(): # threshold using skimage from skimage.data import camera from skimage.filters import threshold_otsu image = camera() thresh = threshold_otsu(image) binary = image > thresh print(thresh) #from skimage import exposure #counts, bin_centers = exposure.histogram(image.ravel(), 256, source_range='image') #print(str(counts)) #print(str(bin_centers)) # threshold in GPU import pyclesperanto_prototype as cle gpu_image = cle.push(image) gpu_binary = cle.threshold_otsu(gpu_image) print(str(binary)) print(str(cle.pull(gpu_binary))) # compare import numpy as np assert(np.allclose(binary, (cle.pull(gpu_binary) > 0)))
def process_image(input: Image, sigma: float = 5) -> Labels: if input: # push image to GPU input = cle.push(input.data) # process the image blurred = cle.gaussian_blur(input, sigma_x=sigma, sigma_y=sigma) binary = cle.threshold_otsu(blurred) labels = cle.connected_components_labeling_box(binary) # pull result back output = cle.pull(labels) return output
# Open a napari viewer viewer = napari.Viewer() images = [] # add the original image channels as independent layers for i in range(0, dataset.shape[2]): viewer.add_image(dataset[:, :, i], blending='additive', name="channel" + str(i), colormap=colours[i]) images.append(dataset[:, :, i]) # image segmentation: nuclei and cells binary_image = cle.threshold_otsu(dataset[:, :, nuclei_channel]) labels_nuclei = cle.connected_components_labeling_box(binary_image) labels_cells = cle.extend_labels_with_maximum_radius(labels_nuclei, radius=50) # The game class CellCountingArcade(): """ The game allows the player to shoot bullets from the bottom of the screen which move up and if they hit a nucleus it is removed from the image data in the viewer with the surrounding cell. """ def __init__(self, images, nuclei: LabelsData, cells: LabelsData, viewer: napari.Viewer): self.images = images self.nuclei = nuclei self.cells = cells
# Raw data - Otsu threshold -> Divide by standard deviation of image. image3D = cle.imread( '/archive/MIL/marciano/20201211_CapillaryLooping/cropped/wt/1_CH00_000000.tif' ) cle.imshow(image3D, 'raw data', True) # Gamma Correction inside_gamma = 0.5 image3Dblurred1 = cle.create_like(image3D) image3Dblurred1 = cle.gamma_correction(image3D, image3Dblurred1, inside_gamma) # Otsu Threshold image3Dthresh = cle.create_like(image3Dblurred1) image3Dthresh = cle.threshold_otsu(image3Dblurred1, image3Dthresh) # Dilation, Fill Holes, Erosion # Dilation Occurs 4x. image3Ddilated1 = cle.create_like(image3Dthresh) image3Ddilated1 = cle.dilate_sphere(image3Dthresh, image3Ddilated1) image3Ddilated2 = cle.create_like(image3Ddilated1) image3Ddilated2 = cle.dilate_sphere(image3Ddilated1, image3Ddilated2) image3Ddilated3 = cle.create_like(image3Ddilated2) image3Ddilated3 = cle.dilate_sphere(image3Ddilated2, image3Ddilated3) image3Ddilated4 = cle.create_like(image3Ddilated3) image3Ddilated4 = cle.dilate_sphere(image3Ddilated3, image3Ddilated4)
# initialize GPU cle.select_device("GTX") print("Used GPU: " + cle.get_device().name) # load data image = imread('https://imagej.nih.gov/ij/images/blobs.gif') print("Loaded image size: " + str(image.shape)) # push image to GPU memory input = cle.push(image) print("Image size in GPU: " + str(input.shape)) # process the image inverted = cle.subtract_image_from_scalar(image, scalar=255) blurred = cle.gaussian_blur(inverted, sigma_x=1, sigma_y=1) binary = cle.threshold_otsu(blurred) labeled = cle.connected_components_labeling_box(binary) # The maxmium intensity in a label image corresponds to the number of objects num_labels = cle.maximum_of_all_pixels(labeled) # print out result print("Num objects in the image: " + str(num_labels)) # for debugging: print out image print(labeled) # for debugging: save image to disc imsave("result.tif", cle.pull(labeled))