Exemple #1
0
def main():
    parser = argparse.ArgumentParser(description=_description)
    parser.add_argument('--output', '-o', metavar='stitched_elisa.tif',
                        default='stitched_elisa.tif')
    parser.add_argument('--saturationmap', '-s', metavar='saturated_elisa.tif',
                        default='saturated_elisa.tif')
    parser.add_argument(
            'original_image',
            help="Original MicroManager stack (used for metadata)")
    parser.add_argument(
            'normalized_image',
            help="Normalized image (used for image data)")
    args = parser.parse_args()
    orig_fn = args.original_image
    bgsub_fn = args.normalized_image
    print 'Reading metadata.'
    with open(orig_fn, 'rb') as f:
        metadata = tf.read_micromanager_metadata(f)
    print('Loading original image.')
    with tf.TiffFile(bgsub_fn) as bgsub_img:
        bgsub = np.concatenate([page.asarray()[np.newaxis,:] for page in bgsub_img.pages])
    canvas = stitch(metadata, bgsub)
    print('Saving image.')
    tf.imsave(args.output, canvas)
    del canvas, bgsub
    print('Computing saturation map.')
    with tf.TiffFile(orig_fn) as bgsub_img:
        orig = np.concatenate([page.asarray()[np.newaxis,:] for page in bgsub_img.pages])
    saturated = (orig == 4095)
    canvas = (stitch(metadata, saturated) > 0).astype(np.uint8)
    tf.imsave(args.saturationmap, canvas)
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(description=_description)
    parser.add_argument('--output', '-o', metavar='stitched_elisa.tif',
                        default='stitched_elisa.tif')
    parser.add_argument('--saturationmap', '-s', metavar='saturated_elisa.tif',
                        default='saturated_elisa.tif')
    parser.add_argument(
        'original_image',
        help="Original MicroManager stack (used for metadata)")
    parser.add_argument(
        'normalized_image',
        help="Normalized image (used for image data)")
    args = parser.parse_args()
    orig_fn = args.original_image
    bgsub_fn = args.normalized_image
    print 'Reading metadata.'
    with open(orig_fn, 'rb') as f:
        metadata = tf.read_micromanager_metadata(f)
    print('Loading original image.')
    with tf.TiffFile(bgsub_fn) as bgsub_img:
        bgsub = np.concatenate([page.asarray()[np.newaxis, :] for page in bgsub_img.pages])
    canvas = stitch(metadata, bgsub)
    print('Saving image.')
    tf.imsave(args.output, canvas)
    del canvas, bgsub
    print('Computing saturation map.')
    with tf.TiffFile(orig_fn) as bgsub_img:
        orig = np.concatenate([page.asarray()[np.newaxis, :] for page in bgsub_img.pages])
    saturated = (orig == 4095)
    canvas = (stitch(metadata, saturated) > 0).astype(np.uint8)
    tf.imsave(args.saturationmap, canvas)
def main():
    parser = argparse.ArgumentParser(description=_description)
    parser.add_argument('--output', '-o', metavar='normalized.tif',
                        default='normalized.tif', help='Output filename')
    parser.add_argument(
        '--subtract', action='store_true',
        help='Subtract background instead of dividing and truncating')
    parser.add_argument('infile')
    args = parser.parse_args()

    infile = args.infile
    print 'Reading image stack'
    t = tf.TiffFile(infile)
    ar = tf.stack_pages(t.pages)
    n = ar.shape[0]

    percentile = 0.01 if args.subtract else 0.05

    if os.path.exists('background.tif'):
        print 'Reading background image'
        bg = tf.imread('background.tif')
    else:
        print 'Computing background image'
        sorted_ar = ar.copy()
        sorted_ar.sort(0)
        bg = sorted_ar[int(round(percentile*n, 0))]
        print 'Saving background image'
        tf.imsave('background.tif', bg)
        del sorted_ar

    print 'Performing background normalization'
    if not args.subtract:
        ar = ar.astype(np.double)
        for i in range(n):
            ar[i] /= bg

        print 'Converting to 16-bit TIFF'
        max_normed = (4095.0 / bg.min()) - 1
        ar -= 1
        ar *= 65535
        ar /= max_normed
        ar = ar.round()
    else:
        ar = ar.astype(np.int16)
        for i in range(n):
            ar[i] -= bg
    ar[ar < 0] = 0
    ar = ar.astype(np.uint16)

    print 'Writing normalized image'
    with tf.TiffWriter(args.output) as out:
        for i in range(n):
            if (i % 100) == 0:
                print i,
                sys.stdout.flush()
            out.save(ar[i])
    print
 def crop_tiffs(self):
     self.images = []
     self.file_list = []
     for plane in list(range(self.planes)):
         time_slice = slice(plane*self.channels+self.channel2use, -1, self.channels*self.planes)
         with ScanImageTiffReader(self.tiff) as reader:
             data = reader.data()
             data = data[time_slice, : , self.xslice]
         self.images.append(data)
         tif_name = self.tiff.split('.')[0] + '_template_plane' + str(plane) + '.tif'
         self.file_list.append(tif_name)
         tifffile.imsave(tif_name, data)
def try_image_to_kitti():
    # loading data
    directory = r'D:\output-datasets\offroad-14\1'
    base_name = '000084'
    rgb_file = os.path.join(directory, '{}.jpg'.format(base_name))
    depth_file = os.path.join(directory, '{}-depth.tiff'.format(base_name))
    stencil_file = os.path.join(directory, '{}-stencil.png'.format(base_name))
    json_file = os.path.join(directory, '{}.json'.format(base_name))
    rgb = np.array(Image.open(rgb_file))
    depth = tifffile.imread(depth_file)
    stencil = np.array(Image.open(stencil_file))
    with open(json_file) as f:
        data = json.load(f)

    # creating pointcloud for original data
    csv_name = base_name + '-orig'
    vecs, _ = points_to_homo(data, depth, tresholding=False)
    vecs_p = ndc_to_view(vecs, data['proj_matrix'])
    vecs_p_world = view_to_world(vecs_p, np.array(data['view_matrix']))
    a = np.asarray(vecs_p_world[0:3, :].T)
    np.savetxt(os.path.join('kitti-format', "points-{}.csv".format(csv_name)), a, delimiter=",")

    # whole gta to kitti transformation
    rgb, depth, stencil = image_gta_to_kitti(rgb, depth, stencil, data['width'], data['height'], data['camera_fov'])
    data['proj_matrix'] = get_kitti_proj_matrix(np.array(data['proj_matrix'])).tolist()
    data['width'], data['height'] = get_kitti_img_size()

    # saving new images
    Image.fromarray(rgb).convert(mode="RGB").save(os.path.join('kitti-format', '{}.jpg'.format(base_name)))
    tifffile.imsave(os.path.join('kitti-format', '{}-depth-orig.tiff'.format(base_name)), depth)
    tifffile.imsave(os.path.join('kitti-format', '{}-depth-lzma.tiff'.format(base_name)), depth, compress='lzma')
    tifffile.imsave(os.path.join('kitti-format', '{}-depth-zip-5.tiff'.format(base_name)), depth, compress=5)
    tifffile.imsave(os.path.join('kitti-format', '{}-depth-zip-9.tiff'.format(base_name)), depth, compress=9)
    tifffile.imsave(os.path.join('kitti-format', '{}-depth-zip-zstd.tiff'.format(base_name)), depth, compress='zstd')
    Image.fromarray(stencil).save(os.path.join('kitti-format', '{}-stencil.jpg'.format(base_name)))
    with open(os.path.join('kitti-format', '{}.json'.format(base_name)), 'w+') as f:
        json.dump(data, f)

    data['view_matrix'] = np.array(data['view_matrix'])

    check_proj_matrices(depth, data)

    # creating pointcloud for kitti format data
    csv_name = base_name + '-kitti'
    vecs, _ = points_to_homo(data, depth, tresholding=False)
    vecs_p = ndc_to_view(vecs, data['proj_matrix'])
    vecs_p_world = view_to_world(vecs_p, np.array(data['view_matrix']))
    a = np.asarray(vecs_p_world[0:3, :].T)
    np.savetxt(os.path.join('kitti-format', "points-{}.csv".format(csv_name)), a, delimiter=",")
Exemple #6
0
    def crop(self, data, no_boxes=2, save_dir=None):

        # Extract bounding boxes and crop data
        raw, pred, filenames, bboxes = self.get_bounding_boxes(
            data, no_boxes=no_boxes, save_dir=save_dir)

        cropped = apply_to_batch([raw, bboxes], 'crop')  # crop the raw images
        print "Saving crop regions..."
        if save_dir:
            for crop_boxes, filename in zip(cropped, filenames):
                for i, region in enumerate(crop_boxes):
                    imsave(join(save_dir, filename + '_{}.tiff'.format(i)),
                           region)

        return cropped
Exemple #7
0
    def singlet_image(self, tmpdir_factory):
        a = np.zeros((256, 256), dtype=np.uint8)
        size = 20
        # singlet
        a[20:20+size+1, 20:20+size+1] = 255
        # doublet
        a[100:100+size+1, 100:100+size+1] = 255
        offset = 100 + size + 10
        a[100:100+size+1, offset:offset+size+1] = 255

        # duplicate into second channel
        im = np.concatenate([a[np.newaxis, :], a[np.newaxis, :]])
        filename = tmpdir_factory.mktemp('data').join('img.tif')
        tf.imsave(str(filename), im)
        return filename
def test_2x2(tmpdir):
    metadata = {
        'summary': {
            'InitialPositionList':
                [{'GridRowIndex': r, 'GridColumnIndex': c} for
                 (r, c) in zip([0, 0, 1, 1], [0, 1, 0, 1])],
            'Height': 100,
            'Width': 100,
        }
    }
    stack = np.ones(shape=(4, 100, 100), dtype=np.uint16) * 128
    canvas = stitch.stitch(metadata, stack)
    tf.imsave(str(tmpdir.join('2x2.tif')), canvas)
    delta = np.abs(canvas[11:-11, 11:-11].astype(np.int32) - 128)
    assert np.all(delta < 2)
def test_1x1(tmpdir):
    metadata = {
        'summary': {
            'InitialPositionList':
                [{'GridRowIndex': 0, 'GridColumnIndex': 0}],
            'Height': 100,
            'Width': 100,
        }
    }
    stack = np.ones(shape=(1, 100, 100), dtype=np.uint16) * 128
    canvas = stitch.stitch(metadata, stack)
    tf.imsave(str(tmpdir.join('1x1.tif')), canvas)
    # center is uniform
    assert np.all(canvas[11:-11, 11:-11] == 128)
    # vignetting is symmetric
    assert np.all(canvas[50, :] == canvas[50, ::-1])
    assert np.all(canvas[:, 50] == canvas[::-1, 50])
Exemple #10
0
def test_2x2(tmpdir):
    metadata = {
        'summary': {
            'InitialPositionList': [{
                'GridRowIndex': r,
                'GridColumnIndex': c
            } for (r, c) in zip([0, 0, 1, 1], [0, 1, 0, 1])],
            'Height':
            100,
            'Width':
            100,
        }
    }
    stack = np.ones(shape=(4, 100, 100), dtype=np.uint16) * 128
    canvas = stitch.stitch(metadata, stack)
    tf.imsave(str(tmpdir.join('2x2.tif')), canvas)
    delta = np.abs(canvas[11:-11, 11:-11].astype(np.int32) - 128)
    assert np.all(delta < 2)
Exemple #11
0
def test_1x1(tmpdir):
    metadata = {
        'summary': {
            'InitialPositionList': [{
                'GridRowIndex': 0,
                'GridColumnIndex': 0
            }],
            'Height': 100,
            'Width': 100,
        }
    }
    stack = np.ones(shape=(1, 100, 100), dtype=np.uint16) * 128
    canvas = stitch.stitch(metadata, stack)
    tf.imsave(str(tmpdir.join('1x1.tif')), canvas)
    # center is uniform
    assert np.all(canvas[11:-11, 11:-11] == 128)
    # vignetting is symmetric
    assert np.all(canvas[50, :] == canvas[50, ::-1])
    assert np.all(canvas[:, 50] == canvas[::-1, 50])
def segment_nuclei(img=None,
                   save=True,
                   adaptive=False,
                   color_image=False,
                   load_from_direc=None,
                   feature_to_load="feature_1",
                   mask_location=None,
                   threshold=0.5,
                   area_threshold=50,
                   eccentricity_threshold=1,
                   solidity_threshold=0):
    # Requires a 4 channel image (number of frames, number of features, image width, image height)
    from skimage.filters import threshold_otsu, threshold_adaptive

    if load_from_direc is None:
        img = img[:, 1, :, :]
        nuclear_masks = np.zeros(img.shape, dtype=np.float32)

    if load_from_direc is not None:
        img_files = nikon_getfiles(load_from_direc, feature_to_load)
        img_size = get_image_sizes(load_from_direc, feature_to_load)
        img = np.zeros((len(img_files), img_size[0], img_size[1]),
                       dtype=np.float32)
        nuclear_masks = np.zeros((len(img_files), img.shape[1], img.shape[2]),
                                 dtype=np.float32)
        counter = 0
        for name in img_files:
            img[counter, :, :] = get_image(os.path.join(load_from_direc, name))
            counter += 1

    for frame in xrange(img.shape[0]):
        interior = img[frame, :, :]
        if adaptive:
            block_size = 61
            nuclear_mask = np.float32(
                threshold_adaptive(interior,
                                   block_size,
                                   method='median',
                                   offset=-.075))
        else:
            nuclear_mask = np.float32(interior > threshold)
        nuc_label = label(nuclear_mask)
        max_cell_id = np.amax(nuc_label)
        for cell_id in xrange(1, max_cell_id + 1):
            img_new = nuc_label == cell_id
            img_fill = binary_fill_holes(img_new)
            nuc_label[img_fill == 1] = cell_id

        region_temp = regionprops(nuc_label)

        for region in region_temp:
            if region.area < area_threshold:
                nuclear_mask[nuc_label == region.label] = 0
            if region.eccentricity > eccentricity_threshold:
                nuclear_mask[nuc_label == region.label] = 0
            if region.solidity < solidity_threshold:
                nuclear_mask[nuc_label == region.label] = 0

        nuclear_masks[frame, :, :] = nuclear_mask

        if save:
            img_name = os.path.join(mask_location,
                                    "nuclear_mask_" + str(frame) + ".png")
            tiff.imsave(img_name, nuclear_mask)

        if color_image:
            img_name = os.path.join(mask_location,
                                    "nuclear_colorimg_" + str(frame) + ".png")

            from skimage.segmentation import find_boundaries
            import palettable
            from skimage.color import label2rgb

            seg = label(nuclear_mask)
            bound = find_boundaries(seg, background=0)

            image_label_overlay = label2rgb(
                seg,
                bg_label=0,
                bg_color=(0.8, 0.8, 0.8),
                colors=palettable.colorbrewer.sequential.YlGn_9.mpl_colors)
            image_label_overlay[bound == 1, :] = 0

            scipy.misc.imsave(img_name, np.float32(image_label_overlay))
    return nuclear_masks
def segment_cytoplasm(img=None,
                      save=True,
                      load_from_direc=None,
                      feature_to_load="feature_1",
                      color_image=False,
                      nuclear_masks=None,
                      mask_location=None,
                      smoothing=1,
                      num_iters=80):
    if load_from_direc is None:
        cytoplasm_masks = np.zeros((img.shape[0], img.shape[2], img.shape[3]),
                                   dtype=np.float32)
        img = img[:, 1, :, :]

    if load_from_direc is not None:
        img_files = nikon_getfiles(load_from_direc, feature_to_load)
        img_size = get_image_sizes(load_from_direc, feature_to_load)
        img = np.zeros((len(img_files), img_size[0], img_size[1]),
                       dtype=np.float32)
        cytoplasm_masks = np.zeros(
            (len(img_files), img.shape[1], img.shape[2]), dtype=np.float32)

        counter = 0
        for name in img_files:
            img[counter, :, :] = get_image(os.path.join(load_from_direc, name))
            counter += 1

    for frame in xrange(img.shape[0]):
        interior = img[frame, :, :]

        nuclei = nuclear_masks[frame, :, :]

        nuclei_label = label(nuclei, background=0)

        seg = segment_image_w_morphsnakes(interior,
                                          nuclei_label,
                                          num_iters=num_iters,
                                          smoothing=smoothing)
        seg[seg == 0] = -1

        cytoplasm_mask = np.zeros(seg.shape, dtype=np.float32)
        max_cell_id = np.amax(seg)
        for cell_id in xrange(1, max_cell_id + 1):
            img_new = seg == cell_id
            img_fill = binary_fill_holes(img_new)
            cytoplasm_mask[img_fill == 1] = 1

        cytoplasm_masks[frame, :, :] = cytoplasm_mask

        if save:
            img_name = os.path.join(mask_location,
                                    "cytoplasm_mask_" + str(frame) + ".png")
            tiff.imsave(img_name, np.float32(cytoplasm_mask))

        if color_image:
            img_name = os.path.join(
                mask_location, "cytoplasm_colorimg_" + str(frame) + ".png")

            from skimage.segmentation import find_boundaries
            import palettable
            from skimage.color import label2rgb

            seg = label(cytoplasm_mask)
            bound = find_boundaries(seg, background=0)

            image_label_overlay = label2rgb(
                seg,
                bg_label=0,
                bg_color=(0.8, 0.8, 0.8),
                colors=palettable.colorbrewer.sequential.YlGn_9.mpl_colors)
            image_label_overlay[bound == 1, :] = 0

            scipy.misc.imsave(img_name, np.float32(image_label_overlay))

    return cytoplasm_masks
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue',
                        help='select tissue to train.',
                        default=None)
    parser.add_argument('--inputFolder',
                        help='Select input folder.',
                        default=None)
    parser.add_argument('--outputFolder',
                        help='select output folder',
                        default=None)
    parser.add_argument('--nr_images',
                        help='select number of images to create',
                        default=None)
    parser.add_argument('--overlapProbability',
                        help='select overlapProbability',
                        default=None)
    parser.add_argument('--scale', help='select output folder', default=None)

    args = parser.parse_args()
    tisquant = TisQuantExtract()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.overlapProbability:
        args.overlapProbability = float(args.overlapProbability)
    else:
        args.overlapProbability = 0.5
    if args.scale == '1':
        config.scale = True

    print(config.diagnosis)
    tools = Tools()

    annotated_nuclei = AnnotatedObjectSet()
    annotated_images = []
    #ids_paths = tisquant.dbconnector.execute(query=tisquant.getLevel3AnnotatedImagesByDiagnosis_Query(diagnosis = config.diagnosis,magnification = config.magnification, staining_type = config.staining_type, staining = config.staining, segmentation_function = config.segmentation_function, annotator = config.annotator, device = config.device))
    ids_images = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'images', '*.tif'))
    ids_masks = glob.glob(
        os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    #for index,elem in enumerate(ids_paths):
    for index, elem in enumerate(ids_images):
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageId_Query(elem[0],config.annotator))
        #groundtruth_paths = tisquant.dbconnector.execute(tisquant.getLevel3AnnotationByImageIdUsingMaxExperience_Query(elem[0], config.annotator))
        #for groundtruth_path in groundtruth_paths:
        test = AnnotatedImage()
        #    test.readFromPath(tools.getLocalDataPath(elem[1],1),tools.getLocalDataPath(groundtruth_path[0],3))
        test.readFromPath(ids_images[index], ids_masks[index])
        annotated_images.append(test)

    # Create artificial new dataset
    scales = tools.getNormalizedScales(annotated_images)

    for index, img in enumerate(annotated_images):
        test = AnnotatedImage()
        if config.scale:
            test.createWithArguments(
                tools.rescale_image(img.getRaw(),
                                    (scales[index], scales[index])),
                tools.rescale_mask(img.getMask(),
                                   (scales[index], scales[index]),
                                   make_labels=True))
        else:
            test.createWithArguments(img.getRaw(), img.getMask())
        annotated_nuclei.addObjectImage(
            test, useBorderObjects=config.useBorderObjects)
    if config.scale == 0:
        if args.tissue == 'Ganglioneuroma':
            possible_numbers = [9, 16, 25, 36, 49]
        else:
            possible_numbers = [4, 4, 9]
    else:
        possible_numbers = [9, 16, 25, 36, 49]

    # How many images?
    if not args.nr_images:
        args.nr_images = 10
    else:
        args.nr_images = int(args.nr_images)

    for t in tqdm(range(0, args.nr_images)):
        # Create artificial image
        number_nuclei = random.randint(0, possible_numbers.__len__() - 1)
        img = ArtificialAnnotatedImage(
            width=256,
            height=256,
            number_nuclei=possible_numbers[number_nuclei],
            probabilityOverlap=args.overlapProbability)
        total_added = 0
        for i in range(0, possible_numbers[number_nuclei]):
            test = annotated_nuclei.returnArbitraryObject()
            if (randint(0, 1)):
                test = tools.arbitraryEnhance(test)
                total_added += img.addImageAtGridPosition(test)
        if (total_added > 0):
            shape_y = img.getRaw().shape[0]
            shape_x = img.getRaw().shape[1]
            img_new = np.zeros((shape_y, shape_x * 2, 3), dtype=np.float32)
            img_new[:, 0:shape_x,
                    0] = img_new[:, 0:shape_x,
                                 1] = img_new[:, 0:shape_x,
                                              2] = img_new[:,
                                                           shape_x:2 * shape_x,
                                                           0] = img_new[:,
                                                                        shape_x:
                                                                        2 *
                                                                        shape_x,
                                                                        1] = img_new[:,
                                                                                     shape_x:
                                                                                     2
                                                                                     *
                                                                                     shape_x,
                                                                                     2] = img.getRaw(
                                                                                     )
            scipy.misc.toimage(
                img_new, cmin=0.0,
                cmax=1.0).save(config.outputFolder + config.diagnosis[0] +
                               '\\images\\Img_' + str(t) + '.jpg')
            tifffile.imsave(config.outputFolder + config.diagnosis[0] +
                            '\\masks\\Mask_' + str(t) + '.tif',
                            img.getMask(),
                            dtype=np.uint8)
    e = 1
Exemple #15
0
def save_cubes(
    cells,
    planes_paths,
    planes_to_read,
    planes_shape,
    voxel_sizes,
    network_voxel_sizes,
    num_planes_for_cube=20,
    cube_width=50,
    cube_height=50,
    cube_depth=20,
    thread_id=0,
    output_dir="",
    save_empty_cubes=False,
):
    """

    :param cells:
    :param planes_paths:
    :param planes_to_read:
    :param planes_shape:
    :param x_pix_um:
    :param y_pix_um:
    :param x_pix_um_network:
    :param y_pix_um_network:
    :param num_planes_for_cube:
    :param cube_width:
    :param cube_height:
    :param cube_depth:
    :param thread_id:
    :param output_dir:
    :param save_empty_cubes:
    :return:
    """
    channels = list(planes_paths.keys())
    stack_shape = planes_shape + (num_planes_for_cube,)
    stacks = {}
    planes_queues = {}
    for ch in channels:
        stacks[ch] = np.zeros(stack_shape, dtype=np.uint16)
        planes_queues[ch] = deque(maxlen=num_planes_for_cube)
    for plane_idx in tqdm(planes_to_read, desc="Thread: {}".format(thread_id)):
        for ch in channels:
            plane_path = planes_paths[ch][plane_idx]
            planes_queues[ch].append(tifffile.imread(plane_path))
            if len(planes_queues[ch]) == num_planes_for_cube:
                if is_even(num_planes_for_cube):
                    cell_z = int(plane_idx - num_planes_for_cube / 2 + 1)
                else:
                    cell_z = int(
                        plane_idx - floor(num_planes_for_cube) / 2 + 1
                    )

                for j, plane in enumerate(planes_queues[ch]):
                    stacks[ch][:, :, j] = plane

                # ensures no cube_depth planes at the end
                planes_queues[ch].popleft()
                # required since we provide all cells
                # TODO: if len(planes_queues[ch])
                #  < num_planes_for_cube -1: break
                for cell in cells[cell_z]:
                    cube = Cube(
                        cell,
                        ch,
                        stacks,
                        x_pix_um=voxel_sizes[2],
                        y_pix_um=voxel_sizes[1],
                        x_pix_um_network=network_voxel_sizes[2],
                        y_pix_um_network=network_voxel_sizes[1],
                        final_depth=cube_depth,
                        width=cube_width,
                        height=cube_height,
                        depth=num_planes_for_cube,
                    )
                    if not cube.empty or (cube.empty and save_empty_cubes):
                        tifffile.imsave(
                            os.path.join(output_dir, str(cube)), cube.data
                        )
from Classes.Helper import SVGTools
import matplotlib.pyplot as plt
from tifffile import tifffile
import glob
import os
diagnosis = ['normal', 'Neuroblastoma', 'Ganglioneuroma']
tool = SVGTools()
for diag in diagnosis:
    ids_images = glob.glob(
        os.path.join(
            r"\\chubaka\home\florian.kromp\settings\desktop\nucleusanalyzer\FFG COIN VISIOMICS\Ongoing\Image groundtruth curation",
            diag, '*_svg.svg'))
    for img_path in ids_images:
        mask = tool.transformSVGToMaskNew(img_path)
        tifffile.imsave(img_path.replace('_svg.svg', '_mask.tif'), mask)

#plt.imshow(tool.transformSVGToMaskNew(r"\\chubaka\home\florian.kromp\settings\desktop\nucleusanalyzer\FFG COIN VISIOMICS\Ongoing\Image groundtruth curation\Neuroblastoma\4471_svg.svg"))
Exemple #17
0
        fullsize = (float(a), float(a))
    elif o == '-t':
        fmt = a
    else:
        assert False, "unknown option"

image = np.zeros(fullsize)
xs, ys = samplesize
snr_ = []
for i in range(fullsize[0] / samplesize[0]):
    for j in range(fullsize[1] / samplesize[1]):

        sample, snr = gen_pattern(alpha)
        snr_.append(snr)
        print 'SNR : %.5f,' % snr,
        print 'Industrial SNR: %.3f db' % snr_ind(snr)

        image[i * xs:(i + 1) * xs, j * xs:(j + 1) * xs] = sample

snr_ = np.array(snr_)
print "Average SNR %.3f +/- %.3f  (%.2f db)" % \
    (snr_.mean(), snr_.std(), snr_ind(snr_.mean()))
# plt.imshow(image, cmap='gray')

if fmt == 'tif':
    tif.imsave('figs/ring.tif', image.astype(np.float32))
elif fmt == 'png':
    plt.savefig('figs/ring.png')
else:
    print 'Output format %s not supported' % fmt
def main():
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--tissue', help='select tissue to train.', default=None)
    parser.add_argument('--inputFolder', help='Select input folder.', default=None)
    parser.add_argument('--outputFolder', help='select output folder', default=None)
    parser.add_argument('--nr_images', help='select number of images to create', default=None)
    parser.add_argument('--overlapProbability', help='select overlapProbability', default=None)
    parser.add_argument('--scale', help='select output folder', default=None)
    parser.add_argument('--img_prefix', help='select output folder', default='Img_')
    parser.add_argument('--mask_prefix', help='select output folder', default='Mask_')
    #random.seed(13431)
    args = parser.parse_args()
    config = Config
    if args.tissue:
        config.diagnosis = [args.tissue]

    if args.outputFolder:
        config.outputFolder = args.outputFolder

    if args.overlapProbability:
        args.overlapProbability = float(args.overlapProbability)
    else:
        args.overlapProbability = 0.5

    if args.tissue == 'Ganglioneuroma':
        n_freq = 20#15
    else:
        n_freq = 30

    if args.scale == '1':
        config.scale=True

    print(config.diagnosis)
    tools = Tools()

    annotated_nuclei =[]
    annotated_images = []
    ids_images = glob.glob(os.path.join(args.inputFolder,config.diagnosis[0],'images','*.tif'))
    ids_masks = glob.glob(os.path.join(args.inputFolder, config.diagnosis[0], 'masks', '*.tif'))

    for index, elem in enumerate(ids_images):
        test = AnnotatedImage()
        test.readFromPath(ids_images[index], ids_masks[index],type='uint16')
        annotated_images.append(test)

    # Create artificial new dataset
    scales = tools.getNormalizedScales(annotated_images)
    running = 0
    for index,img in enumerate(annotated_images):
        test = AnnotatedImage()
        annotated_nuclei.append(AnnotatedObjectSet())
        if config.scale:
            test.createWithArguments(tools.rescale_image(img.getRaw(),(scales[index],scales[index])),tools.rescale_mask(img.getMask(),(scales[index],scales[index]), make_labels=True))
        else:
            test.createWithArguments(img.getRaw(),img.getMask())
        annotated_nuclei[running].addObjectImage(test, useBorderObjects=config.useBorderObjects, tissue=args.tissue, scale=Config.scale)
        running += 1
        del test
    if config.scale == 0:
            if args.tissue == 'Ganglioneuroma':
                possible_numbers = [9, 16, 25, 36, 49]
            else:
                possible_numbers = [4, 4, 9]
    else:
        possible_numbers = [9,16,25,36,49]

    # How many images?
    if not args.nr_images:
        args.nr_images=10
    else:
        args.nr_images=int(args.nr_images)

    for t in tqdm(range(0,args.nr_images)):
        nr_img = random.randint(0,annotated_nuclei.__len__()-1)
        # Create artificial image
        number_nuclei = random.randint(0, possible_numbers.__len__()-1)

        # calculate Background
        tmp_image = annotated_nuclei[nr_img].images[0].getRaw()
        tmp_mask = annotated_nuclei[nr_img].images[0].getMask()
        kernel = np.ones((15, 15), np.uint8)
        bg = cv2.erode((tmp_mask == 0).astype(np.uint8), kernel, iterations=1)
        bg = np.sort(tmp_image[np.where(bg>0)])
        img = ArtificialAnnotatedImage(width=256,height=256,number_nuclei=possible_numbers[number_nuclei],probabilityOverlap=args.overlapProbability,background=bg)
        total_added = 0
        for i in range(0,possible_numbers[number_nuclei]):
            test = annotated_nuclei[nr_img].returnArbitraryObject()
            if (randint(0,1)):
                test = tools.arbitraryEnhance(test)
                total_added += img.addImageAtGridPosition(test)
        if (total_added > 0):
            shape_y = img.getRaw().shape[0]
            shape_x = img.getRaw().shape[1]
            img_new = np.zeros((shape_y,shape_x*2,3),dtype=np.float32)
            img_new[:,0:shape_x,0] = img_new[:,0:shape_x,1] = img_new[:,0:shape_x,2] = img_new[:,shape_x:2*shape_x,0] = img_new[:,shape_x:2*shape_x,1] = img_new[:,shape_x:2*shape_x,2] = img.getRaw()
            scipy.misc.toimage(img_new, cmin=0.0, cmax=1.0).save(config.outputFolder + config.diagnosis[0] + '\\images\\' + args.img_prefix + str(t) + '.jpg')
            tifffile.imsave(config.outputFolder + config.diagnosis[0] + '\\masks\\' + args.mask_prefix + str(t) + '.tif',img.getMask(),dtype=np.uint8)
    e=1
Exemple #19
0
def main():
    parser = argparse.ArgumentParser(description=_description)
    parser.add_argument('--output',
                        '-o',
                        metavar='normalized.tif',
                        default='normalized.tif',
                        help='Output filename')
    parser.add_argument(
        '--subtract',
        action='store_true',
        help='Subtract background instead of dividing and truncating')
    parser.add_argument('infile')
    args = parser.parse_args()

    infile = args.infile
    print 'Reading image stack'
    t = tf.TiffFile(infile)
    ar = tf.stack_pages(t.pages)
    n = ar.shape[0]

    percentile = 0.01 if args.subtract else 0.05

    if os.path.exists('background.tif'):
        print 'Reading background image'
        bg = tf.imread('background.tif')
    else:
        print 'Computing background image'
        sorted_ar = ar.copy()
        sorted_ar.sort(0)
        bg = sorted_ar[int(round(percentile * n, 0))]
        print 'Saving background image'
        tf.imsave('background.tif', bg)
        del sorted_ar

    print 'Performing background normalization'
    if not args.subtract:
        ar = ar.astype(np.double)
        for i in range(n):
            ar[i] /= bg

        print 'Converting to 16-bit TIFF'
        max_normed = (4095.0 / bg.min()) - 1
        ar -= 1
        ar *= 65535
        ar /= max_normed
        ar = ar.round()
    else:
        ar = ar.astype(np.int16)
        for i in range(n):
            ar[i] -= bg
    ar[ar < 0] = 0
    ar = ar.astype(np.uint16)

    print 'Writing normalized image'
    with tf.TiffWriter(args.output) as out:
        for i in range(n):
            if (i % 100) == 0:
                print i,
                sys.stdout.flush()
            out.save(ar[i])
    print
Exemple #20
0
 def save_plane(self, plane):
     plane_name = f"plane_{str(self.z).zfill(4)}.tif"
     f_path = os.path.join(self.plane_directory, plane_name)
     tifffile.imsave(f_path, plane.T)
Exemple #21
0
def test_tiff_io(tmpdir, layer):
    folder = str(tmpdir)
    dest_path = os.path.join(folder, "layer.tiff")
    tifffile.imsave(dest_path, layer)
    reloaded = tifffile.imread(dest_path)
    assert (reloaded == layer).all()
    else:
        assert False, "unknown option"


image = np.zeros(fullsize)
xs, ys = samplesize
snr_ = []
for i in range(fullsize[0]/samplesize[0]):
    for j in range(fullsize[1]/samplesize[1]):

        sample, snr = gen_pattern(alpha)
        snr_.append(snr)
        print 'SNR : %.5f,' % snr,
        print 'Industrial SNR: %.3f db' % snr_ind(snr)

        image[i*xs:(i+1)*xs, j*xs:(j+1)*xs] = sample

snr_ = np.array(snr_)
print "Average SNR %.3f +/- %.3f  (%.2f db)" % \
    (snr_.mean(), snr_.std(), snr_ind(snr_.mean()))
# plt.imshow(image, cmap='gray')

if fmt == 'tif':
    tif.imsave('figs/ring.tif', image.astype(np.float32))
elif fmt == 'png':
    plt.savefig('figs/ring.png')
else:
    print 'Output format %s not supported' % fmt