From 11c7fd2f59de5443f7fbb8a6ea7e860a9ad3fb88 Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Fri, 20 Jul 2012 15:16:48 -0500 Subject: [PATCH] Add function 'concatenate' to ImageCollection Many algorithms work on 3D stacks rather than images. It is convenient to provide automatic conversion from an ImageCollection to an nD-stack. --- skimage/io/collection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/skimage/io/collection.py b/skimage/io/collection.py index e698b52b..7a07aa7f 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -307,3 +307,23 @@ class ImageCollection(object): """ self.data = np.empty_like(self.data) + + def concatenate(self): + """Concatenate all images in the collection on a new axis. + + Returns + ------- + ar : np.ndarray + An array having one more dimension than the images in `self`. + + Raises + ------ + ValueError + If images in the collection don't have identical shapes. + """ + all_images = [img[np.newaxis, ...] for img in self] + try: + ar = np.concatenate(all_images) + except ValueError: + raise ValueError('Image dimensions must agree.') + return ar