def test_get_image(self): temp_dir = self.get_temp_dir() # test tiff files test_img_path = os.path.join(temp_dir, 'phase.tif') _write_image(test_img_path, 300, 300) test_img = io_utils.get_image(test_img_path) self.assertEqual(np.asarray(test_img).shape, (300, 300)) # test png files test_img_path = os.path.join(temp_dir, 'feature_0.png') _write_image(test_img_path, 400, 400) test_img = io_utils.get_image(test_img_path) self.assertEqual(np.asarray(test_img).shape, (400, 400))
def test_get_image(self): image = 255 * np.random.random(size=(300, 300, 1)).astype('float32') temp_dir = self.get_temp_dir() # test tiff files test_img_path = os.path.join(temp_dir, 'phase.tif') imsave(test_img_path, image, check_contrast=False) test_img = io_utils.get_image(test_img_path) self.assertAllEqual(image, test_img) # test png files # pngs are integer only and don't have a channel axis. test_img_path = os.path.join(temp_dir, 'feature_0.png') image = image.astype('uint8') imsave(test_img_path, image, check_contrast=False) test_img = io_utils.get_image(test_img_path) test_img = np.expand_dims(test_img, axis=-1) self.assertAllClose(image, test_img)
def load_training_images_3d(direc_name, training_direcs, raw_image_direc, channel_names, image_size, num_frames, montage_mode=False): """Load each image in the training_direcs into a numpy array. # Arguments direc_name: directory containing folders of training data training_direcs: list of directories of images inside direc_name. raw_image_direc: directory name inside each training dir with raw images channel_names: Loads all raw images with a channel_name in the filename image_size: size of each image as tuple (x, y) num_frames: number of frames to load from each training directory montage_mode: load masks from "montaged" subdirs inside annotation_direc """ is_channels_first = K.image_data_format() == 'channels_first' image_size_x, image_size_y = image_size # flatten list of lists X_dirs = [os.path.join(direc_name, t, raw_image_direc) for t in training_direcs] if montage_mode: X_dirs = [os.path.join(t, p) for t in X_dirs for p in os.listdir(t)] X_dirs = sorted_nicely(X_dirs) # Initialize training data array if is_channels_first: X_shape = (len(X_dirs), len(channel_names), num_frames, image_size_x, image_size_y) else: X_shape = (len(X_dirs), num_frames, image_size_x, image_size_y, len(channel_names)) X = np.zeros(X_shape, dtype=K.floatx()) # Load 3D training images for b, direc in enumerate(X_dirs): for c, channel in enumerate(channel_names): imglist = nikon_getfiles(direc, channel) for i, img in enumerate(imglist): if i >= num_frames: print('Skipped final {skip} frames of {dir}, as num_frames ' 'is {num} but there are {total} total frames'.format( skip=len(imglist) - num_frames, dir=direc, num=num_frames, total=len(imglist))) break image_data = np.asarray(get_image(os.path.join(direc, img))) if is_channels_first: X[b, c, i, :, :] = image_data else: X[b, i, :, :, c] = image_data return X
def load_annotated_images_3d(direc_name, training_direcs, annotation_direc, annotation_name, image_size, num_frames, montage_mode=False): """Load each annotated image in the training_direcs into a numpy array. # Arguments direc_name: directory containing folders of training data training_direcs: list of directories of images inside direc_name. annotation_direc: directory name inside each training dir with masks annotation_name: Loads all masks with annotation_name in the filename image_size: size of each image as tuple (x, y) num_frames: number of frames to load from each training directory montage_mode: load masks from "montaged" subdirs inside annotation_direc """ is_channels_first = K.image_data_format() == 'channels_first' image_size_x, image_size_y = image_size # wrapping single annotation name in list for consistency if not isinstance(annotation_name, list): annotation_name = [annotation_name] y_dirs = [os.path.join(direc_name, t, annotation_direc) for t in training_direcs] if montage_mode: y_dirs = [os.path.join(t, p) for t in y_dirs for p in os.listdir(t)] y_dirs = sorted_nicely(y_dirs) if is_channels_first: y_shape = (len(y_dirs), len(annotation_name), num_frames, image_size_x, image_size_y) else: y_shape = (len(y_dirs), num_frames, image_size_x, image_size_y, len(annotation_name)) y = np.zeros(y_shape, dtype='int32') for b, direc in enumerate(y_dirs): for c, name in enumerate(annotation_name): imglist = nikon_getfiles(direc, name) for z, img_file in enumerate(imglist): if z >= num_frames: print('Skipped final {skip} frames of {dir}, as num_frames ' 'is {num} but there are {total} total frames'.format( skip=len(imglist) - num_frames, dir=direc, num=num_frames, total=len(imglist))) break annotation_img = get_image(os.path.join(direc, img_file)) if is_channels_first: y[b, c, z, :, :] = annotation_img else: y[b, z, :, :, c] = annotation_img return y
def load_annotated_images_2d(direc_name, training_direcs, annotation_direc, annotation_name, image_size): """Load each annotated image in the training_direcs into a numpy array. Args: direc_name (str): directory containing folders of training data training_direcs (str[]): list of directories of images inside direc_name. annotation_direc (str): directory name inside each training dir with masks annotation_name (str): Loads all masks with annotation_name in the filename image_size (tuple): size of each image as tuple (x, y) Returns: numpy.array: 4D tensor of label masks """ is_channels_first = K.image_data_format() == 'channels_first' # Unpack size tuple image_size_x, image_size_y = image_size # wrapping single annotation name in list for consistency if not isinstance(annotation_name, list): annotation_name = [annotation_name] # Initialize feature mask array if is_channels_first: y_shape = (len(training_direcs), len(annotation_name), image_size_x, image_size_y) else: y_shape = (len(training_direcs), image_size_x, image_size_y, len(annotation_name)) y = np.zeros(y_shape, dtype='int32') for b, direc in enumerate(training_direcs): imglist = os.listdir(os.path.join(direc_name, direc, annotation_direc)) for l, annotation in enumerate(annotation_name): for img in imglist: # if annotation_name is NOT in image file name, skip it. if not fnmatch(img, '*{}*'.format(annotation)): continue image_data = get_image( os.path.join(direc_name, direc, annotation_direc, img)) if is_channels_first: y[b, l, :, :] = image_data else: y[b, :, :, l] = image_data return y
def load_training_images_2d(direc_name, training_direcs, raw_image_direc, channel_names, image_size): """Load each image in the training_direcs into a numpy array. Args: direc_name (str): directory containing folders of training data training_direcs (str[]): list of directories of images inside direc_name. raw_image_direc (str): directory name inside each training dir with raw images channel_names (str[]): Loads all raw images with a channel_name in the filename image_size (tuple): size of each image as tuple (x, y) Returns: numpy.array: 4D tensor of image data """ is_channels_first = K.image_data_format() == 'channels_first' # Unpack size tuples image_size_x, image_size_y = image_size # Initialize training data array if K.image_data_format() == 'channels_first': X_shape = (len(training_direcs), len(channel_names), image_size_x, image_size_y) else: X_shape = (len(training_direcs), image_size_x, image_size_y, len(channel_names)) X = np.zeros(X_shape, dtype=K.floatx()) # Load training images for b, direc in enumerate(training_direcs): # e.g. "/data/ecoli/kc", "set1", "RawImages", imglist = os.listdir(os.path.join(direc_name, direc, raw_image_direc)) for c, channel in enumerate(channel_names): for img in imglist: # if channel string is NOT in image file name, skip it. if not fnmatch(img, '*{}*'.format(channel)): continue image_file = os.path.join(direc_name, direc, raw_image_direc, img) image_data = np.asarray(get_image(image_file), dtype=K.floatx()) if is_channels_first: X[b, c, :, :] = image_data else: X[b, :, :, c] = image_data return X
def load_image(path, channel=0, ndim=3): """Load an image file as a single-channel numpy array. Args: path (str): Filepath to the image file to load. channel (list): Loads the given channel if available. If channel is list of length > 1, each channel will be summed. ndim (int): The expected rank of the returned tensor. Returns: numpy.array: The image channel loaded as an array. """ if not path: raise IOError('Invalid path: %s' % path) img = get_image(path) channel = channel if isinstance(channel, (list, tuple)) else [channel] # getting a little tricky, which axis is channel axis? if img.ndim == ndim: # file includes channels, find the channel axis # assuming the channels axis is the smallest dimension axis = img.shape.index(min(img.shape)) if max(channel) >= img.shape[axis]: raise ValueError('Channel {} was passed but channel axis is ' 'only size {}'.format(max(channel), img.shape[axis])) # slice out only the required channel slc = [slice(None)] * len(img.shape) # use integer to select only the relevant channels slc[axis] = channel img = img[tuple(slc)] # sum on the channel axis img = img.sum(axis=axis) # expand the (proper) channel axis img = np.expand_dims(img, axis=-1) if not img.ndim == ndim: raise ValueError('Expected image with ndim = {} but found ndim={} ' 'and shape={}'.format(ndim, img.ndim, img.shape)) return img