Exemplo n.º 1
0
    def get_image_clusters(self, cluster_size, hard_radius=1., noise=0,
                           signal_dev=0, size_dev=0, angle=None):
        N = self.repeats
        separation = [int(sep + 2 * hard_radius * s)
                      for (sep, s) in zip(self.separation, self.size)]
        margin = self.separation
        Nsqrt = int(N**(1/self.ndim) + 0.9999)
        pos = np.meshgrid(*[np.arange(0, s * Nsqrt, s) for s in separation],
                          indexing='ij')
        pos = np.array([p.ravel() for p in pos], dtype=np.float).T[:N] + margin
        pos += (np.random.random(pos.shape) - 0.5)  #randomize subpixel location
        if self.ndim == 2 and angle is None:
            angles = np.random.uniform(0, 2*np.pi, N)
        elif self.ndim == 3 and angle is None:
            angles = np.random.uniform(0, 2*np.pi, (N, 3))
        elif self.ndim == 2 and angle is not None:
            angles = np.repeat(angle, N)
        elif self.ndim == 3 and angle is not None:
            angles = np.repeat([angle], N, axis=0)

        shape = tuple(np.max(pos, axis=0).astype(np.int) + margin)
        if signal_dev > 0:
            signal = self.signal * np.random.uniform(1-signal_dev, 1+signal_dev,
                                                     N)
        else:
            signal = np.repeat(self.signal, N)
        if size_dev > 0:
            size = np.array([self.size]) * np.random.uniform(1-size_dev,
                                                             1+size_dev,
                                                             (N, 1))
        else:
            size = np.repeat([self.size], N, axis=0)

        image = np.zeros(shape, dtype=self.dtype)
        coords = []
        for _pos, _signal, _size, _angle in zip(pos, signal, size, angles):
            coords.extend(draw_cluster(image, _pos, _size, cluster_size,
                                       hard_radius, _angle, max_value=_signal,
                                       feat_func=self.feat_func,
                                       **self.feat_kwargs))
        coords = np.array(coords)
        signal = np.repeat(signal, cluster_size)
        size = np.repeat(size, cluster_size, axis=0)
        if self.isotropic:
            size = size[:, 0]

        if noise > 0:
            image = image + np.random.poisson(noise, shape)
            if image.max() <= 255:
                image = image.astype(np.uint8)

        return image, (coords, signal, size), (pos, angles)
Exemplo n.º 2
0
    def get_image_clusters(self, cluster_size, hard_radius=1., noise=0,
                           signal_dev=0, size_dev=0, angle=None):
        N = self.repeats
        separation = [int(sep + 2 * hard_radius * s)
                      for (sep, s) in zip(self.separation, self.size)]
        margin = self.separation
        Nsqrt = int(N**(1/self.ndim) + 0.9999)
        pos = np.meshgrid(*[np.arange(0, s * Nsqrt, s) for s in separation],
                          indexing='ij')
        pos = np.array([p.ravel() for p in pos], dtype=np.float).T[:N] + margin
        pos += (np.random.random(pos.shape) - 0.5)  #randomize subpixel location
        if self.ndim == 2 and angle is None:
            angles = np.random.uniform(0, 2*np.pi, N)
        elif self.ndim == 3 and angle is None:
            angles = np.random.uniform(0, 2*np.pi, (N, 3))
        elif self.ndim == 2 and angle is not None:
            angles = np.repeat(angle, N)
        elif self.ndim == 3 and angle is not None:
            angles = np.repeat([angle], N, axis=0)

        shape = tuple(np.max(pos, axis=0).astype(np.int) + margin)
        if signal_dev > 0:
            signal = self.signal * np.random.uniform(1-signal_dev, 1+signal_dev,
                                                     N)
        else:
            signal = np.repeat(self.signal, N)
        if size_dev > 0:
            size = np.array([self.size]) * np.random.uniform(1-size_dev,
                                                             1+size_dev,
                                                             (N, 1))
        else:
            size = np.repeat([self.size], N, axis=0)

        image = np.zeros(shape, dtype=self.dtype)
        coords = []
        for _pos, _signal, _size, _angle in zip(pos, signal, size, angles):
            coords.extend(draw_cluster(image, _pos, _size, cluster_size,
                                       hard_radius, _angle, max_value=_signal,
                                       feat_func=self.feat_func,
                                       **self.feat_kwargs))
        coords = np.array(coords)
        signal = np.repeat(signal, cluster_size)
        size = np.repeat(size, cluster_size, axis=0)
        if self.isotropic:
            size = size[:, 0]

        if noise > 0:
            image = image + np.random.poisson(noise, shape)
            if image.max() <= 255:
                image = image.astype(np.uint8)

        return image, (coords, signal, size), (pos, angles)