Esempio n. 1
0
def main(image_path, psf_path):
    image = imageio.volread(image_path)  # z 0.6u
    psf = imageio.volread(psf_path)  # z 0.2u

    image, psf = image.astype(np.float32), psf.astype(np.float32)

    # crop image (too large)
    image = image[:, 768:768 + 512, 768:768 + 512]

    imageio.volwrite("input.tif", image)
    raise RuntimeError("DEBUG")

    # rescale psf
    psf = rescale(psf, (1 / 3, 1, 1), anti_aliasing=False)
    # normalize
    psf = (psf - psf.max()) / (psf.max() - psf.min())
    psf /= psf.sum()
    print(f"psf range [{psf.min(), psf.max()}]")

    print(f"image shape {image.shape}, psf shape {psf.shape}")

    try:
        deconv = deconvlucy(image, psf, 10)
    except Exception:
        raise
    else:
        print("saving...")
        imageio.volwrite("result.tif", deconv)
Esempio n. 2
0
    def _check_inputs(self):
        print("checking training data dims ... ")
        hr_im_list = sorted(
            tl.files.load_file_list(path=self.train_hr_path,
                                    regx='.*.tif',
                                    printable=False))
        lr_im_list = sorted(
            tl.files.load_file_list(path=self.train_lr_path,
                                    regx='.*.tif',
                                    printable=False))
        len(hr_im_list) == len(lr_im_list) or _raise(
            ValueError("Num of HR and LR not equal"))

        for hr_file, lr_file in zip(hr_im_list, lr_im_list):
            hr = imageio.volread(os.path.join(self.train_hr_path, hr_file))
            lr = imageio.volread(os.path.join(self.train_lr_path, lr_file))
            # print('checking dims: \n%s %s\n%s %s' % (hr_file, str(hr.shape), lr_file, str(lr.shape)))
            if 'factor' not in dir(self):
                self.factor = hr.shape[0] // lr.shape[0]
            valid_dim = [
                self.factor == hs / ls for hs, ls in zip(hr.shape, lr.shape)
            ]
            if not all(valid_dim):
                raise (ValueError(
                    'dims mismatch: \n%s %s\n%s %s' %
                    (hr_file, str(hr.shape), lr_file, str(lr.shape))))
Esempio n. 3
0
def processFile(filepath, zfile=None):
    """
    Get datatypes based on file extension
    """
    datatypes = []
    if endsWith(filepath, ['.nii', '.nii.gz', '.xrw', '.dcm']):
        datatypes = ['volume']
    elif endsWith(filepath, ['.obj', '.mtl']):
        datatypes = ['mesh']
    elif endsWith(filepath, ['.las', '.laz', '.ptx', '.ply', '.xyz', '.txt']):
        datatypes = ['point']
    elif endsWith(filepath, ['.jpg', '.jpeg', '.png', '.gif']):
        datatypes = ['image', 'photogrammetry']
    elif endsWith(filepath, ['.tif', '.tiff']):
        if (zfile != None):
            ofile = zfile.extract(filepath, path='/tmp/')
            img = imageio.volread(ofile)
            os.remove('/tmp/' + filepath)
        else:
            img = imageio.volread(filepath)
        shape = img.shape[::-1]
        dim = len(shape)
        if dim <= 2 or (dim == 3 and shape[2] <= 3):
            datatypes = ['image']
        else:
            datatypes = ['volume']
    return datatypes
Esempio n. 4
0
def test_different_read_modes():
    
    dname1, dname2, fname1, fname2 = _prepare()
    
    for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:
        
        # Test imread()
        im = imageio.imread(fname)
        assert isinstance(im, np.ndarray)
        assert im.shape == (512, 512)
        
        # Test mimread()
        ims = imageio.mimread(fname)
        assert isinstance(ims, list)
        assert ims[0].shape == im.shape
        assert len(ims) > 1
        #    
        ims2 = imageio.mimread(dname)
        assert len(ims) == len(ims2)
        
        # Test volread()
        vol = imageio.volread(dname)
        assert vol.ndim == 3
        assert vol.shape[0] > 10
        assert vol.shape[1:] == (512, 512)
        #
        vol2 = imageio.volread(fname)  # fname works as well
        assert (vol == vol2).all()
        
        # Test mvolread()
        vols = imageio.mvolread(dname)
        assert isinstance(vols, list)
        assert len(vols) == n
        assert vols[0].shape == vol.shape
        assert sum([v.shape[0] for v in vols]) == len(ims)
Esempio n. 5
0
def load_training_data(root, image_folder, labels_folder, ext):

    # get the image and label mask paths and validate them
    image_pattern = os.path.join(root, image_folder, f'*{ext}')
    print("Looking for images with the pattern", image_pattern)
    train_images = glob(image_pattern)
    assert len(train_images) > 0, "Did not find any images"
    train_images.sort()

    label_pattern = os.path.join(root, labels_folder, f'*{ext}')
    print("Looking for labels with the pattern", image_pattern)
    train_labels = glob(label_pattern)
    assert len(train_labels) > 0, "Did not find any labels"
    train_labels.sort()

    check_training_data(train_images, train_labels)

    # normalization parameters: lower and upper percentile used for image normalization
    # maybe these should be exposed
    lower_percentile = 1
    upper_percentile = 99.8
    ax_norm = (0, 1, 2)

    train_images = [imageio.volread(im) for im in train_images]
    train_labels = [imageio.volread(im) for im in train_labels]
    check_training_images(train_images, train_labels)
    train_images = [
        normalize(im, lower_percentile, upper_percentile, axis=ax_norm)
        for im in train_images
    ]
    train_labels = [fill_label_holes(im) for im in train_labels]

    return train_images, train_labels
Esempio n. 6
0
def test_different_read_modes():

    dname1, dname2, fname1, fname2 = _prepare()

    for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:

        # Test imread()
        im = imageio.imread(fname)
        assert isinstance(im, np.ndarray)
        assert im.shape == (512, 512)

        # Test mimread()
        ims = imageio.mimread(fname)
        assert isinstance(ims, list)
        assert ims[0].shape == im.shape
        assert len(ims) > 1
        #
        ims2 = imageio.mimread(dname)
        assert len(ims) == len(ims2)

        # Test volread()
        vol = imageio.volread(dname)
        assert vol.ndim == 3
        assert vol.shape[0] > 10
        assert vol.shape[1:] == (512, 512)
        #
        vol2 = imageio.volread(fname)  # fname works as well
        assert (vol == vol2).all()

        # Test mvolread()
        vols = imageio.mvolread(dname)
        assert isinstance(vols, list)
        assert len(vols) == n
        assert vols[0].shape == vol.shape
        assert sum([v.shape[0] for v in vols]) == len(ims)
Esempio n. 7
0
 def getmaskarray(self):
     if self.mfn is None:
         return None
     try:
         im = imageio.volread(self.mfn, flatten=True)
     except TypeError:
         try:
             im = imageio.volread(self.mfn)
         except Exception:
             # require tifffile only if imageio fails
             from skimage.external.tifffile import tifffile
             im = tifffile.imread(self.mfn)
     return im
    def check_result(self, in_path, res_path, check_range=False):
        res = imageio.volread(res_path)
        exp = imageio.volread(in_path).astype(res.dtype)

        if check_range:
            min_res = res.min()
            min_exp = exp.min()
            self.assertEqual(min_res, min_exp)
            max_res = res.max()
            max_exp = exp.max()
            self.assertEqual(max_res, max_exp)
        else:
            un_res = np.unique(res)
            un_exp = np.unique(exp)
            self.assertTrue(np.array_equal(un_exp, un_res))
Esempio n. 9
0
    def __set_image_volume(self):
        pathname = url2pathname(self.get_url())

        # Volume loading is currently limited to tiffs/numpy files only
        if is_numpy_file(self.__filename):
            data = numpy.load(pathname)
        else:
            data = imageio.volread(pathname)

        # https://github.com/CellProfiler/python-bioformats/blob/855f2fb7807f00ef41e6d169178b7f3d22530b79/bioformats/formatreader.py#L768-L791
        if data.dtype in [numpy.int8, numpy.uint8]:
            self.scale = 255
        elif data.dtype in [numpy.int16, numpy.uint16]:
            self.scale = 65535
        elif data.dtype == numpy.int32:
            self.scale = 2**32 - 1
        elif data.dtype == numpy.uint32:
            self.scale = 2**32
        else:
            self.scale = 1

        self.__image = Image(
            image=data,
            path_name=self.get_pathname(),
            file_name=self.get_filename(),
            dimensions=3,
            scale=self.scale,
            spacing=self.__spacing,
        )
Esempio n. 10
0
def pack_itk_snap(dst_path: str, raw, label, overwrite: bool = True):
    """
    Pack raw-label product from an ITK-SNAP project.

    Args:
        dst_path (str): destination file
        raw (array-like): the source NRRD file
        label (array-like): the NIFTI label file
        overwrite (bool, optional): overwrite destination file
    """
    try:
        import imageio
    except ImportError:
        logger.error('requires "imageio" to load the source')
        raise

    logger.debug('loading raw data')
    raw = imageio.volread(raw)  # volread can actually handle 2-D as well
    logger.debug('loading label')
    label = _read_nifti(label)

    assert raw.shape == label.shape, "shape mis-matched between raw data and the label"

    mode = "w" if overwrite else "x"
    with h5py.File(dst, mode) as h:
        h["raw"] = raw
        h["label"] = label.astype(np.uint8)
Esempio n. 11
0
def processTiffStack(infile, outdir, channel, timestep, verbose):
    """
    Process multi-dimensional tiff
    :param infile: 
    :param outdir: 
    :param verbose: 
    :return: 
    """
    if verbose: print('processTiffStack')
    #img = Image.open(infile)
    img = imageio.volread(infile)
    volinfo = parseVolInfo(img.shape)
    volinfo["shape"] = img.shape
    #volinfo["dtype"] = getNumpyType(img.dtype.name)
    volinfo["dtype"] = getFrameData(img, volinfo, 0, 0, 0).dtype
    if verbose:
        print(volinfo)

    if volinfo["numslices"] < 5:
        raise NameError("Tiff has less than 5 frames")

    fullW = volinfo["size"][0]
    fullH = volinfo["size"][1]
    voldata = np.zeros((fullH, fullW, volinfo["numslices"]), volinfo["dtype"])

    for i in range(volinfo["numslices"]):
        voldata[:, :, i] = getFrameData(img, volinfo, i, channel, timestep)

    # normalize 16 bit data
    if volinfo["dtype"] == np.dtype(np.uint16) or volinfo["dtype"] == np.dtype(
            np.int16):
        voldata = normalize16bitData(voldata, verbose)

    saveVolume(voldata, volinfo, outdir, verbose)
Esempio n. 12
0
def _LoadKer(file, fold=FOLD_TMP, *, fmt=FMT.TIF):
    """
	Reads the 4D array `ker` [T,Z,Y,X] from a multilayer .tif image
	"""
    ## Read from File ##
    return np.moveaxis(np.array(imageio.volread(fold + file + str(fmt))),
                       (3, 2), (0, 1))
def read_logpack(fn):
    data = -1.0 + np.array(imageio.volread(fn), dtype='float') / 2**15
    scale = float(".".join(fn.split('.')[:-1]).split('_lp')[-1])
    return np.exp2(data * scale)


# write_logpack(main_imgfile[:-4]+'_fdata' , flepith.fdata3d)
Esempio n. 14
0
def getGlom_im(file_name):
    #print(imageio.volread(file_name).astype(float).dtype)
    im = torch.tensor(imageio.volread(file_name).astype(float),
                      dtype=torch.float32)
    im /= torch.max(im) - 0.5
    im = im.unsqueeze(0)
    return im
def main(image, colormap, scan_type, lateral, axial, angle, suffix):
    # load image
    logger.info("loading \"{}\"".format(image))
    I = imageio.volread(image)
    logger.info("image shape {}".format(I.shape))

    # load colormap
    cm = imageio.imread(colormap)
    cm = cm[0, :, :]
    logger.info("colormap contains {} steps".format(cm.shape[0]))

    nz, ny, nx = I.shape
    rad = radians(angle)
    if scan_type == 'spl':
        scale = (np.float32(0.), np.float32(nx * sin(rad)))
        convert = lambda x, y: colored_by_depth_spl(x, y, lateral, axial, rad)
    elif scan_type == 'obj':
        raise NotImplementedError
    elif scan_type == 'ortho':
        raise NotImplementedError
    lookup = create_lookup_function(cm, scale)

    J = convert(I, lookup)

    # apply suffix
    fn, ext = os.path.splitext(image)
    for iz in tqdm(range(nz), total=nz, file=tqdm_log):
        imageio.imwrite('{}_{:03d}_{}{}'.format(fn, iz, suffix, ext), J[iz,
                                                                        ...])
Esempio n. 16
0
def main(src_dir, binning=(4, 4, 1), host=None, n_workers=1):
    desc_bin = "-".join(str(b) for b in binning)
    parent, dname = os.path.split(src_dir)
    dst_dir = os.path.join(parent, f"{dname}_bin{desc_bin}")
    logger.info(f'destination: "{dst_dir}"')

    try:
        os.makedirs(dst_dir)
    except FileExistsError:
        logger.warning(f"destination folder already exists")

    file_list = glob.glob(os.path.join(src_dir, "*.tif"))
    logger.info(f"found {len(file_list)} file(s) to process")

    # test read
    array = imageio.volread(file_list[0])
    shape, dtype = array.shape, array.dtype
    del array
    logger.info(f"array info {shape}, {dtype}")

    # build sampler
    sampler = tuple(slice(None, None, b) for b in reversed(binning))

    @delayed
    def volread_np(uri):
        return np.array(imageio.volread(uri))

    def volread_da(uri):
        return da.from_delayed(volread_np(uri), shape, dtype)

    tasks = []
    for src_path in file_list:
        array = volread_da(src_path)
        array = array[sampler]

        fname = os.path.basename(src_path)
        dst_path = os.path.join(dst_dir, fname)
        task = delayed(imageio.volwrite)(dst_path, array)

        tasks.append(task)

    def downsample(src_path, dst_path=None):
        if dst_path is None:
            fname = os.path.basename(src_path)
            dst_path = os.path.join(dst_dir, fname)

        # extract
        array = volread_da(src_path)
        # transform
        array = array[sampler]
        # load
        imageio.volwrite(dst_path, array)

        return dst_path

    with get_client(address=host, n_workers=n_workers, threads_per_worker=8):
        dst_path = batch_submit(downsample, file_list, return_results=True)
        for i, path in enumerate(dst_path):
            print(f"{i+1}/{len(file_list)}, {os.path.basename(path)}")
        """
Esempio n. 17
0
def volread(filename):
    """ volread(filename) 
    
    Read volume from a file. If filename is 'stent', read a dedicated
    test dataset. For reading any other kind of volume, the imageio
    package is required.
    
    """
    
    if filename == 'stent':
        # Get full filename
        path = vv.misc.getResourceDir()
        filename2 = os.path.join(path, 'stent_vol.ssdf')
        if os.path.isfile(filename2):
            filename = filename2
        else:
            raise IOError("File '%s' does not exist." % filename)
        # Load
        s = vv.ssdf.load(filename)
        return s.vol.astype('int16') * s.colorscale
    
    elif imageio is not None:
        return imageio.volread(filename)
        
    else:
        raise RuntimeError("visvis.volread needs the imageio package to read arbitrary files.")
Esempio n. 18
0
def load_segmentation_from_tif(segmentation_fpath):
    volume = volread(segmentation_fpath)
    transposed = np.transpose(volume, axes=(1, 2, 0))

    segmentation = transposed.view(Segmentation3D)

    return segmentation
Esempio n. 19
0
def get_normalized_im(path, normalize='fixed'):
    """read 3-D tiff and normalize
    Params:
        normalize: str, in ['fixed', 'percentile']

    Return:
        np.adarray in shape of [depth, height, width, channels=1]
    """

    assert normalize in ['fixed', 'percentile']

    im = imageio.volread(path)  # [depth, height, width]
    max_val = 255.
    if im.dtype in [np.uint8, 'uint8']:
        pass
    elif im.dtype in [np.uint16, 'uint16']:
        max_val = 65535.
    elif im.dtype in [np.float32, 'float32']:
        pass
    else:
        raise Exception('\nunsupported image bitdepth %s\n' % str(im.dtype))

    if (im.dtype == np.float32) and normalize == 'percentile':
        raise ValueError(
            'normalization mode "fixed" cannot be applied to image with dtype float32'
        )

    im = normalize_max(
        im, max_val) if normalize == 'fixed' else normalize_percentile(im)
    im = im[..., np.newaxis]  # [depth, height, width, channels=1]
    return im
Esempio n. 20
0
def readvol(filename, dataset=None, drop_channel=True):
    r"""Load a image volume in HDF5, TIFF or PNG formats.
    """
    img_suf = filename[filename.rfind('.') + 1:]
    if img_suf in ['h5', 'hdf5']:
        data = readh5(filename, dataset)
    elif 'tif' in img_suf:
        data = imageio.volread(filename).squeeze()
        if data.ndim == 4:
            # convert (z,c,y,x) to (c,z,y,x) shape
            data = data.transpose(1, 0, 2, 3)
    elif 'png' in img_suf:
        data = readimgs(filename)
        if data.ndim == 4:
            # convert (z,y,x,c) to (c,z,y,x) shape
            data = data.transpose(3, 0, 1, 2)
    else:
        raise ValueError('unrecognizable file format for %s' % (filename))

    if drop_channel and data.ndim == 4:
        # convert multiple channels to grayscale by average
        data = np.mean(data, axis=0).astype(np.uint8)

    assert data.ndim in [3,
                         4], "Volume data should be 3D or 4D, got {}D".format(
                             data.ndim)

    return data
Esempio n. 21
0
    def _load_test_images(self):
        images = []
        for im_path in self.test_names:
            im = imageio.volread(im_path).astype('float32')
            for i in range(im.shape[0]):
                images.append(im[i, :, :])
        images = np.stack(images)

        print('%d images' % len(images))

        # Put images in correct format
        norm = lambda im: (im / 255.0).reshape((512, 512, 1))
        np_test_imgs = np.array([norm(im) for im in images])

        # Take a crop size, crop size chunk from the center of the image
        np_test_imgs = np_test_imgs[:, (512 // 2) -
                                    self.params.crop // 2:(512 // 2) +
                                    self.params.crop // 2, (512 // 2) -
                                    self.params.crop // 2:(512 // 2) +
                                    self.params.crop // 2, :]

        if self.params.network_dimension == 3:
            np_test_imgs = np.expand_dims(np_test_imgs, axis=0)
            # Split images into chunks of length self.params.depth
            np_test_imgs = np.array_split(np_test_imgs,
                                          np_test_imgs.shape[1] //
                                          self.params.depth,
                                          axis=1)
        elif self.params.network_dimension == 2:
            # Split images into chunks of length 1
            np_test_imgs = np.array_split(np_test_imgs,
                                          np_test_imgs.shape[0],
                                          axis=0)
        self.test_imgs = np_test_imgs
Esempio n. 22
0
def find_spots_in_round(sample_name, round_index, imaging_round,
                        input_directory, output_directory):
    """
    """

    round_directory = ("round%d" % round_index)
    sample_input_subdirectory = input_directory / sample_name / round_directory
    sample_output_subdirectory = output_directory / sample_name / round_directory
    sample_output_subdirectory.mkdir(parents=True, exist_ok=True)

    channels = imaging_round["channels"]
    filename = imaging_round["filename"]
    reference_channel = imaging_round["reference_channel"]
    for channel_index, channel in enumerate(channels):
        input_filename = filename + ("_fused_tp_0_ch_%d.tif" % channel_index)
        output_filename = channel + ".tif"
        input_path = sample_input_subdirectory / input_filename
        output_path = sample_output_subdirectory / output_filename

        if channel_index == reference_channel:
            image_stack = imageio.volread(input_path)
            max_filtered_image_stack = image_stack.max(0)
            imageio.imsave(output_path, max_filtered_image_stack)
        else:
            find_spots(input_path, output_path)
Esempio n. 23
0
def volread_HWD(filename, path, make_mask=False):
    im = imageio.volread(os.path.join(path,
                                      filename))  # [depth, height, width]
    if make_mask:
        im = otsu_thresholding(im)

    return rearrange3d(im)
Esempio n. 24
0
    def __getitem__(self, idx: int) -> Dict[str, Union[numpy.ndarray, list]]:
        sample = super().__getitem__(idx)
        path_idx = idx // self.info.datasets_per_file
        idx %= self.info.datasets_per_file
        img_path = self.paths[path_idx]

        try:
            img: numpy.ndarray = imageio.volread(img_path)
        except Exception as e:
            logger.error("Cannot load %s due to %s", img_path, e)
            raise e

        img = numpy.asarray(img)
        if self.info.datasets_per_file > 1:
            img = img[idx:idx + 1]

        for axis in self.remove_singleton_axes_at:
            if img.shape[axis] == 1:
                img = numpy.squeeze(img, axis=axis)

        for axis in self.insert_singleton_axes_at:
            img = numpy.expand_dims(img, axis=axis)

        while len(img.shape) > 5 and img.shape[0] == 1:
            logger.warning("squeeze(0) %s", img.shape)
            img = img.squeeze(0)

        assert len(img.shape) == 5, (self.info.name, idx, img.shape)

        sample[self.tensor_name] = img
        sample["batch_len"] = img.shape[0]
        return self.transform(sample)
Esempio n. 25
0
def main(settings):
    # Initiate the analysis pipline by parameters in SETTINGS.
    with open(settings, 'r') as fd:
        settings = yaml.safe_load(fd)

    # Set logger.
    handler = logging.StreamHandler()
    formatter = logging.Formatter(
        '%(levelname).1s %(asctime)s [%(name)s] %(message)s', '%H:%M:%S'
    )
    handler.setFormatter(formatter)
    log_level = logging.WARNING if settings['VERBOSE'] == 0 else logging.DEBUG
    logging.basicConfig(level=log_level, handlers=[handler])
    logger = logging.getLogger(__name__)

    # Start the image analysis.
    frames = imageio.volread(settings['INPF_RAW_IMAGE'])

    analyzer = Analyzer(settings)
    analyzer.process(frames)

    #DEBUG
    #from pprint import pprint
    #pprint(settings)
    print(frames.shape)
Esempio n. 26
0
    def __getitem__(
            self,
            idx: int) -> typing.OrderedDict[str, Union[numpy.ndarray, list]]:
        path_idx = idx // self.info.datasets_per_file
        idx %= self.info.datasets_per_file
        try:
            img_path = self.paths[path_idx]
        except IndexError as e:
            logger.error(
                f"idx: {idx}, path_idx: {path_idx}, paths: {self.paths}")
            raise e

        try:
            img: numpy.ndarray = numpy.asarray(imageio.volread(img_path))
        except Exception as e:
            logger.error("Cannot load %s", img_path)
            raise e

        if self.info.datasets_per_file > 1:
            img = img[idx:idx + 1]

        for axis in self.remove_singleton_axes_at:
            img = numpy.squeeze(img, axis=axis)

        for axis in self.insert_singleton_axes_at:
            img = numpy.expand_dims(img, axis=axis)

        return self.transform(OrderedDict(**{self.tensor_name: img}))
Esempio n. 27
0
def analyze_psf(ctx, path, ratio, lateral, axial, preview, save_roi,
                no_fix_negative):
    path = os.path.abspath(path)

    data = imageio.volread(path)
    if not no_fix_negative:
        vmin = np.min(data)
        if vmin < 0:
            data -= vmin

    analyzer = PSFAverage((axial, lateral, lateral), ratio)
    rois, table = analyzer(data)

    out_dir, _ = os.path.splitext(path)
    out_dir = f'{out_dir}_psf'
    try:
        os.mkdir(out_dir)
    except FileExistsError:
        logger.warning(f'output directory "{out_dir}" exists')

    if save_roi:
        logger.info("saving ROIs...")
        for i, roi in enumerate(rois):
            out_path = os.path.join(out_dir, f'psf_{i:03d}.tif')
            imageio.volwrite(out_path, roi)

    if preview:
        fig, ax = plt.subplots()
        plt.ion()
        plt.show()
Esempio n. 28
0
    def __init__(self, parent=None):
        # initialization of the superclass
        super(VideoMainWindow, self).__init__(parent)
        # setup the GUI
        self.setupUi(self)
        # Use a file dialog to choose tiff stack
        file, mask = QFileDialog.getOpenFileName(self, 'Open a .tif/.tiff stack')
        # Read image data
        self.img = volread(file)
        # Transpose second and third axes (y, x) to correct orientation (x, y)
        self.img = np.transpose(self.img, (0, 2, 1))
        # for i in range(0, len(self.img)):
        #     self.img[i] = self.img[i].T

        # connect the signals with the slots
        # self.actionLoad.triggered.connect(self.open_tiff)
        # self.actionClose.triggered.connect(self.close)
        # self.actionTIFF.triggered.connect(self.open_tiff)
        # self.actionFolder.triggered.connect(self.open_folder)
        # self.actionStart_ImageProcess.triggered.connect(self.image_process)

        self.ptr = 0
        self.lastTime = ptime.time()
        self.fps = None

        # self.start()

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(0)
Esempio n. 29
0
def volread(filename):
    """ volread(filename) 
    
    Read volume from a file. If filename is 'stent', read a dedicated
    test dataset. For reading any other kind of volume, the imageio
    package is required.
    
    """

    if filename == 'stent':
        # Get full filename
        path = vv.misc.getResourceDir()
        filename2 = os.path.join(path, 'stent_vol.ssdf')
        if os.path.isfile(filename2):
            filename = filename2
        else:
            raise IOError("File '%s' does not exist." % filename)
        # Load
        s = vv.ssdf.load(filename)
        return s.vol.astype('int16') * s.colorscale

    elif imageio is not None:
        return imageio.volread(filename)

    else:
        raise RuntimeError(
            "visvis.volread needs the imageio package to read arbitrary files."
        )
Esempio n. 30
0
def fetch_test_data():
    try:
        from urllib.request import urlopen
    except ImportError:
        from urllib2 import urlopen

    try:
        from io import BytesIO as Buffer
    except ImportError:
        from StringIO import StringIO as Buffer

    import zipfile
    from imageio import volread

    im_url = "https://imagej.nih.gov/ij/images/t1-head-raw.zip"

    with closing(urlopen(im_url)) as response:
        if response.status != 200:
            raise RuntimeError(
                "Test data could not be found at {}, status code {}".format(
                    im_url, response.status))
        zip_buffer = Buffer(response.read())

    with zipfile.ZipFile(zip_buffer) as zf:
        tif_buffer = Buffer(zf.read('JeffT1_le.tif'))
        return np.asarray(volread(tif_buffer, format='tif'), dtype=np.uint8)
Esempio n. 31
0
def run(file_list, train_on, name="untitled", dst_dir="."):
    try:
        os.makedirs(dst_dir)
    except FileExistsError:
        pass

    model_dir = os.path.join(dst_dir, "model")
    try:
        os.makedirs(model_dir)
    except FileExistsError:
        pass

    logger.info("preloading image for training")
    image = imageio.volread(train_on)
    train(image, name=name, model_dir=model_dir)

    def load_image():
        for file_path in file_list:
            yield imageio.volread(file_path)

    for file_name, result in zip(
            file_list, infer(load_image(), name=name, model_dir=model_dir)):
        fname = os.path.basename(file_name)
        file_path = os.path.join(dst_dir, fname)
        imageio.imwrite(file_path, result)
Esempio n. 32
0
 def __readimage(self, fn):
     try:
         im = imageio.volread(fn)
     except Exception:
         # require tifffile only if imageio fails
         from skimage.external.tifffile import tifffile
         im = tifffile.imread(fn)
     return self.__fix_image_dimensions(im)
Esempio n. 33
0
def test_volume():

    vol1 = imageio.imread("imageio:stent.npz")
    assert vol1.shape == (256, 128, 128)

    fname = os.path.join(test_dir, "stent.bsdf")
    imageio.volsave(fname, vol1)

    vol2 = imageio.volread(fname)
    assert vol1.shape == vol2.shape
    assert np.all(vol1 == vol2)
Esempio n. 34
0
def test_npz_reading_writing():
    """ Test reading and saveing npz """
    
    if IS_PYPY:
        return  # no support for npz format :(
    
    im2 = np.ones((10, 10), np.uint8) * 2
    im3 = np.ones((10, 10, 10), np.uint8) * 3
    im4 = np.ones((10, 10, 10, 10), np.uint8) * 4
    
    filename1 = os.path.join(test_dir, 'test_npz.npz')

    # One image
    imageio.imsave(filename1, im2)
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 1
    
    # Multiple images
    imageio.mimsave(filename1, [im2, im2, im2])
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 3
    
    # Volumes
    imageio.mvolsave(filename1, [im3, im3])
    im = imageio.volread(filename1)
    ims = imageio.mvolread(filename1)
    assert (im == im3).all()
    assert len(ims) == 2
    
    # Mixed
    W = imageio.save(filename1)
    assert W.format.name == 'NPZ'
    W.append_data(im2)
    W.append_data(im3)
    W.append_data(im4)
    raises(RuntimeError, W.set_meta_data, {})  # no meta data support
    W.close()
    #
    R = imageio.read(filename1)
    assert R.format.name == 'NPZ'
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    assert (ims[1] == im3).all()
    assert (ims[2] == im4).all()
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)
    raises(RuntimeError, R.get_meta_data, None)  # no meta data support
    raises(RuntimeError, R.get_meta_data, 0)  # no meta data support
Esempio n. 35
0
def test_spe_reading():
    need_internet()
    fname = get_remote_file("images/test_000_.SPE")

    fr1 = np.zeros((32, 32), np.uint16)
    fr2 = np.ones_like(fr1)

    # Test imread
    im = imageio.imread(fname)
    ims = imageio.mimread(fname)

    np.testing.assert_equal(im, fr1)
    np.testing.assert_equal(ims, [fr1, fr2])

    # Test volread
    vol = imageio.volread(fname)
    vols = imageio.mvolread(fname)

    np.testing.assert_equal(vol, [fr1, fr2])
    np.testing.assert_equal(vols, [[fr1, fr2]])

    # Test get_reader
    r = imageio.get_reader(fname)

    np.testing.assert_equal(r.get_data(1), fr2)
    np.testing.assert_equal(list(r), [fr1, fr2])
    pytest.raises(IndexError, r.get_data, -1)
    pytest.raises(IndexError, r.get_data, 2)

    # check metadata
    md = r.get_meta_data()
    assert md["ROIs"] == [
        {"top_left": [238, 187], "bottom_right": [269, 218], "bin": [1, 1]}
    ]
    cmt = [
        "OD 1.0 in r, g                                                    "
        "              ",
        "000200000000000004800000000000000000000000000000000000000000000000"
        "0002000001000X",
        "                                                                  "
        "              ",
        "                                                                  "
        "              ",
        "ACCI2xSEQU-1---10000010001600300EA                              SW"
        "0218COMVER0500",
    ]
    assert md["comments"] == cmt
    np.testing.assert_equal(md["frame_shape"], fr1.shape)
Esempio n. 36
0
def test_dicom():
    
    # Test volread()
    fname1 = get_remote_file('images/dicom_sample.zip', test_dir)
    dname1 = fname1[:-4]
    z = ZipFile(fname1)
    z.extractall(dname1)
    #
    vol = imageio.volread(dname1, 'DICOM')
    assert vol.ndim == 3
    assert vol.shape[0] > 20
    assert vol.shape[1] == 512
    assert vol.shape[2] == 512
    
    # Test volsave()
    raises(ValueError, imageio.volsave, dname1, vol)
    raises(ValueError, imageio.volsave, dname1, np.zeros((100, 100, 100, 3)))
    # todo: we have no format to save volumes yet!
    
    # Test mvolread()
    vols = imageio.mvolread(dname1, 'DICOM')
    assert isinstance(vols, list)
    assert len(vols) == 1
    assert vols[0].shape == vol.shape
Esempio n. 37
0
def test_functions():
    """ Test the user-facing API functions """

    # Test help(), it prints stuff, so we just check whether that goes ok
    imageio.help()  # should print overview
    imageio.help("PNG")  # should print about PNG

    fname1 = get_remote_file("images/chelsea.png", test_dir)
    fname2 = fname1[:-3] + "jpg"
    fname3 = fname1[:-3] + "notavalidext"
    open(fname3, "wb")

    # Test read()
    R1 = imageio.read(fname1)
    R2 = imageio.read(fname1, "png")
    assert R1.format is R2.format
    # Fail
    raises(ValueError, imageio.read, fname3)  # existing but not readable
    raises(FileNotFoundError, imageio.read, "notexisting.barf")
    raises(IndexError, imageio.read, fname1, "notexistingformat")

    # Test save()
    W1 = imageio.save(fname2)
    W2 = imageio.save(fname2, "JPG")
    W1.close()
    W2.close()
    assert W1.format is W2.format
    # Fail
    raises(FileNotFoundError, imageio.save, "~/dirdoesnotexist/wtf.notexistingfile")

    # Test imread()
    im1 = imageio.imread(fname1)
    im2 = imageio.imread(fname1, "png")
    assert im1.shape[2] == 3
    assert np.all(im1 == im2)

    # Test imsave()
    if os.path.isfile(fname2):
        os.remove(fname2)
    assert not os.path.isfile(fname2)
    imageio.imsave(fname2, im1[:, :, 0])
    imageio.imsave(fname2, im1)
    assert os.path.isfile(fname2)

    # Test mimread()
    fname3 = get_remote_file("images/newtonscradle.gif", test_dir)
    ims = imageio.mimread(fname3)
    assert isinstance(ims, list)
    assert len(ims) > 1
    assert ims[0].ndim == 3
    assert ims[0].shape[2] in (1, 3, 4)
    # Test protection
    with raises(RuntimeError):
        imageio.mimread("imageio:chelsea.png", "dummy", length=np.inf)

    if IS_PYPY:
        return  # no support for npz format :(

    # Test mimsave()
    fname5 = fname3[:-4] + "2.npz"
    if os.path.isfile(fname5):
        os.remove(fname5)
    assert not os.path.isfile(fname5)
    imageio.mimsave(fname5, [im[:, :, 0] for im in ims])
    imageio.mimsave(fname5, ims)
    assert os.path.isfile(fname5)

    # Test volread()
    fname4 = get_remote_file("images/stent.npz", test_dir)
    vol = imageio.volread(fname4)
    assert vol.ndim == 3
    assert vol.shape[0] == 256
    assert vol.shape[1] == 128
    assert vol.shape[2] == 128

    # Test volsave()
    volc = np.zeros((10, 10, 10, 3), np.uint8)  # color volume
    fname6 = os.path.join(test_dir, "images", "stent2.npz")
    if os.path.isfile(fname6):
        os.remove(fname6)
    assert not os.path.isfile(fname6)
    imageio.volsave(fname6, volc)
    imageio.volsave(fname6, vol)
    assert os.path.isfile(fname6)

    # Test mvolread()
    vols = imageio.mvolread(fname4)
    assert isinstance(vols, list)
    assert len(vols) == 1
    assert vols[0].shape == vol.shape

    # Test mvolsave()
    if os.path.isfile(fname6):
        os.remove(fname6)
    assert not os.path.isfile(fname6)
    imageio.mvolsave(fname6, [volc, volc])
    imageio.mvolsave(fname6, vols)
    assert os.path.isfile(fname6)

    # Fail for save functions
    raises(ValueError, imageio.imsave, fname2, np.zeros((100, 100, 5)))
    raises(ValueError, imageio.imsave, fname2, 42)
    raises(ValueError, imageio.mimsave, fname5, [np.zeros((100, 100, 5))])
    raises(ValueError, imageio.mimsave, fname5, [42])
    raises(ValueError, imageio.volsave, fname6, np.zeros((100, 100, 100, 40)))
    raises(ValueError, imageio.volsave, fname6, 42)
    raises(ValueError, imageio.mvolsave, fname6, [np.zeros((90, 90, 90, 40))])
    raises(ValueError, imageio.mvolsave, fname6, [42])
Esempio n. 38
0
def test_tifffile_reading_writing():
    """ Test reading and saving tiff """
    
    need_internet()  # We keep a test image in the imageio-binary repo
    
    im2 = np.ones((10, 10, 3), np.uint8) * 2

    filename1 = os.path.join(test_dir, 'test_tiff.tiff')

    # One image
    imageio.imsave(filename1, im2)
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert im.shape == im2.shape
    assert (im == im2).all()
    assert len(ims) == 1
    
    # Multiple images
    imageio.mimsave(filename1, [im2, im2, im2])
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert im.shape == im2.shape
    assert (im == im2).all()  # note: this does not imply that the shape match!
    assert len(ims) == 3
    for i in range(3):
        assert ims[i].shape == im2.shape
        assert (ims[i] == im2).all()

    # Read all planes as one array - we call it a volume for clarity
    vol = imageio.volread(filename1)
    vols = imageio.mvolread(filename1)
    assert vol.shape == (3, ) + im2.shape
    assert len(vols) == 1 and vol.shape == vols[0].shape
    for i in range(3):
        assert (vol[i] == im2).all()
    
    # remote multipage rgb file
    filename2 = get_remote_file('images/multipage_rgb.tif')
    img = imageio.mimread(filename2)
    assert len(img) == 2
    assert img[0].shape == (3, 10, 10)

    # Mixed
    W = imageio.save(filename1)
    W.set_meta_data({'planarconfig': 'SEPARATE'})  # was "planar"
    assert W.format.name == 'TIFF'
    W.append_data(im2)
    W.append_data(im2)
    W.close()
    #
    R = imageio.read(filename1)
    assert R.format.name == 'TIFF'
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    # meta = R.get_meta_data()
    # assert meta['orientation'] == 'top_left'  # not there in later version
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)
    
    # Ensure imread + imwrite works round trip
    filename3 = os.path.join(test_dir, 'test_tiff2.tiff')
    im1 = imageio.imread(filename1)
    imageio.imwrite(filename3, im1)
    im3 = imageio.imread(filename3)
    assert im1.ndim == 3
    assert im1.shape == im3.shape
    assert (im1 == im3).all()
    
    # Ensure imread + imwrite works round trip - volume like
    filename3 = os.path.join(test_dir, 'test_tiff2.tiff')
    im1 = imageio.volread(filename1)
    imageio.volwrite(filename3, im1)
    im3 = imageio.volread(filename3)
    assert im1.ndim == 4
    assert im1.shape == im3.shape
    assert (im1 == im3).all()

    # Read metadata
    md = imageio.get_reader(filename2).get_meta_data()
    assert md['is_imagej'] is None
    assert md['description'] == 'shape=(2,3,10,10)'
    assert md['description1'] == ''
    assert md['datetime'] == datetime.datetime(2015, 5, 9, 9, 8, 29)
    assert md['software'] == 'tifffile.py'

    # Write metadata
    dt = datetime.datetime(2018, 8, 6, 15, 35, 5)
    w = imageio.get_writer(filename1, software='testsoftware')
    w.append_data(np.zeros((10, 10)), meta={'description': 'test desc',
                                            'datetime': dt})
    w.close()
    r = imageio.get_reader(filename1)
    md = r.get_meta_data()
    assert 'datetime' in md
    assert md['datetime'] == dt
    assert 'software' in md
    assert md['software'] == 'testsoftware'
    assert 'description' in md
    assert md['description'] == 'test desc'
Esempio n. 39
0
def test_functions():
    """ Test the user-facing API functions """
    
    # Test help(), it prints stuff, so we just check whether that goes ok
    imageio.help()  # should print overview
    imageio.help('PNG')  # should print about PNG
    
    fname1 = get_remote_file('images/chelsea.png', test_dir)
    fname2 = fname1[:-3] + 'jpg'
    fname3 = fname1[:-3] + 'notavalidext'
    open(fname3, 'wb')
    
    # Test read()
    R1 = imageio.read(fname1)
    R2 = imageio.read(fname1, 'png')
    assert R1.format is R2.format
    # Fail
    raises(ValueError, imageio.read, fname3)  # existing but not readable
    raises(IOError, imageio.read, 'notexisting.barf')
    raises(IndexError, imageio.read, fname1, 'notexistingformat')
    
    # Test save()
    W1 = imageio.save(fname2)
    W2 = imageio.save(fname2, 'JPG')
    assert W1.format is W2.format
    # Fail
    raises(ValueError, imageio.save, 'wtf.notexistingfile')
    
    # Test imread()
    im1 = imageio.imread(fname1)
    im2 = imageio.imread(fname1, 'png')
    assert im1.shape[2] == 3
    assert np.all(im1 == im2)
    
    # Test imsave()
    if os.path.isfile(fname2):
        os.remove(fname2)
    assert not os.path.isfile(fname2)
    imageio.imsave(fname2, im1[:, :, 0])
    imageio.imsave(fname2, im1)
    assert os.path.isfile(fname2)
    
    # Test mimread()
    fname3 = get_remote_file('images/newtonscradle.gif', test_dir)
    ims = imageio.mimread(fname3)
    assert isinstance(ims, list)
    assert len(ims) > 1
    assert ims[0].ndim == 3
    assert ims[0].shape[2] in (1, 3, 4)
    
    if IS_PYPY:
        return  # no support for npz format :(
    
    # Test mimsave()
    fname5 = fname3[:-4] + '2.npz'
    if os.path.isfile(fname5):
        os.remove(fname5)
    assert not os.path.isfile(fname5)
    imageio.mimsave(fname5, [im[:, :, 0] for im in ims])
    imageio.mimsave(fname5, ims)
    assert os.path.isfile(fname5)
    
    # Test volread()
    fname4 = get_remote_file('images/stent.npz', test_dir)
    vol = imageio.volread(fname4)
    assert vol.ndim == 3
    assert vol.shape[0] == 256
    assert vol.shape[1] == 128
    assert vol.shape[2] == 128
    
    # Test volsave()
    volc = np.zeros((10, 10, 10, 3), np.uint8)  # color volume
    fname6 = fname4[:-4] + '2.npz'
    if os.path.isfile(fname6):
        os.remove(fname6)
    assert not os.path.isfile(fname6)
    imageio.volsave(fname6, volc)
    imageio.volsave(fname6, vol)
    assert os.path.isfile(fname6)
    
    # Test mvolread()
    vols = imageio.mvolread(fname4)
    assert isinstance(vols, list)
    assert len(vols) == 1
    assert vols[0].shape == vol.shape
    
    # Test mvolsave()
    if os.path.isfile(fname6):
        os.remove(fname6)
    assert not os.path.isfile(fname6)
    imageio.mvolsave(fname6, [volc, volc])
    imageio.mvolsave(fname6, vols)
    assert os.path.isfile(fname6)
    
    # Fail for save functions
    raises(ValueError, imageio.imsave, fname2, np.zeros((100, 100, 5)))
    raises(ValueError, imageio.imsave, fname2, 42)
    raises(ValueError, imageio.mimsave, fname5, [np.zeros((100, 100, 5))])
    raises(ValueError, imageio.mimsave, fname5, [42])
    raises(ValueError, imageio.volsave, fname4, np.zeros((100, 100, 100, 40)))
    raises(ValueError, imageio.volsave, fname4, 42)
    raises(ValueError, imageio.mvolsave, fname4, [np.zeros((90, 90, 90, 40))])
    raises(ValueError, imageio.mvolsave, fname4, [42])