def test_manual_image_creation_from_file(self): from jicbioimage.core.image import Image # Preamble: let us define the path to a TIFF file and create a numpy # array from it. # from libtiff import TIFF # tif = TIFF.open(path_to_tiff, 'r') # ar = tif.read_image() path_to_tiff = os.path.join(DATA_DIR, 'single-channel.ome.tif') use_plugin('freeimage') ar = imread(path_to_tiff) # It is possible to create an image from a file. image = Image.from_file(path_to_tiff) self.assertEqual(len(image.history), 0) self.assertEqual(image.history.creation, 'Created Image from {}'.format(path_to_tiff)) # With name... image = Image.from_file(path_to_tiff, name='Test1') self.assertEqual(image.history.creation, 'Created Image from {} as Test1'.format(path_to_tiff)) # Without history... image = Image.from_file(path_to_tiff, log_in_history=False) self.assertEqual(len(image.history), 0) # It is worth noting the image can support more multiple channels. # This is particularly important when reading in images in rgb format. fpath = os.path.join(DATA_DIR, 'tjelvar.png') image = Image.from_file(fpath) self.assertEqual(image.shape, (50, 50, 3))
def Imagetransform(srcfolder, destfolder, mode): if not os.path.exists(srcfolder): raise Exception('input srcfolder does not exists! ') if not os.path.exists(destfolder): os.makedirs(destfolder) filelist = os.listdir(srcfolder) count = len(filelist) varlist = np.random.randint(5, 51, count) IO.use_plugin('pil') # SET the specific plugin, default: imgio for i, sfile in enumerate(filelist): print("{}, {}".format(i, varlist[i])) filename = srcfolder + '\\' + sfile mat = IO.imread(filename) # mat = data.load(filename) if len(mat.shape) < 3: continue w, h, c = mat.shape if h < 64 or w < 64: continue noimat = add_noise(mat, mode, 50) # varlist[i] # plt.figure('noi') # plt.imshow(noimat, interpolation='nearest') # plt.show() outfile = destfolder + '\\' + sfile IO.imsave(outfile, noimat)
def __init__(self, seg_dir, cell_wall_dir, measurement_dir, out_dir, out_prefix): use_plugin('freeimage') self.reconstructed_cells = [] self.selected_points = [] self.segmentation_maps = load_segmentation_maps(seg_dir) self.cell_wall_images = self.get_images(cell_wall_dir) self.measurement_images = self.get_images(measurement_dir) self.out_dir = out_dir self.out_prefix = out_prefix self.segment_me_dir = os.path.join(out_dir, 'segment_me') self.answer_dir = os.path.join(out_dir, 'answers') for d in (self.out_dir, self.segment_me_dir, self.answer_dir): if not os.path.isdir(d): os.mkdir(d) self.xdim, self.ydim = self.measurement_images[0].shape start = time() self.reconstruction = Reconstruction(self.segmentation_maps, start=0) for z in range(0, len(self.segmentation_maps) - 1): self.reconstruction.extend(z) elapsed = (time() - start) / 60 print('Reconstruction done {} minutes.'.format(elapsed))
def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that SimpleITK is used. """ use_plugin('simpleitk')
def load_data_from_tif(raw_tiff_name, pixel_size=2048, rotate=True): """ Load data from TIFF :param raw_tiff_name: :param pixel_size :param rotate: :return: """ ImageData = Image.open(raw_tiff_name) # im = img_as_uint(np.array(ImageData)) io.use_plugin('freeimage') image_2d_data = np.array(ImageData, dtype=np.int32) print(image_2d_data.shape, type(image_2d_data), image_2d_data.min(), image_2d_data.max()) # image_2d_data.astype(np.uint32) image_2d_data.astype(np.float64) if rotate: image_2d_data = image_2d_data.transpose() # Merge data if required if pixel_size == 1024: counts_vec = image_2d_data[::2, ::2] + image_2d_data[::2, 1::2] + \ image_2d_data[1::2, ::2] + image_2d_data[1::2, 1::2] pixel_type = '1K' # print (DataR.shape, type(DataR)) else: # No merge counts_vec = image_2d_data pixel_type = '2K' counts_vec = counts_vec.reshape((pixel_size * pixel_size, )) print('Minimum counts (on pixels) = ', counts_vec.min()) return counts_vec, pixel_type
def _find_working_imread(modes=IMREAD_MODES): "Finds an image-reading mode that works; returns the name and a function." if isinstance(modes, str_types): modes = [modes] for mode in modes: try: if mode.startswith('skimage-'): from skimage.io import use_plugin, imread use_plugin(mode[len('skimage-'):]) elif mode == 'cv2': import cv2 def imread(f): img = cv2.imread(f) if img.ndim == 3: b, g, r = np.rollaxis(img, axis=-1) return np.dstack([r, g, b]) return img elif mode == 'matplotlib': import matplotlib.pyplot as mpl imread = lambda f: mpl.imread(f)[::-1] return mode, imread except ImportError: pass else: raise ImportError("couldn't import any of {}".format(', '.join(modes)))
def main(): parser = argparse.ArgumentParser() parser.add_argument('input', help="Input image") parser.add_argument('outdir', help="Output directory") parser.add_argument('--mask', help="Mask image") parser.add_argument('--delay', help="Delay between slides") parser.add_argument( '--sort', choices=["avgcolor", "centroidcolor", "dist", "random"], default="random", help="Region sorting method") # TODO: Generate slideshow args = parser.parse_args() use_plugin('gtk') use_plugin('pil') segment_image(args.input, args.outdir, map_sort_function(args.sort), args.mask, show_images=True) generate_slideshow(args.outdir, '{d}/slideshow.txt'.format(d=args.outdir), args.delay)
def single_transform(self, image_path, transform_choice, file_folder="SynthesizedImages", repeat=1): """ Call this function to generate a synthesized images based on requested transform\n :param image_path: File path of image to transform\n :param transform_choice: The name of the transform function to apply\n :param file_folder: OPTIONAL, File folder path (not including image file) where synthesized images are to be stored :param repeat: OPTIONAL, integer, number of times to apply single transform """ # Sets plugin for skimage, using PIL to keep read in image formats the same for arrays io.use_plugin('pil') img = Image.open(image_path) # Transform image transformed = img.copy() for count in range(int(repeat)): transformed = self.transform_chooser(transformed, transform_choice) # Save image to new file image_file_name = "synth_" + self.get_image_name(image_path) + ".jpg" file_name = os.path.join(file_folder, image_file_name) transformed.save(file_name, "JPEG")
def randomizer(self, image_path, file_folder="SynthesizedImages", num_of_images=random.randint(1, 10), num_of_transforms=random.randint(1, 6)): """ Call this function to generate a set of random synthesized images\n :param image_path: File path of image to transform\n :param file_folder: OPTIONAL, File folder path (not including image file) where synthesized images are to be stored\n :param num_of_images: OPTIONAL, integer, the number of synthesized images\n :param num_of_transforms: OPTIONAL, integer, the number of transforms to apply to each image """ # Sets plugin for skimage, using PIL to keep read in image formats the same for arrays io.use_plugin('pil') img = Image.open(image_path) for count, images in enumerate(range(num_of_images), 1): # The number of transformations that will be applied transformed = img.copy() for operations in range(0, num_of_transforms): # The transformation function to be applied transformed = self.transform_chooser(transformed) # Save image to new file image_file_name = "new_" + self.get_image_name( image_path) + "_" + str(count) + ".jpg" file_name = os.path.join(file_folder, image_file_name) transformed.save(file_name, "JPEG")
def test_imread_collection_single_MEF(): io.use_plugin('fits') testfile = os.path.join(data_dir, 'multi.fits') ic1 = io.imread_collection(testfile) ic2 = io.ImageCollection([(testfile, 1), (testfile, 2), (testfile, 3)], load_func=fplug.FITSFactory) assert _same_ImageCollection(ic1, ic2)
def apply_mask(input_file, mask_file, output_file): use_plugin('pil') # TODO - shape mismatches input_image = imread(input_file) mask_image = imread(mask_file) logger.info('Input image shape: {}'.format(input_image.shape)) logger.info('Mask image shape: {}'.format(mask_image.shape)) xdim, ydim, _ = input_image.shape output_image = np.zeros((xdim, ydim), np.uint8) mask_xs, mask_ys = np.where(mask_image == 255) mask_zs = np.zeros(mask_xs.shape, np.uint8) mask_locations = mask_xs, mask_ys, mask_zs output_locations = (mask_xs, mask_ys) output_image[output_locations] = input_image[mask_locations] imsave(output_file, output_image)
def __init__(self, seg_dir, cell_wall_dir, measurement_dir, out_dir, out_prefix): use_plugin('freeimage') self.reconstructed_cells = [] self.selected_points = [] self.segmentation_maps = load_segmentation_maps(seg_dir) self.cell_wall_images = self.get_images(cell_wall_dir) self.measurement_images = self.get_images(measurement_dir) self.out_dir = out_dir self.out_prefix = out_prefix self.segment_me_dir = os.path.join(out_dir, 'segment_me') self.answer_dir = os.path.join(out_dir, 'answers') for d in (self.out_dir, self.segment_me_dir, self.answer_dir): if not os.path.isdir(d): os.mkdir(d) self.xdim, self.ydim = self.measurement_images[0].shape start = time() self.reconstruction = Reconstruction(self.segmentation_maps, start=0) for z in range(0, len(self.segmentation_maps)-1): self.reconstruction.extend(z) elapsed = ( time() - start ) / 60 print('Reconstruction done {} minutes.'.format(elapsed))
def flip_rotate(prep_id, tif): io.use_plugin('tifffile') INPUT = os.path.join(DATA_ROOT, prep_id, TIF) OUTPUT = os.path.join(DATA_ROOT, prep_id, ROTATED) input_tif = os.path.join(INPUT, tif) output_tif = os.path.join(OUTPUT, tif) try: img = io.imread(input_tif) except: return 'Bad file size' try: img = np.rot90(img, 1) except: print('could not rotate', tif) try: img = np.fliplr(img) except: print('could not flip', tif) try: img = img_as_uint(img) except: print('could not convert to 16bit', tif) try: io.imsave(output_tif, img) except: print('Could not save {}'.format(output_tif)) return " Flipped and rotated"
def test_fits_plugin_import(): # Make sure we get an import exception if Astropy isn't there # (not sure how useful this is, but it ensures there isn't some other # error when trying to load the plugin) try: io.use_plugin('fits') except ImportError: raise()
def test_imread_collection_MEF_and_simple(): io.use_plugin('fits') testfile1 = os.path.join(data_dir, 'multi.fits') testfile2 = os.path.join(data_dir, 'simple.fits') ic1 = io.imread_collection([testfile1, testfile2]) ic2 = io.ImageCollection([(testfile1, 1), (testfile1, 2), (testfile1, 3), (testfile2, 0)], load_func=fplug.FITSFactory) assert _same_ImageCollection(ic1, ic2)
def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that PIL is used. """ try: use_plugin('pil') except ImportError: pass
def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that sitk is used. """ try: use_plugin('simpleitk') except ImportError: pass
def ensemble_average_images(dirin, pattern, filout): """A function to do an ensemble average of images and a spatial average of this ensemble average Parameters ---------- dirin: str . Input directory pattern: str Input Pattern images to average filout: str Output filename """ #to be able to read high precision images (e.g. 16bits) io.use_plugin('freeimage') #im=io.imread("B00001_0.tif",as_grey=True) #plt.imshow(im) #plt.show() #pattern="B0*_0.tif" #dirin='/media/HDImage/SMARTEOLE/PIV-Nov2015-TIF/PIV_10ms_000lmin_05deg_z539mm_dt35us_1000im/' # list automatically the file names list_image = sorted( glob.glob(os.path.join(os.path.abspath(dirin), pattern))) # Read this list imlist = io.imread_collection(list_image) #not possible to do the average like this: meanimg=sum(imlist)/len(imlist) #plt.imshow(imlist[0],clim=(0,1000),cmap=cm.gray) #plt.show() # Accumulation of pixel value in float type (to avoid saturation) fimlist = imlist[0].astype(float) for i in range(len(imlist) - 1): fimlist += imlist[i + 1].astype(float) #plt.hist(fimlist[0].ravel(),bins=10000,range=(30,5500)) #plt.show() #mean value with float rounded with np.rint() meanimg_ens = np.rint(fimlist / len(imlist)) #convert to integer meanimg_int = meanimg_ens.astype(np.uint16) #mean spatial value rounded with np.rint() meanimg_space = np.rint(np.mean(meanimg_ens)) #convert to integer meanimg_space = meanimg_space.astype(np.uint16) # Dimensionless mean value mean_out = (np.rint(meanimg_ens / np.mean(meanimg_ens))).astype(np.uint16) #plt.imshow(meanimg,clim=(0,100),cmap=cm.gray) #plt.show() #io.imsave(filout,meanimg_int) # save in a file io.imsave(filout, mean_out) return mean_out
def write_16bit_png_mpl(float_image, dir, name): from skimage import io, exposure, img_as_uint, img_as_float # Use pypng to write z as a color PNG. png_save_loc = '{}/{}.png'.format(dir, name) with open(png_save_loc, 'wb') as f: io.use_plugin('freeimage') im = exposure.rescale_intensity(float_image, out_range='float') im = img_as_uint(im) io.imsave(png_save_loc, im)
def ensemble_average_images(dirin,pattern,filout): """A function to do an ensemble average of images and a spatial average of this ensemble average Parameters ---------- dirin: str . Input directory pattern: str Input Pattern images to average filout: str Output filename """ #to be able to read high precision images (e.g. 16bits) io.use_plugin('freeimage') #im=io.imread("B00001_0.tif",as_grey=True) #plt.imshow(im) #plt.show() #pattern="B0*_0.tif" #dirin='/media/HDImage/SMARTEOLE/PIV-Nov2015-TIF/PIV_10ms_000lmin_05deg_z539mm_dt35us_1000im/' # list automatically the file names list_image=sorted( glob.glob( os.path.join( os.path.abspath(dirin), pattern ) ) ) # Read this list imlist=io.imread_collection(list_image) #not possible to do the average like this: meanimg=sum(imlist)/len(imlist) #plt.imshow(imlist[0],clim=(0,1000),cmap=cm.gray) #plt.show() # Accumulation of pixel value in float type (to avoid saturation) fimlist=imlist[0].astype(float) for i in range(len(imlist)-1): fimlist += imlist[i+1].astype(float) #plt.hist(fimlist[0].ravel(),bins=10000,range=(30,5500)) #plt.show() #mean value with float rounded with np.rint() meanimg_ens=np.rint(fimlist/len(imlist)) #convert to integer meanimg_int=meanimg_ens.astype(np.uint16) #mean spatial value rounded with np.rint() meanimg_space=np.rint(np.mean(meanimg_ens)) #convert to integer meanimg_space=meanimg_space.astype(np.uint16) # Dimensionless mean value mean_out=(np.rint(meanimg_ens/np.mean(meanimg_ens))).astype(np.uint16) #plt.imshow(meanimg,clim=(0,100),cmap=cm.gray) #plt.show() #io.imsave(filout,meanimg_int) # save in a file io.imsave(filout,mean_out) return mean_out;
def total_error(folder: str, truth_path: str): use_plugin('matplotlib') results = imread_collection(folder + '/*.png') imgs = [img_as_float(img) for img in results] truth = rescale(imread(truth_path), 0.24) print(truth.shape) height, width, channels = truth.shape for img in imgs: print(np.sum(np.linalg.norm(np.reshape(truth, [width*height, channels]) - np.reshape(img, [width*height, channels]), axis=1)) / (width*height))
def test_fits_plugin_import(): # Make sure we get an import exception if PyFITS isn't there # (not sure how useful this is, but it ensures there isn't some other # error when trying to load the plugin) try: io.use_plugin('fits') except ImportError: assert pyfits_available == False else: assert pyfits_available == True
def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that freeimage is used. """ try: sio.use_plugin('freeimage') except RuntimeError: pass
def reconstruct_and_measure(seg_dir, measure_dir, out_dir, results_file, start_z, end_z): logger.info('Segmentation dir: {}'.format(seg_dir)) logger.info('Measurement dir: {}'.format(measure_dir)) logger.info('Output dir: {}'.format(out_dir)) logger.info('Results file: {}'.format(results_file)) use_plugin('freeimage') smaps = load_segmentation_maps(seg_dir) idata = load_intensity_data(measure_dir) xdim, ydim = idata[0].shape if start_z is None: start_z = 0 if end_z is None: end_z = len(smaps)-1 # Minus 1 is intentional; need to be able to extend one more z-stack logger.info('Start z: {:d}'.format(start_z)) logger.info('End z: {:d}'.format(end_z)) r = Reconstruction(smaps, start=start_z) logger.debug('Reconstruction instance: {}'.format(r)) for z in range(start_z, end_z): r.extend(z) rcells = r.cells_larger_then(3) # Write the mask images mask_fpaths = get_mask_output_fpaths(seg_dir, out_dir, start_z, end_z) generate_reconstruction_mask(mask_fpaths, xdim, ydim, rcells, start_z, end_z) # Calculate total area sum_segmentation_area = sum_segmentation_dir(seg_dir) with open(results_file, "w") as f: f.write('mean_intensity,quartile_intensity,best_intensity,best_z,x,y,z,volume,zext,sum_seg_area\n') for rcell in rcells: x, y, z = rcell.centroid mean_intensity = rcell.measure_mean_intensity(idata) quartile_intensity = rcell.measure_quartile_intensity(idata) best_intensity, best_z = rcell.measure_best_slice(idata) volume = rcell.pixel_area zext = rcell.z_extent f.write("{},{},{},{},{},{},{},{},{},{}\n".format( mean_intensity, quartile_intensity, best_intensity, best_z, x, y, z, volume, zext, sum_segmentation_area))
def main(): import skimage.io as io import sys if len(sys.argv) != 2: print "Usage: skivi <image-file>" sys.exit(-1) io.use_plugin('qt') io.imshow(io.imread(sys.argv[1]), fancy=True) io.show()
def open(self): #http://flockhart.virtualave.net/RBIF0100/regexp.html if not self.filename.endswith(".pbm"): io.use_plugin("freeimage") self.data = io.imread(self.filename) #self.data = self.data.astype("float64") self.sizex = self.data.shape[0] self.sizey = self.data.shape[1] if self.filename.endswith("ppm"): self.color = True return self.data
def _import_skimage_io(): """ To import skimage io only when it is/can be used """ try: from skimage import io as skio skio.use_plugin('freeimage') except ImportError as exc: raise ImportError("Could not find the package skimage, its subpackage " "io and the pluging freeimage which are required to support " "several image formats. Error details: {0}".format(exc)) return skio
def reconstruct_and_measure(seg_dir, measure_dir, out_dir, results_file, start_z, end_z): logger.info('Segmentation dir: {}'.format(seg_dir)) logger.info('Measurement dir: {}'.format(measure_dir)) logger.info('Output dir: {}'.format(out_dir)) logger.info('Results file: {}'.format(results_file)) use_plugin('freeimage') smaps = load_segmentation_maps(seg_dir) idata = load_intensity_data(measure_dir) xdim, ydim = idata[0].shape if start_z is None: start_z = 0 if end_z is None: end_z = len( smaps ) - 1 # Minus 1 is intentional; need to be able to extend one more z-stack logger.info('Start z: {:d}'.format(start_z)) logger.info('End z: {:d}'.format(end_z)) r = Reconstruction(smaps, start=start_z) logger.debug('Reconstruction instance: {}'.format(r)) for z in range(start_z, end_z): r.extend(z) rcells = r.cells_larger_then(3) # Write the mask images mask_fpaths = get_mask_output_fpaths(seg_dir, out_dir, start_z, end_z) generate_reconstruction_mask(mask_fpaths, xdim, ydim, rcells, start_z, end_z) # Calculate total area sum_segmentation_area = sum_segmentation_dir(seg_dir) with open(results_file, "w") as f: f.write( 'mean_intensity,quartile_intensity,best_intensity,best_z,x,y,z,volume,zext,sum_seg_area\n' ) for rcell in rcells: x, y, z = rcell.centroid mean_intensity = rcell.measure_mean_intensity(idata) quartile_intensity = rcell.measure_quartile_intensity(idata) best_intensity, best_z = rcell.measure_best_slice(idata) volume = rcell.pixel_area zext = rcell.z_extent f.write("{},{},{},{},{},{},{},{},{},{}\n".format( mean_intensity, quartile_intensity, best_intensity, best_z, x, y, z, volume, zext, sum_segmentation_area))
def _import_skimage_io(): """ To import skimage io only when it is/can be used """ try: from skimage import io as skio skio.use_plugin('freeimage') except ImportError as exc: raise ImportError( "Could not find the package skimage, its subpackage " "io and the pluging freeimage which are required to support " "several image formats. Error details: {0}".format(exc)) return skio
def setUp(self): # This multipage TIF file was created with imagemagick: # convert im1.tif im2.tif -adjoin multipage.tif use_plugin('pil') paths = [os.path.join(data_dir, 'multipage_rgb.tif'), os.path.join(data_dir, 'no_time_for_that_tiny.gif')] self.imgs = [MultiImage(paths[0]), MultiImage(paths[0], conserve_memory=False), MultiImage(paths[1]), MultiImage(paths[1], conserve_memory=False), ImageCollection(paths[0]), ImageCollection(paths[1], conserve_memory=False), ImageCollection(os.pathsep.join(paths))]
def generate_reconstruction_mask(out_fpaths, xdim, ydim, rcells, start_z, end_z): """Generate reconstruction mask.""" use_plugin('freeimage') das = {z: np.zeros((xdim, ydim, 3), dtype=np.uint8) for z in range(start_z, end_z+1)} # Plus one is intentional; end goes to z-1 for rcell in rcells: c = shades_of_jop_unique() for z, cellslice in rcell.slice_dict.items(): das[z][cellslice.coord_list] = c for out_fn, z in zip(out_fpaths, range(start_z, end_z+1)): # out_fn = os.path.join(out_dir, "da%d.tif" % z) imsave(out_fn, das[z])
def import_skimage_io(): """ To import skimage io only when it is/can be used """ try: from skimage import io as skio # tifffile works better on local, but not available on scarf # no plugin will use the default python imaging library (PIL) # This behaviour might need to be changed when switching to python 3 skio.use_plugin('tifffile') except ImportError as exc: raise ImportError( "Could not find the package skimage, its subpackage " "io and the pluging freeimage which are required to support " "several image formats. Error details: {0}".format(exc)) return skio
def load_data_from_tif(raw_tiff_name, pixel_size=2048, rotate=True): """ Load data from TIFF :param raw_tiff_name: :param pixel_size :param rotate: :return: """ from skimage import io from PIL import Image import numpy as np ImageData = Image.open(raw_tiff_name) # im = img_as_uint(np.array(ImageData)) io.use_plugin('freeimage') image_2d_data = np.array(ImageData, dtype=np.int32) print(image_2d_data.shape, type(image_2d_data), image_2d_data.min(), image_2d_data.max()) # image_2d_data.astype(np.uint32) image_2d_data.astype(np.float64) if rotate: image_2d_data = image_2d_data.transpose() # Merge data if required if pixel_size == 1024: counts_vec = image_2d_data[::2, ::2] + image_2d_data[::2, 1::2] + \ image_2d_data[1::2, ::2] + image_2d_data[1::2, 1::2] pixel_type = '1K' # print (DataR.shape, type(DataR)) else: # No merge counts_vec = image_2d_data pixel_type = '2K' counts_vec = counts_vec.reshape((pixel_size * pixel_size, )) print(counts_vec.min()) data_ws_name = os.path.basename(raw_tiff_name).split( '.')[0] + '_{}'.format(pixel_type) CreateWorkspace(DataX=np.zeros((pixel_size**2, )), DataY=counts_vec, DataE=np.sqrt(counts_vec), NSpec=pixel_size**2, OutputWorkspace=data_ws_name, VerticalAxisUnit='SpectraNumber') return data_ws_name, counts_vec
def load(image_name, as_gray=False): """Load an image file located in the data directory. Parameters ---------- image_name : string File name. as_gray : bool, optional (default : False) Convert to grayscale. Returns ------- image : ndarray Image loaded from ``pytracks.data_dir''. """ io.use_plugin('pil') return io.imread(path.join(data_dir, image_name), as_gray=as_gray)
def imgs(): use_plugin('pil') paths = [ testing.fetch('data/multipage_rgb.tif'), testing.fetch('data/no_time_for_that_tiny.gif') ] imgs = [ MultiImage(paths[0]), MultiImage(paths[0], conserve_memory=False), MultiImage(paths[1]), MultiImage(paths[1], conserve_memory=False), MultiImage(os.pathsep.join(paths)) ] yield imgs reset_plugins()
def load(f, as_grey=False): """Load an image file located in the data directory. Parameters ---------- f : string File name. as_grey : bool, optional Convert to greyscale. Returns ------- img : ndarray Image loaded from ``skimage.data_dir``. """ use_plugin('pil') return imread(os.path.join(assets, f), as_grey=as_grey)
from contextlib import contextmanager from numpy.testing import assert_equal, raises from skimage import io from skimage.io import manage_plugins io.use_plugin('pil') priority_plugin = 'pil' def setup_module(): manage_plugins.use_plugin('test') # see ../_plugins/test_plugin.py def teardown_module(): io.reset_plugins() @contextmanager def protect_preferred_plugins(): """Contexts where `preferred_plugins` can be modified w/o side-effects.""" preferred_plugins = manage_plugins.preferred_plugins.copy() try: yield finally: manage_plugins.preferred_plugins = preferred_plugins def test_read():
import os.path import numpy as np from numpy.testing import * from numpy.testing.decorators import skipif from tempfile import NamedTemporaryFile from skimage import data_dir from skimage.io import imread, imsave, use_plugin, reset_plugins from skimage._shared.six.moves import StringIO try: from PIL import Image from skimage.io._plugins.pil_plugin import _palette_is_grayscale use_plugin('pil') except ImportError: PIL_available = False else: PIL_available = True def teardown(): reset_plugins() def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that PIL is used. """
import os.path import numpy as np from numpy.testing.decorators import skipif from tempfile import NamedTemporaryFile from skimage import data_dir from skimage.io import imread, imsave, use_plugin, reset_plugins try: import SimpleITK as sitk use_plugin('simpleitk') except ImportError: sitk_available = False else: sitk_available = True def teardown(): reset_plugins() def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that sitk is used. """ try: use_plugin('simpleitk') except ImportError: pass
import collections import math import numpy from pathlib import Path import scipy.ndimage import scipy.ndimage.morphology import skimage.exposure import skimage.filters import skimage.measure import skimage.morphology import skimage.io as skio import sklearn.linear_model import sklearn.neighbors import sklearn.svm for function in ('imread', 'imsave', 'imread_collection'): skio.use_plugin('freeimage', function) import multiprocessing import os import pickle import sys supplementary_out_exclusion_mask = skio.imread(str(Path(os.path.expanduser('~')) / 'Data' / 'experiment01_a' / 'supplementary_out_exclusion_mask.png')) def select_random_in_mask_and_out_mask_coords(mask_fpath, coord_count, separation=None): im_mask = skio.imread(str(mask_fpath)) > 0 labels = skimage.measure.label(im_mask) regions = skimage.measure.regionprops(labels) if len(regions) == 0: raise RuntimeError('No regions found in mask image file "{}".'.format(str(mask_fpath))) # Lump together the coordinates of every lit mask pixel
def test_imread_MEF(): io.use_plugin('fits') testfile = os.path.join(data_dir, 'multi.fits') img = io.imread(testfile) assert np.all(img == pyfits.getdata(testfile, 1))
import os import skimage as si import skimage.io as sio import numpy as np from numpy.testing import * from numpy.testing.decorators import skipif from tempfile import NamedTemporaryFile try: import skimage.io._plugins.freeimage_plugin as fi FI_available = True sio.use_plugin('freeimage') except RuntimeError: FI_available = False np.random.seed(0) def setup_module(self): """The effect of the `plugin.use` call may be overridden by later imports. Call `use_plugin` directly before the tests to ensure that freeimage is used. """ try: sio.use_plugin('freeimage') except RuntimeError: pass
def setup(): use_plugin('pil')
def decafImages(src_path, socketid, output_path, result_path): # Establishing connection to send results and write messages from skimage import io from cloudcv17 import config import redis import json import caffe import numpy as np import os import glob import time import scipy.io as sio import traceback rs = redis.StrictRedis(host=config.REDIS_HOST, port=6379) try: # Needed to fix error https://github.com/BVLC/caffe/issues/438 io.use_plugin('matplotlib') # Caffe Initialisations CAFFE_DIR = os.path.normpath(os.path.join(os.path.dirname(caffe.__file__), "..", "..")) MODEL_FILE = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/deploy.prototxt') PRETRAINED = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel') # Set CPU mode caffe.set_mode_cpu() # Make classifier. classifier = caffe.Classifier(MODEL_FILE, PRETRAINED) # Find decaf features and send Results if os.path.isdir(src_path): for input_file in glob.glob(os.path.join(src_path, '*')): if os.path.isfile(input_file): rs.publish('chat', json.dumps({'message': 'Processing ' + os.path.basename(input_file), 'socketid': str(socketid)})) # Loading Image input_image = caffe.io.load_image(input_file) # Finding decaf features start = time.time() classifier.predict([input_image]) blobs = classifier.blobs.items() features = blobs[-3][1].data[:, :, 0, 0] features_center = blobs[-3][1].data[4, :, 0, 0] features_center = np.resize(features_center, (1, 4096)) timeMsg = "Completed in %.2f s." % (time.time() - start) rs.publish('chat', json.dumps({'message': timeMsg, 'socketid': str(socketid)})) # Saving decaf features matfile = {} matfile['decaf'] = features matfile['decaf_center'] = features_center out_file = os.path.join(output_path, os.path.basename(input_file) + '.mat') publish_file = os.path.join(result_path, os.path.basename(input_file) + '.mat') sio.savemat(out_file, matfile) rs.publish('chat', json.dumps({'web_result': publish_file, 'socketid': str(socketid)})) else: input_file = src_path rs.publish('chat', json.dumps({'message': 'Processing ' + os.path.basename(input_file), 'socketid': str(socketid)})) # Loading Image input_image = caffe.io.load_image(input_file) # Finding decaf features start = time.time() classifier.predict([input_image]) blobs = classifier.blobs.items() features = blobs[-3][1].data[:, :, 0, 0] features_center = blobs[-3][1].data[4, :, 0, 0] features_center = np.resize(features_center, (1, 4096)) timeMsg = "Completed in %.2f s." % (time.time() - start) rs.publish('chat', json.dumps({'message': timeMsg, 'socketid': str(socketid)})) # Saving decaf features matfile = {} matfile['decaf'] = features matfile['decaf_center'] = features_center out_file = os.path.join(result_path, os.path.basename(input_file) + '.mat') out_file = os.path.join(output_path, os.path.basename(input_file) + '.mat') publish_file = os.path.join(result_path, os.path.basename(input_file) + '.mat') sio.savemat(out_file, matfile) rs.publish('chat', json.dumps({'web_result': publish_file, 'socketid': str(socketid)})) rs.publish('chat', json.dumps({'message': 'Thank you for using CloudCV', 'socketid': str(socketid)})) except: # In case of an error, send the whole error with traceback rs.publish('chat', json.dumps({'message': str(traceback.format_exc()), 'socketid': str(socketid)}))
raise ImportError( "Cannot find the package 'skimage.io' which is required to read/write image files in " "TIFF and other formats" ) try: from skimage import exposure except ImportError: raise ImportError( "Unable to import package skimage.exposure which is required to write output image " "files with correct range of values" ) # Ideally, use freeimage plugin. That provides good support for tiff, png, and more # Backup solution: tifffile required for tiff format; skimage provides basic png, etc. support. try: skio.use_plugin("freeimage") except RuntimeError: FREEIMG_ERR_MSG = "Could not find the plugin 'freeimage' in skimage." # Because tifffile is going to be imported later on (in skio.imsave()) - check that it can be imported if not __package__: try: # it is unused here - it will be used by the tiff skio plugin # pylint: disable=unused-import import tifffile _USING_PLUGIN_TIFFFILE = True except ImportError: raise ImportError( "Cannot find the package 'tifffile' which is required to read/write TIFF image " "files." + FREEIMG_ERR_MSG )
def setup(): if imageio_available: np.random.seed(0) use_plugin('imageio')
def test_imread_simple(): io.use_plugin('fits') testfile = os.path.join(data_dir, 'simple.fits') img = io.imread(testfile) assert np.all(img == pyfits.getdata(testfile, 0))
from numpy.testing import * from skimage import io from skimage.io._plugins import plugin from numpy.testing.decorators import skipif try: io.use_plugin('pil') PIL_available = True priority_plugin = 'pil' except ImportError: PIL_available = False try: io.use_plugin('freeimage') FI_available = True priority_plugin = 'freeimage' except RuntimeError: FI_available = False def setup_module(self): plugin.use('test') # see ../_plugins/test_plugin.py def teardown_module(self): io.reset_plugins() class TestPlugin: def test_read(self):
from skimage import img_as_float from skimage import color from skimage import io import numpy as np from skimage.feature import peak_local_max, corner_peaks __author__ = "Kern" io.use_plugin("matplotlib") def generate_color_map(image, color_mask): img_f = img_as_float(image) black_mask = color.rgb2gray(img_f) < 0.1 distance = color.rgb2gray(1 - np.abs(img_f - color_mask)) distance[black_mask] = 0 return distance def generate_red_map(image): return generate_color_map(image, (1, 0, 0)) def generate_green_map(image): return generate_color_map(image, (0, 1, 0)) def generate_pink_map(image): return generate_color_map(image, (1, 0.5, 0.5))
import skimage from skimage import io io.use_plugin('gtk') import skimage.filter as filter from skimage.io import imshow from skimage.transform import hough_circle from skimage.feature import peak_local_max from skimage.draw import circle_perimeter from skimage.morphology import disk, erosion, dilation from data import Dataset import numpy as np def find_pupil(image, threshold=25): #image = image < threshold #image = dilation(image, disk(5)) #imshow(image) image = filter.canny(image, sigma=2, low_threshold=10, high_threshold=50) return find_circles(image, np.arange(15, 40, 2))[0:1] def find_circles(image, input_radii): result = hough_circle(image, input_radii) centers = [] accums = [] radii = [] for radius, h in zip(input_radii, result): # For each radius, extract two circles peaks = peak_local_max(h, num_peaks=2) centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius, radius]) circles = []