Esempio n. 1
0
 def setUp(self):
     reset_plugins()
     # Generic image collection with images of different shapes.
     self.images = ImageCollection(self.pattern)
     # Image collection with images having shapes that match.
     self.images_matched = ImageCollection(self.pattern_matched)
     # Same images as a collection of frames
     self.frames_matched = MultiImage(self.pattern_matched)
Esempio n. 2
0
 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))]
Esempio n. 3
0
 def setUp(self):
     # This multipage TIF file was created with imagemagick:
     # convert im1.tif im2.tif -adjoin multipage.tif
     paths = [
         os.path.join(data_dir, 'multipage.tif'),
         os.path.join(data_dir, 'no_time_for_that.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('%s:%s' % (paths[0], paths[1]))
     ]
Esempio n. 4
0
    def test_custom_load_func(self):

        def load_fn(x):
            return x

        ic = ImageCollection(os.pathsep.join(self.pattern), load_func=load_fn)
        assert_equal(ic[0], self.pattern[0])
Esempio n. 5
0
def load_from_dir(directory, extension):
    """
    load_from_dir: Takes in the directory path and extension of images to be read, and reads them in.
            Args:
                (String) directroy: path to directory
                (String) extension: extension of the files to be read
            Returns:
                (Numpy Array)data: The normal data w the 3 channels
                (Numpy Array) data_gray: the data converted to gray scale format 
    """
    data = np.array(
        ImageCollection('{a}/*.{b}'.format(a=directory, b=extension),
                        load_func=imread))
    data_gray = np.array(
        ImageCollection('{a}/*.{b}'.format(a=directory, b=extension),
                        load_func=imread_grayscale))
    return (data, data_gray)
Esempio n. 6
0
    def test_custom_load(self):
        load_pattern = [(1, 'one'), (2, 'two')]

        def load_fn(x):
            return x

        ic = ImageCollection(load_pattern, load_func=load_fn)
        assert_equal(ic[1], (2, 'two'))
Esempio n. 7
0
def imgs():
    use_plugin('pil')
    paths = [
        os.path.join(data_dir, 'multipage_rgb.tif'),
        os.path.join(data_dir, '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),
        ImageCollection(paths[0]),
        ImageCollection(paths[1], conserve_memory=False),
        ImageCollection(os.pathsep.join(paths))
    ]
    yield imgs

    reset_plugins()
Esempio n. 8
0
    def test_custom_load_func_sequence(self):
        filename = fetch('data/no_time_for_that_tiny.gif')

        def reader(frameno):
            vid = imageio.get_reader(filename)
            return vid.get_data(frameno)

        ic = ImageCollection(range(24), load_func=reader)
        # the length of ic should be that of the given load_pattern sequence
        assert len(ic) == 24
        # GIF file has frames of size 25x14 with 4 channels (RGBA)
        assert ic[0].shape == (25, 14, 4)
Esempio n. 9
0
    def test_custom_load_func_w_kwarg(self):
        load_pattern = os.path.join(data_dir, 'no_time_for_that_tiny.gif')

        def load_fn(f, step):
            vid = imageio.get_reader(f)
            seq = [v for v in vid.iter_data()]
            return seq[::step]

        ic = ImageCollection(load_pattern, load_func=load_fn, step=3)
        # Each file should map to one image (array).
        assert len(ic) == 1
        # GIF file has 24 frames, so 24 / 3 equals 8.
        assert len(ic[0]) == 8
Esempio n. 10
0
def test_imagecollection_input():
    """Test function for ImageCollection. The new behavior (implemented
    in 0.16) allows the `pattern` argument to accept a list of strings
    as the input.

    Notes
    -----
        If correct, `images` will receive three images.
    """
    pattern = [
        os.path.join(data_dir, pic) for pic in
        ['palette_gray.png', 'chessboard_GRAY_U16.tif', 'rocket.jpg']
    ]
    images = ImageCollection(pattern)
    assert len(images) == 3
Esempio n. 11
0
def test_imagecollection_input():
    """Test function for ImageCollection. The new behavior (implemented
    in 0.16) allows the `pattern` argument to accept a list of strings
    as the input.

    Notes
    -----
        If correct, `images` will receive three images.
    """
    # Ensure that these images are part of the legacy datasets
    # this means they will always be available in the user's install
    # regarless of the availability of pooch
    pattern = [
        os.path.join(data_dir, pic)
        for pic in ['coffee.png', 'chessboard_GRAY.png', 'rocket.jpg']
    ]
    images = ImageCollection(pattern)
    assert len(images) == 3
Esempio n. 12
0
def get_sample_of_class(classs, labels, input_path, num=-1):
    """
    Returns images sampled from files by class
    """

    existing_files = pd.DataFrame(
        [path.splitext(f)[0] for f in os.listdir(input_path)],
        columns=[labels.columns[0]])
    class_labels = pd.DataFrame(labels[labels['level'] == classs]['image'],
                                columns=[labels.columns[0]])
    class_labels = class_labels.merge(existing_files,
                                      on=class_labels.columns[0])
    if num == -1:
        num = class_labels.count()

    files = [
        path.join(raw_train, c + ".jpeg")
        for c in class_labels[:num][class_labels.columns[0]]
    ]
    return ImageCollection(files, conserve_memory=True)
Esempio n. 13
0
 def setUp(self):
     reset_plugins()
     # Generic image collection with images of different shapes.
     self.images = ImageCollection(self.pattern)
     # Image collection with images having shapes that match.
     self.images_matched = ImageCollection(self.pattern_matched)
Esempio n. 14
0
class TestImageCollection():

    pattern = [os.path.join(data_dir, pic)
               for pic in ['camera.png', 'color.png']]

    pattern_matched = [os.path.join(data_dir, pic)
                       for pic in ['camera.png', 'moon.png']]

    def setUp(self):
        reset_plugins()
        # Generic image collection with images of different shapes.
        self.images = ImageCollection(self.pattern)
        # Image collection with images having shapes that match.
        self.images_matched = ImageCollection(self.pattern_matched)

    def test_len(self):
        assert len(self.images) == 2

    def test_getitem(self):
        num = len(self.images)
        for i in range(-num, num):
            assert type(self.images[i]) is np.ndarray
        assert_allclose(self.images[0],
                                  self.images[-num])

        # assert_raises expects a callable, hence this thin wrapper function.
        def return_img(n):
            return self.images[n]
        assert_raises(IndexError, return_img, num)
        assert_raises(IndexError, return_img, -num - 1)

    def test_slicing(self):
        assert type(self.images[:]) is ImageCollection
        assert len(self.images[:]) == 2
        assert len(self.images[:1]) == 1
        assert len(self.images[1:]) == 1
        assert_allclose(self.images[0], self.images[:1][0])
        assert_allclose(self.images[1], self.images[1:][0])
        assert_allclose(self.images[1], self.images[::-1][0])
        assert_allclose(self.images[0], self.images[::-1][1])

    def test_files_property(self):
        assert isinstance(self.images.files, list)

        def set_files(f):
            self.images.files = f
        assert_raises(AttributeError, set_files, 'newfiles')

    def test_custom_load(self):
        load_pattern = [(1, 'one'), (2, 'two')]

        def load_fn(x):
            return x

        ic = ImageCollection(load_pattern, load_func=load_fn)
        assert_equal(ic[1], (2, 'two'))

    def test_custom_load_func(self):

        def load_fn(x):
            return x

        ic = ImageCollection(os.pathsep.join(self.pattern), load_func=load_fn)
        assert_equal(ic[0], self.pattern[0])

    def test_concatenate(self):
        array = self.images_matched.concatenate()
        expected_shape = (len(self.images_matched),) + self.images[0].shape
        assert_equal(array.shape, expected_shape)

    def test_concatentate_mismatched_image_shapes(self):
        assert_raises(ValueError, self.images.concatenate)
Esempio n. 15
0
class TestImageCollection(TestCase):

    pattern = [
        os.path.join(data_dir, pic) for pic in ['camera.png', 'color.png']
    ]

    pattern_matched = [
        os.path.join(data_dir, pic) for pic in ['camera.png', 'moon.png']
    ]

    def setUp(self):
        reset_plugins()
        # Generic image collection with images of different shapes.
        self.images = ImageCollection(self.pattern)
        # Image collection with images having shapes that match.
        self.images_matched = ImageCollection(self.pattern_matched)

    def test_len(self):
        assert len(self.images) == 2

    def test_getitem(self):
        num = len(self.images)
        for i in range(-num, num):
            assert isinstance(self.images[i], np.ndarray)
        assert_allclose(self.images[0], self.images[-num])

        def return_img(n):
            return self.images[n]

        with testing.raises(IndexError):
            return_img(num)
        with testing.raises(IndexError):
            return_img(-num - 1)

    def test_slicing(self):
        assert type(self.images[:]) is ImageCollection
        assert len(self.images[:]) == 2
        assert len(self.images[:1]) == 1
        assert len(self.images[1:]) == 1
        assert_allclose(self.images[0], self.images[:1][0])
        assert_allclose(self.images[1], self.images[1:][0])
        assert_allclose(self.images[1], self.images[::-1][0])
        assert_allclose(self.images[0], self.images[::-1][1])

    def test_files_property(self):
        assert isinstance(self.images.files, list)

        def set_files(f):
            self.images.files = f

        with testing.raises(AttributeError):
            set_files('newfiles')

    def test_custom_load(self):
        load_pattern = [(1, 'one'), (2, 'two')]

        def load_fn(x):
            return x

        ic = ImageCollection(load_pattern, load_func=load_fn)
        assert_equal(ic[1], (2, 'two'))

    def test_custom_load_func(self):
        def load_fn(x):
            return x

        ic = ImageCollection(os.pathsep.join(self.pattern), load_func=load_fn)
        assert_equal(ic[0], self.pattern[0])

    def test_concatenate(self):
        array = self.images_matched.concatenate()
        expected_shape = (len(self.images_matched), ) + self.images[0].shape
        assert_equal(array.shape, expected_shape)

    def test_concatentate_mismatched_image_shapes(self):
        with testing.raises(ValueError):
            self.images.concatenate()
Esempio n. 16
0
def get_images(path):
    pattern = os.path.abspath(path) + os.sep + '*.jpg'
    return ImageCollection(pattern, load_func=imread_convert)
Esempio n. 17
0
 def setUp(self):
     # Generic image collection with images of different shapes.
     self.images = ImageCollection(self.pattern)
     # Image collection with images having shapes that match.
     self.images_matched = ImageCollection(self.pattern_matched)
Esempio n. 18
0
class TestImageCollection(TestCase):
    pattern = [
        os.path.join(data_dir, pic) for pic in ['brick.png', 'color.png']
    ]

    pattern_matched = [
        os.path.join(data_dir, pic) for pic in ['brick.png', 'moon.png']
    ]

    def setUp(self):
        reset_plugins()
        # Generic image collection with images of different shapes.
        self.images = ImageCollection(self.pattern)
        # Image collection with images having shapes that match.
        self.images_matched = ImageCollection(self.pattern_matched)
        # Same images as a collection of frames
        self.frames_matched = MultiImage(self.pattern_matched)

    def test_len(self):
        assert len(self.images) == 2

    def test_getitem(self):
        num = len(self.images)
        for i in range(-num, num):
            assert isinstance(self.images[i], np.ndarray)
        assert_allclose(self.images[0], self.images[-num])

        def return_img(n):
            return self.images[n]

        with testing.raises(IndexError):
            return_img(num)
        with testing.raises(IndexError):
            return_img(-num - 1)

    def test_slicing(self):
        assert type(self.images[:]) is ImageCollection
        assert len(self.images[:]) == 2
        assert len(self.images[:1]) == 1
        assert len(self.images[1:]) == 1
        assert_allclose(self.images[0], self.images[:1][0])
        assert_allclose(self.images[1], self.images[1:][0])
        assert_allclose(self.images[1], self.images[::-1][0])
        assert_allclose(self.images[0], self.images[::-1][1])

    def test_files_property(self):
        assert isinstance(self.images.files, list)

        def set_files(f):
            self.images.files = f

        with testing.raises(AttributeError):
            set_files('newfiles')

    def test_custom_load_func_w_kwarg(self):
        load_pattern = os.path.join(data_dir, 'no_time_for_that_tiny.gif')

        def load_fn(f, step):
            vid = imageio.get_reader(f)
            seq = [v for v in vid.iter_data()]
            return seq[::step]

        ic = ImageCollection(load_pattern, load_func=load_fn, step=3)
        # Each file should map to one image (array).
        assert len(ic) == 1
        # GIF file has 24 frames, so 24 / 3 equals 8.
        assert len(ic[0]) == 8

    def test_custom_load_func(self):
        def load_fn(x):
            return x

        ic = ImageCollection(os.pathsep.join(self.pattern), load_func=load_fn)
        assert_equal(ic[0], self.pattern[0])

    def test_concatenate(self):
        array = self.images_matched.concatenate()
        expected_shape = (len(self.images_matched), ) + self.images[0].shape
        assert_equal(array.shape, expected_shape)

    def test_concatenate_mismatched_image_shapes(self):
        with testing.raises(ValueError):
            self.images.concatenate()

    def test_multiimage_imagecollection(self):
        assert_equal(self.images_matched[0], self.frames_matched[0])
        assert_equal(self.images_matched[1], self.frames_matched[1])