imsegm.utilities.data_io module¶
Framework for handling input/output
Copyright (C) 2015-2018 Jiri Borovec <jiri.borovec@fel.cvut.cz>
- imsegm.utilities.data_io.add_padding(img_size, padding, min_row, min_col, max_row, max_col)[source]¶
add some padding but still be inside image
- Parameters
- Return tuple(int,int,int,int)
>>> add_padding((50, 50), 5, 15, 25, 35, 55) (10, 20, 40, 50)
- imsegm.utilities.data_io.convert_img_2_nifti_gray(path_img, path_out)[source]¶
converting standard image to Nifti format
- Parameters
- Return str
path to output image
>>> np.random.seed(0) >>> img = np.random.random((150, 125)) >>> p_in = './temp_sample-image.png' >>> io.imsave(p_in, img) >>> p_out = convert_img_2_nifti_gray(p_in, '.') >>> p_out 'temp_sample-image.nii' >>> os.remove(p_out) >>> os.remove(p_in)
- imsegm.utilities.data_io.convert_img_2_nifti_rgb(path_img, path_out)[source]¶
converting standard image to Nifti format
- Parameters
- Return str
path to output image
>>> np.random.seed(0) >>> p_in = './temp_sample-image.png' >>> io.imsave(p_in, np.random.random((150, 125, 3))) >>> p_nifty = convert_img_2_nifti_rgb(p_in, '.') >>> p_nifty 'temp_sample-image.nii' >>> os.remove(p_nifty) >>> os.remove(p_in)
- imsegm.utilities.data_io.convert_img_color_from_rgb(image, color_space)[source]¶
convert image colour space from RGB to xxx
- Parameters
image (ndarray) – rgb image
color_space (str) –
- Return ndarray
image
>>> convert_img_color_from_rgb(np.ones((50, 75, 3)), 'hsv').shape (50, 75, 3)
- imsegm.utilities.data_io.convert_img_color_to_rgb(image, color_space)[source]¶
convert image colour space to RGB to xxx
- Parameters
image (ndarray) – rgb image
color_space (str) –
- Return ndarray
image
>>> convert_img_color_to_rgb(np.ones((50, 75, 3)), 'hsv').shape (50, 75, 3)
- imsegm.utilities.data_io.convert_nifti_2_img(path_img_in, path_img_out)[source]¶
given input and output path convert from nifti to image
- Parameters
- Return str
path to output image
>>> np.random.seed(0) >>> p_in = './temp_sample-image.png' >>> io.imsave(p_in, np.random.random((150, 125, 3))) >>> p_nifty = convert_img_2_nifti_rgb(p_in, '.') >>> p_nifty 'temp_sample-image.nii' >>> p_img = convert_nifti_2_img(p_nifty, './temp_sample-image.jpg') >>> p_img './temp_sample-image.jpg' >>> os.remove(p_nifty) >>> os.remove(p_img) >>> os.remove(p_in)
- imsegm.utilities.data_io.cut_object(img, mask, padding, use_mask=False, bg_color=None, allow_rotate=True)[source]¶
cut an object from image according binary object segmentation
- Parameters
- Returns
>>> img = np.ones((10, 20), dtype=int) >>> img[3:7, 4:16] = 2 >>> mask = np.zeros((10, 20), dtype=int) >>> mask[4:6, 5:15] = 1 >>> cut_object(img, mask, 2) array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) >>> cut_object(img, mask, 2, use_mask=True, allow_rotate=False) array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1], [1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
- imsegm.utilities.data_io.export_image(path_img, img, stretch_range=True)[source]¶
export an image with given path and optional pattern for image name
- Parameters
- Return str
path to the image
>>> # PNG image >>> np.random.seed(0) >>> img = np.random.random([5, 10]) >>> path_img = export_image(os.path.join('.', 'testing-image'), img) >>> os.path.basename(path_img) 'testing-image.png' >>> os.path.exists(path_img) True >>> im, name = load_image_2d(path_img) >>> im.shape (5, 10) >>> im array([[143, 186, 157, 141, 110, 168, 114, 232, 251, 99], [206, 137, 148, 241, 18, 22, 5, 216, 202, 226], [255, 208, 120, 203, 30, 166, 37, 246, 135, 108], [ 68, 201, 118, 148, 4, 160, 159, 160, 245, 177], [ 93, 113, 181, 15, 173, 174, 54, 33, 82, 94]], dtype=uint8) >>> os.remove(path_img)
>>> # TIFF image >>> img = np.random.random([5, 20, 25]) >>> path_img = export_image(os.path.join('.', 'testing-image'), img) >>> os.path.basename(path_img) 'testing-image.tiff' >>> os.path.exists(path_img) True >>> im, name = load_image_2d(path_img) >>> im.shape (5, 20, 25) >>> os.remove(path_img)
- imsegm.utilities.data_io.find_files_match_names_across_dirs(list_path_pattern, drop_none=True)[source]¶
walk over dir with images and segmentation and pair those with the same name and if the folder with centers exists also add to each par a center
Note
returns just paths
- Parameters
- Returns
DF<path_1, path_2, …>
>>> def _mp(d, n): ... return os.path.join(update_path('data-images'), 'drosophila_ovary_slice', d, n) >>> df = find_files_match_names_across_dirs([ ... _mp('image', '*.jpg'), ... _mp('segm', '*.png'), ... _mp('center_levels', '*.csv')]) >>> len(df) > 0 True >>> df.columns.tolist() ['path_1', 'path_2', 'path_3'] >>> df = find_files_match_names_across_dirs([ ... _mp('image', '*.png'), ... _mp('segm', '*.jpg'), ... _mp('center_levels', '*.csv')]) >>> len(df) 0
- imsegm.utilities.data_io.get_image2d_boundary_color(image, size=1)[source]¶
extract background color as median along image boundaries
- Parameters
image (ndarray) –
size (float) –
- Returns
>>> img = np.zeros((5, 15), dtype=int) >>> img[:4, 3:9] = 1 >>> img array([[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> get_image2d_boundary_color(img) 0 >>> get_image2d_boundary_color(np.ones((5, 15, 3), dtype=int), size=2) array([1, 1, 1]) >>> get_image2d_boundary_color(np.ones((5, 15, 3, 1), dtype=int)) array(0)
- imsegm.utilities.data_io.image_open(path_img)[source]¶
just a wrapper to suppers debug messages from the PIL function
- Parameters
path_img (str) –
- Return Image
- imsegm.utilities.data_io.io_image_decorate(func)[source]¶
costume decorator to suppers debug messages from the PIL function to suppress PIl debug logging - DEBUG:PIL.PngImagePlugin:STREAM b’IHDR’ 16 13
- Parameters
func –
- Returns
- imsegm.utilities.data_io.io_imread(path_img)[source]¶
just a wrapper to suppers debug messages from the PIL function
- Parameters
path_img (str) –
- Return ndarray
- imsegm.utilities.data_io.io_imsave(path_img, img)[source]¶
just a wrapper to suppers debug messages from the PIL function
- Parameters
path_img (str) –
img (ndarray) – image
- imsegm.utilities.data_io.load_complete_image_folder(path_dir, img_name_pattern='*.png', nb_sample=None, im_range=255, skip=None)[source]¶
load complete image folder with specific name pattern
- Parameters
- Returns
>>> p_imgs = os.path.join(update_path('data-images'), 'drosophila_ovary_slice', 'image') >>> l_imgs, l_names = load_complete_image_folder(p_imgs, '*.jpg') >>> len(l_imgs) 5 >>> l_names ['insitu4174', 'insitu4358', 'insitu7331', 'insitu7544', 'insitu7545']
- imsegm.utilities.data_io.load_image_2d(path_img)[source]¶
loading any supported image type
- Parameters
path_img (str) – path to the input image
- Return str, ndarray
image name, image as matrix
>>> # PNG image >>> img_name = 'testing-image' >>> img = np.random.randint(0, 255, size=(20, 20, 3)) >>> path_img = export_image(os.path.join('.', img_name), img, stretch_range=False) >>> os.path.basename(path_img) 'testing-image.png' >>> os.path.exists(path_img) True >>> img_new, _ = load_image_2d(path_img) >>> np.array_equal(img, img_new) True >>> io.imsave(path_img, np.random.random((50, 65, 4))) >>> img_new, _ = load_image_2d(path_img) >>> img_new.shape (50, 65, 3) >>> Image.fromarray(np.random.randint(0, 2, (65, 50)), mode='1').save(path_img) >>> img_new, _ = load_image_2d(path_img) >>> img_new.shape (65, 50) >>> os.remove(path_img)
>>> # TIFF image >>> img_name = 'testing-image' >>> img = np.random.randint(0, 255, size=(5, 20, 20)) >>> path_img = export_image(os.path.join('.', img_name), img, stretch_range=False) >>> os.path.basename(path_img) 'testing-image.tiff' >>> os.path.exists(path_img) True >>> img_new, _ = load_image_2d(path_img) >>> img_new.shape (5, 20, 20) >>> np.array_equal(img, img_new) True >>> os.remove(path_img)
- imsegm.utilities.data_io.load_image_tiff_volume(path_img, im_range=None)[source]¶
loading TIFF image
- Parameters
- Return ndarray
>>> p_img = os.path.join(update_path('data-images'), 'drosophila_ovary_3D', 'AU10-13_f0011.tif') >>> img = load_image_tiff_volume(p_img) >>> img.shape (30, 323, 512) >>> p_img = os.path.join(update_path('data-images'), 'drosophila_ovary_slice', 'image', 'insitu7545.tif') >>> img = load_image_tiff_volume(p_img) >>> img.shape (647, 1024, 3)
- imsegm.utilities.data_io.load_images_list(path_imgs, im_range=255)[source]¶
load list of images together with image names
- Parameters
- Return list(ndarray), list(str)
>>> np.random.seed(0) >>> path_in = './temp_sample-image.png' >>> io.imsave(path_in, np.random.random((150, 125, 3))) >>> l_imgs, l_names = load_images_list([path_in, './temp_sample.img']) >>> l_names ['temp_sample-image'] >>> [img.shape for img in l_imgs] [(150, 125, 3)] >>> [img.dtype for img in l_imgs] [dtype('uint8')] >>> os.remove(path_in) >>> path_in = './temp_sample-image.tif' >>> io.imsave(path_in, np.random.random((150, 125, 3))) >>> l_imgs, l_names = load_images_list([path_in, './temp_sample.img']) >>> l_names ['temp_sample-image'] >>> os.remove(path_in)
- imsegm.utilities.data_io.load_img_double_band_split(path_img, im_range=1.0, quantiles=(2, 98))[source]¶
load image and split channels
- Parameters
- Returns
>>> p_imgs = os.path.join(update_path('data-images'), 'drosophila_ovary_slice', 'image') >>> p_img = os.path.join(p_imgs, 'insitu7545.jpg') >>> img_b1, img_b2 = load_img_double_band_split(p_img) >>> img_b1.shape (647, 1024) >>> p_img = os.path.join(p_imgs, 'insitu7545.tif') >>> img_b1, img_b2 = load_img_double_band_split(p_img) >>> img_b1.shape (647, 1024) >>> p_img = os.path.join(update_path('data-images'), 'drosophila_ovary_3D', 'AU10-13_f0011.tif') >>> img_b1, img_b2 = load_img_double_band_split(p_img) >>> img_b1.shape (15, 323, 512)
- imsegm.utilities.data_io.load_landmarks_csv(path_file)[source]¶
load the landmarks from a given file of TXT type and return array
- Parameters
path_file (str) – name of the input file(whole path)
- Return ndarray
array of landmarks of size <nbLandmarks> x 2
>>> lnds = np.array([[1, 2], [2, 4], [5, 6]]) >>> fp = save_landmarks_csv('./sample_landmarks.test', lnds) >>> fp './sample_landmarks.csv' >>> lnds_new = load_landmarks_csv(fp) >>> np.array_equal(lnds, lnds_new) True >>> os.remove(fp)
- imsegm.utilities.data_io.load_landmarks_txt(path_file)[source]¶
load the landmarks from a given file of TXT type and return array
- Parameters
path_file (str) – name of the input file(whole path)
- Return ndarray
array of landmarks of size <nbLandmarks> x 2
>>> lnds = np.array([[1, 2], [2, 4], [5, 6]]) >>> fp = save_landmarks_txt('./sample_landmarks.test', lnds) >>> fp './sample_landmarks.txt' >>> lnds_new = load_landmarks_txt(fp) >>> np.array_equal(lnds, lnds_new) True >>> os.remove(fp)
- imsegm.utilities.data_io.load_params_from_txt(path_file)[source]¶
parse the parameter file which was coded by repr function
- Parameters
path_file (str) – path to file with parameters
- Return dict
>>> p = {'abc': 123} >>> path_file = './sample_config.txt' >>> with open(path_file, 'w') as fp: ... lines = ['"{}" : {},'.format(k, p[k]) for k in p] ... _ = fp.write(os.linesep.join(lines)) # it may return nb characters >>> p2 = load_params_from_txt(path_file) >>> p2 {'abc': '123'} >>> os.remove(path_file)
- imsegm.utilities.data_io.load_tiff_volume_split_double_band(path_img, im_range=None)[source]¶
load TIFF volume assuming that there are two bands in zip style: c1, c2, c1, c2, c1, … and split each odd index belong to one of two bands
- Parameters
- Return ndarray, ndarray
>>> p_img = os.path.join(update_path('data-images'), 'drosophila_ovary_3D', 'AU10-13_f0011.tif') >>> img_b1, img_b2 = load_tiff_volume_split_double_band(p_img) >>> img_b1.shape, img_b2.shape ((15, 323, 512), (15, 323, 512)) >>> p_img = os.path.join(update_path('data-images'), 'drosophila_ovary_slice', 'image', 'insitu7545.tif') >>> img_b1, img_b2 = load_tiff_volume_split_double_band(p_img) >>> img_b1.shape, img_b2.shape ((1, 647, 1024), (1, 647, 1024)) >>> img = np.random.randint(0, 255, (1, 3, 250, 200)) >>> p_img = './sample-multistack.tif' >>> io.imsave(p_img, img) >>> img_b1, img_b2 = load_tiff_volume_split_double_band(p_img) >>> img_b1.shape, img_b2.shape ((1, 250, 200), (1, 250, 200)) >>> os.remove(p_img)
- imsegm.utilities.data_io.load_zvi_volume_double_band_split(path_img)[source]¶
loading zvi image and split by bands
- Parameters
path_img (str) – path to the image
- Return ndarray, ndarray
>>> p_img = os.path.join(update_path('data-images'), 'others', 'sample.zvi') >>> img_b1, img_b2 = load_zvi_volume_double_band_split(p_img) >>> img_b1.shape (2, 488, 648)
- imsegm.utilities.data_io.merge_image_channels(img_ch1, img_ch2, img_ch3=None)[source]¶
merge 2 or 3 image channels into single image
- Parameters
img_ch1 (ndarray) – image channel
img_ch2 (ndarray) – image channel
img_ch3 (ndarray) – image channel
- Return ndarray
>>> np.random.seed(0) >>> merge_image_channels(np.random.random((150, 125)), ... np.random.random((150, 125))).shape (150, 125, 3) >>> merge_image_channels(np.random.random((150, 125)), ... np.random.random((150, 125)), ... np.random.random((150, 125))).shape (150, 125, 3)
- imsegm.utilities.data_io.save_landmarks_csv(path_file, landmarks, dtype=<class 'float'>)[source]¶
save the landmarks into a given file of CSV type
- imsegm.utilities.data_io.save_landmarks_txt(path_file, landmarks)[source]¶
save the landmarks into a given file of TXT type
- Parameters
path_file (str) – name of the input file(whole path)
landmarks – array of landmarks of size nb_landmarks x 2
- Return str
path to output file
- imsegm.utilities.data_io.scale_image_intensity(img, im_range=1.0, quantiles=(2, 98))[source]¶
scale image values with in give quntile range to filter some outlaiers
- Parameters
- Return ndarray
>>> np.random.seed(0) >>> img = np.random.randint(10, 255, (25, 30)) >>> im = scale_image_intensity(img) >>> im.min() 0.0 >>> im.max() 1.0
- imsegm.utilities.data_io.scale_image_size(path_img, size, path_out=None)[source]¶
load image - scale image - export image on the same path
- Parameters
- Return str
path to output image
>>> np.random.seed(0) >>> path_in = './test_sample_image.png' >>> io.imsave(path_in, np.random.random((150, 125, 3))) >>> path_out = scale_image_size(path_in, [75, 50]) >>> Image.open(path_out).size (75, 50) >>> os.remove(path_out)
- imsegm.utilities.data_io.scale_image_vals_in_range(img, im_range=1.0)[source]¶
scale image values in given range
- Parameters
img (ndarray) – input image
im_range – range to scale image values (1. or 255)
- Return ndarray
>>> np.random.seed(0) >>> img = np.random.randint(10, 255, (25, 30)) >>> im = scale_image_vals_in_range(img) >>> im.min() 0.0 >>> im.max() 1.0
- imsegm.utilities.data_io.swap_coord_x_y(points)[source]¶
swap X and Y coordinates in vector of possitions
>>> swap_coord_x_y(np.array([[1, 2], [2, 4], [5, 6]])) [[2, 1], [4, 2], [6, 5]]
- imsegm.utilities.data_io.update_path(path_file, lim_depth=5, absolute=True)[source]¶
bubble in the folder tree up until it found desired file otherwise return original one
- Parameters
- Return str
path to output file / folder
>>> path = 'sample_file.test' >>> f = open(path, 'w') >>> update_path(path, absolute=False) 'sample_file.test'