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
  • img_size (tuple(int,int)) –

  • padding (int) – set padding around segmented object

  • min_row (int) – setting top left corner of bounding box

  • min_col (int) – setting top left corner of bounding box

  • max_row (int) – setting bottom right corner of bounding box

  • max_col (int) – setting bottom right corner of bounding box

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
  • path_img (str) – path to input image

  • path_out (str) – path to output directory

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
  • path_img (str) – path to input image

  • path_out (str) – path to output directory

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
  • path_img_in (str) – path to input image

  • path_img_out (str) – path to output image

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
  • img (ndarray) – inout image

  • mask (ndarray) – segmentation

  • padding (int) – set padding around segmented object

  • use_mask (bool) – fill BG values also outside the mask

  • bg_color – set as default values outside bounding box

  • allow_rotate (bool) – allowing rotate object to get minimal bbox

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
  • path_img (str) – path to the output image

  • img (ndarray) – image np.array<height, width>

  • stretch_range (bool) –

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
  • list_path_pattern (list(str)) – list of paths with image name patterns

  • drop_none (bool) – drop if there are some none - missing values in rows

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
  • path_dir (str) – loading dictionary

  • img_name_pattern (str) – image name pattern

  • nb_sample (int) – load just some subset of images

  • im_range – range to scale image values (1. or 255)

  • skip (list(str)|None) – skip some prticular images by name

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(path_im, im_range=255)[source]
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
  • path_img (str) – path to the input image

  • im_range (float) – range to scale image values (1. or 255)

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
  • path_imgs (list(str)) – paths to input images

  • im_range – range to scale image values (1. or 255)

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
  • path_img (str) – path to the image

  • im_range (float|None) – range to scale image values (1. or 255)

  • quantiles (tuple(int,int)) – scale image values in certain percentile range

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
  • path_img (str) – path to the input image

  • im_range (float) – range to scale image values (1. or 255)

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

Parameters
  • path_file (str) – fName is name of the input file(whole path)

  • int ]] landmarks ([[int,) – array of landmarks of size nb_landmarks x 2

  • dtype (type) – data type

Return str

path to output file

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
  • img (ndarray) – input image

  • im_range – range to scale image values (1. or 255)

  • quantiles (tuple(int,int)) – scale image values in certain quantile range

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
  • path_img (str) – path to the image

  • size (tuple(int,int)) – new image size

  • path_out (str) – path to output image, if none overwrite the input

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

Parameters

points (list(tuple(int,int))) –

Return list(tuple(int,int))

>>> 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
  • path_file (str) – path to the input file / folder

  • lim_depth (int) – maximal depth for going up

  • absolute (bool) – format absolute path

Return str

path to output file / folder

>>> path = 'sample_file.test'
>>> f = open(path, 'w')
>>> update_path(path, absolute=False)
'sample_file.test'
imsegm.utilities.data_io.COLUMNS_COORDS = ['X', 'Y'][source]

position columns

imsegm.utilities.data_io.DICT_CONVERT_COLOR_FROM_RGB = {'hed': skimage.color.rgb2hed, 'hsv': skimage.color.rgb2hsv, 'lab': skimage.color.rgb2lab, 'luv': skimage.color.rgb2luv, 'xyz': skimage.color.rgb2xyz}[source]

conversion function from RGB color space

imsegm.utilities.data_io.DICT_CONVERT_COLOR_TO_RGB = {'hed': skimage.color.hed2rgb, 'hsv': skimage.color.hsv2rgb, 'lab': skimage.color.lab2rgb, 'luv': skimage.color.luv2rgb, 'xyz': skimage.color.xyz2rgb}[source]

conversion function to RGB color space