本文整理汇总了Python中tifffile.imread函数的典型用法代码示例。如果您正苦于以下问题:Python imread函数的具体用法?Python imread怎么用?Python imread使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了imread函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_dark
def remove_dark(A, folder):
"""
This function will subtract the dark files from the data files
Parameters
----------
A : list
list of tiff files
Returns
-------
clean_data : array
dark subtracted data , clean data
shape (number of clean images, detectore shape 0, detecotor shape 1)
"""
clean_data_arr = [] # save the cleaned data
for name in A:
if "dark" in name: # check the dark files
dark_data = imread(name)
print ("+++++ bad", name)
else:
arr = imread(name)
print ("good", name)
# clean the data
clean_data = arr - dark_data
#print (os.path.join(name))
imsave(name, clean_data)
clean_data_arr.append(clean_data)
return np.asarray(clean_data_arr)
示例2: __init__
def __init__(self, file_path):
self.file_path = file_path
if isinstance(file_path, str):
self.data = tifffile.imread(self.file_path)
elif any([isinstance(file_path, t) for t in [np.ndarray,list]]):
data = [tifffile.imread(f) for f in self.file_path if 'tif' in f]
self.data = np.concatenate([d if d.ndim==3 else [d] for d in data], axis=0)
示例3: get_new_images
def get_new_images(self, temp_file_list):
new_pics = []
if temp_file_list is not None:
for i in temp_file_list:
self.pic_list.append(imread(self._directory_name + i))
new_pics.append(imread(self._directory_name + i))
return temp_file_list, new_pics
示例4: imreadStack
def imreadStack(filenameList):
"""Simple wrapper to read a list of image series tiffs into a stack.
Note that this function assumes all images are the same size.
We tend to work with single channel tiff files, and as such use tifffile's imread function.
We've wrapped tifffiles read function to account for the differences in default
image dimension ordering. By convention, we use x,y,frame. The major advantages of
tifffile are 1) speed and 2) the ability to read multiframe tiffs.
:param filenameList: list of strings representing the files to load
:returns: 4d numpy array
"""
firstImageSeries = tifffile.imread(filenameList[0])
if len(firstImageSeries.shape) == 3:
firstImageSeries=np.transpose(firstImageSeries, [1,2,0])
imageStack = np.zeros((firstImageSeries.shape[0], firstImageSeries.shape[1], firstImageSeries.shape[2], len(filenameList)))
for i, fileName in enumerate(filenameList):
array=tifffile.imread(fileName)
if len(array.shape) == 3:
array=np.transpose(array, [1,2,0])
imageStack[:,:,:,i] = array
return imageStack
示例5: get_images
def get_images(imageId, img_key = None):
'''
Load images correspoding to imageId
Parameters
----------
imageId : str
imageId as used in grid_size.csv
img_key : {None, '3', 'A', 'M', 'P'}, optional
Specify this to load single image
None loads all images and returns in a dict
'3' loads image from three_band/
'A' loads '_A' image from sixteen_band/
'M' loads '_M' image from sixteen_band/
'P' loads '_P' image from sixteen_band/
Returns
-------
images : dict
A dict of image data from TIFF files as numpy array
'''
img_names = get_image_names(imageId)
images = dict()
if img_key is None:
for k in img_names.keys():
images[k] = tiff.imread(img_names[k])
else:
images[img_key] = tiff.imread(img_names[img_key])
return images
示例6: main
def main(vol_fname='', label_fname='', dataset_name='', percent_test=0,
normalize_mean=False):
print "Reading data..."
vol = tifffile.imread(vol_fname)
label = tifffile.imread(label_fname)
if zero_mean:
vol = zero_mean(vol)
if len(label.shape) > 3 and label.shape[3] == 3:
print "Converting label to binary..."
label = rgb2bin(label)
#Splitting into training and test sets
train, label_train, test, label_test = split_data(vol, label, percent_test)
#Transpose
train = train.transpose(0,2,1)
test = test.transpose(0,2,1)
label_train = label_train.transpose(0,2,1)
label_test = label_test.transpose(0,2,1)
s_train = train.shape
s_test = test.shape
print "Saving data in znn format..."
#Making the necessary directories
os.makedirs('dataset/{}/data/'.format(dataset_name))
os.makedirs('dataset/{}/spec/'.format(dataset_name))
#Save as znn format
train_outname = "dataset/{0}/data/batch{1}.image".format(dataset_name, 1)
label_train_outname = "dataset/{0}/data/batch{1}.label".format(dataset_name, 1)
if percent_test > 0:
test_outname = "dataset/{0}/data/batch{1}.image".format(dataset_name, 2)
label_test_outname = "dataset/{0}/data/batch{1}.label".format(dataset_name, 2)
emirt.io.znn_img_save(train.astype('double'), train_outname)
emirt.io.znn_img_save(label_train.astype('double'), label_train_outname)
if percent_test > 0:
emirt.io.znn_img_save(test.astype('double'), test_outname)
emirt.io.znn_img_save(label_test.astype('double'), label_test_outname)
#Prepare a spec file
print "Writing spec file..."
write_spec_file(dataset_name, 1, s_train)
if percent_test > 0:
write_spec_file(dataset_name, 2, s_test)
示例7: readData
def readData(filename, x = all, y = all, z = all, **args):
"""Read data from a single tif image or stack
Arguments:
filename (str): file name as regular expression
x,y,z (tuple): data range specifications
Returns:
array: image data
"""
dsize = dataSize(filename);
#print "dsize %s" % str(dsize);
if len(dsize) == 2:
data = tiff.imread(filename, key = 0);
#print "data.shape %s" % str(data.shape);
return io.dataToRange(data.transpose([1,0]), x = x, y = y);
#return io.dataToRange(data, x = x, y = y);
else:
if z is all:
data = tiff.imread(filename);
if data.ndim == 2:
# data = data
data = data.transpose([1,0]);
elif data.ndim == 3:
#data = data.transpose([1,2,0]);
data = data.transpose([2,1,0]);
elif data.ndim == 4: # multi channel image
#data = data.transpose([1,2,0,3]);
data = data.transpose([2,1,0,3]);
else:
raise RuntimeError('readData: dimension %d not supproted!' % data.ndim)
return io.dataToRange(data, x = x, y = y, z = all);
else: #optimize for z ranges
ds = io.dataSizeFromDataRange(dsize, x = x, y = y, z = z);
t = tiff.TiffFile(filename);
p = t.pages[0];
data = numpy.zeros(ds, dtype = p.dtype);
rz = io.toDataRange(dsize[2], r = z);
#print "test"
#print rz;
#print dsize
for i in range(rz[0], rz[1]):
xydata = t.pages[i].asarray();
#data[:,:,i-rz[0]] = io.dataToRange(xydata, x = x, y = y);
data[:,:,i-rz[0]] = io.dataToRange(xydata.transpose([1,0]), x = x, y = y);
return data
示例8: __init__
def __init__(self,infile,rawfile,verbosity=1):
self.verbosity = verbosity;
# read images
self.IN =tiff.imread(infile,verbosity=self.verbosity);
self.INRAW=tiff.imread(rawfile,verbosity=self.verbosity);
# image parameters (TODO: read scale from Tiff?)
self.info = { 'desc': infile.split('/')[-1],
'filename': infile,
'atoms' : 'C' };
示例9: baboon
def baboon(size=512, dtype='float32'):
"""
Load test baboon image array.
Parameters
----------
size : int or tuple of int, optional
Size of the output image.
dtype : str, optional
The desired data-type for the array.
Returns
-------
ndarray
Output 3D test image.
"""
size = _totuple(size, 2)
fname = os.path.join(DATA_PATH, 'baboon.tif')
im = tifffile.imread(fname)
im = skimage.transform.resize(im, size, order=3,
preserve_range=True, mode='constant',
**resize_kwargs)
im = np.expand_dims(im, 0)
im = im.astype(dtype)
return im
示例10: singletiff2multidicom
def singletiff2multidicom(in_files, dicomdir, plans, out_prefix):
import DicomIO
import numpy as np
import os
import shutil
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import tifffile as tiff
outdir = experiment_dir + '/' + out_prefix + '/' + dicomdir
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
shutil.rmtree(outdir)
os.makedirs(outdir)
# Resolve new frame list
out_vols = plans
for file_i in range(len(in_files)):
print "Reading " + in_files[file_i]
ds = tiff.imread(in_files[file_i])
no_slices = ds.shape[0]
for z_i in range(no_slices):
out_vols[file_i][z_i].PixelData = ds[z_i].astype(np.uint16).tostring()
dcmio = DicomIO.DicomIO()
filenames = dcmio.WriteDICOM_frames(outdir, out_vols, 'IM')
return outdir, filenames
示例11: optimize_z
def optimize_z(x,y,z,image,n=None):
"""Optimize z for poly fit"""
if type(image) == str:
img = tf.imread(image)
elif type(image) == np.ndarray:
img = image
data_z = img[:,y,x]
if n is None:
n = getn(data_z)
x_opt_vals, y_opt_vals, z_opt_vals = [], [], []
x_opt,y_opt,z_opt = x,y,z
for i in range(5):
try:
print x_opt,y_opt,z_opt
x_opt,y_opt,z_opt = int(round(x_opt)),int(round(y_opt)),int(round(z_opt))
x_opt, y_opt = optimize_xy(x_opt,y_opt,z_opt,img,nx=None,ny=None)
data_z = img[:,round(y_opt),round(x_opt)]
except Exception as e:
if clrmsg and debug is True: print clrmsg.ERROR
print IndexError("Optimization failed, possibly due to low signal or low SNR. "+str(e))
return [x],[y],['failed']
n = getn(data_z)
z_opt, data_z_yp_poly = parabolic.parabolic_polyfit(data_z, np.argmax(data_z), n)
x_opt_vals.append(x_opt)
y_opt_vals.append(y_opt)
z_opt_vals.append(z_opt)
return x_opt_vals, y_opt_vals, z_opt_vals
示例12: relabel_volume
def relabel_volume(dir, outdir):
files = sorted(os.listdir(dir))
out = None
out_is_there = False
for f in files:
i = tif.imread(os.path.join(dir,f))
if (out_is_there):
out = numpy.dstack([out, i])
else:
out = i
out_is_there = True
print '3d volume', out.shape
import skimage
from skimage.segmentation import relabel_sequential
relabeled,fm,im = skimage.segmentation.relabel_sequential(out)
print 'Max', relabeled.max()
for z in range(relabeled.shape[2]):
tif.imsave(os.path.join(outdir,str(z)+'.tif'),relabeled[:,:,z].astype(numpy.uint32))
print 'stored', z
示例13: find
def find(args):
from sys import stdout
from tifffile import imread
image = imread(str(args.image)).astype('float32')
scale = asarray(args.scale) if args.scale else ones(image.ndim, dtype='int')
blobs = findBlobs(image, range(*args.size), args.threshold)[:, 1:] # Remove scale
blobs = blobs[peakEnclosed(blobs, shape=image.shape, size=args.edge)]
blobs = blobs[:, ::-1] # Reverse to xyz order
blobs = blobs * scale
if args.format == "pickle":
from pickle import dump, HIGHEST_PROTOCOL
from functools import partial
dump = partial(dump, protocol=HIGHEST_PROTOCOL)
dump(blobs, stdout.buffer)
else:
import csv
if args.format == 'txt':
delimiter = ' '
elif args.format == 'csv':
delimiter = ','
writer = csv.writer(stdout, delimiter=delimiter)
for blob in blobs:
writer.writerow(blob)
示例14: loadTiffStack
def loadTiffStack(fname,useLibTiff=False):
"""
Read a TIFF stack.
We're using tifflib by default as, right now, only this works when the application is compile on Windows. [17/08/15]
Bugs: known to fail with tiffs produced by Icy [23/07/15]
"""
if not os.path.exists(fname):
print "imageStackLoader.loadTiffStack can not find %s" % fname
return
purePython = True
if useLibTiff:
from libtiff import TIFFfile
import numpy as np
tiff = TIFFfile(fname)
samples, sample_names = tiff.get_samples() #we should have just one
print "Loading:\n" + tiff.get_info() + " with libtiff\n"
im = np.asarray(samples[0])
else:
print "Loading:\n" + fname + " with tifffile\n"
from tifffile import imread
im = imread(fname)
im=im.swapaxes(1,2)
print "read image of size: cols: %d, rows: %d, layers: %d" % (im.shape[1],im.shape[2],im.shape[0])
return im
示例15: imread
def imread(_path):
""" allow loading jpg, png as well as tif
"""
if os.path.splitext(_path)[1] in ['.tiff', '.tif']:
return tiff.imread(_path)
else:
return cv2.imread(_path,0)