Example #1
0
def exponential_multiplicative_noise(image: ImageWrapper, _lambda,
                                     percentage: float):
    w, h = image.dimensions()
    fn = noise_percentage(
        multiplicative_noise(lambda: np.random.exponential(scale=1 / _lambda)),
        percentage, w, h)
    return pixel_transformation(image, fn)
Example #2
0
def rayleigh_multiplicative_noise(image: ImageWrapper, gamma,
                                  percentage: float):
    w, h = image.dimensions()
    fn = noise_percentage(
        multiplicative_noise(lambda: np.random.rayleigh(scale=gamma)),
        percentage, w, h)
    return pixel_transformation(image, fn)
Example #3
0
def gaussian_additive_noise(image: ImageWrapper, mu: float, sigma: float,
                            percentage: float):
    w, h = image.dimensions()
    fn = noise_percentage(
        additive_noise(lambda: np.random.normal(loc=mu, scale=sigma)),
        percentage, w, h)
    return pixel_transformation(image, fn)
Example #4
0
def pixel_to_pixel_operation(first_image: ImageWrapper,
                             second_image: ImageWrapper, operation):
    assert first_image.dimensions() == second_image.dimensions()

    w, h = first_image.dimensions()
    matrix = []

    for x in range(w):
        row = []
        for y in range(h):
            val = [
                operation(
                    first_image.get_pixel(x, y)[i],
                    second_image.get_pixel(x, y)[i])
                for i in range(len(first_image.get_pixel(x, y)))
            ]
            row.append(tuple(val))
        matrix.append(row)

    return matrix_to_image(normalized_matrix(matrix),
                           mode=first_image.get_mode())
Example #5
0
    def next_image(self, image: ImageWrapper) -> ImageWrapper:
        self.dimensions = image.dimensions()
        self.channels = image.channels

        if self.theta1 is None:
            self.get_avg_color()

        self.iterate()

        image_copy = image.copy()
        image_copy.channels = self.get_channels_with_borders()
        image_copy.draw_image()

        return image_copy
Example #6
0
def scalar_multiplication(first_image: ImageWrapper, scalar: float):
    w, h = first_image.dimensions()

    result = ImageWrapper.from_dimensions(w, h, mode=first_image.get_mode())

    for x in range(w):
        for y in range(h):
            val = [
                int(first_image.get_pixel(x, y)[i] * scalar) % 255
                for i in range(len(first_image.get_pixel(x, y)))
            ]
            result.set_pixel(x, y, tuple(val))

    return result
Example #7
0
def pixel_transformation(image: ImageWrapper, transformation_function):
    w, h = image.dimensions()

    matrix = []

    for x in range(w):
        row = []
        for y in range(h):
            val = transformation_function(x, y, image.get_pixel(x, y))

            row.append(tuple(val))
        matrix.append(row)

    return matrix_to_image(normalized_matrix(matrix), mode=image.get_mode())
Example #8
0
def salt_and_pepper(image: ImageWrapper, p0: float, p1: float,
                    percentage: float):
    def noise(x, y, val):
        res = []

        for v in val:
            rnd: float = np.random.uniform(0, 1)

            if rnd < p0:
                res.append(0)
            elif rnd > p1:
                res.append(255)
            else:
                res.append(v)

        return res

    w, h = image.dimensions()
    fn = noise_percentage(noise, percentage, w, h)
    return pixel_transformation(image, fn)
Example #9
0
def diffusion_step(c_calculator: Callable[[float], float], image: ImageWrapper,
                   step: float) -> ImageWrapper:
    w, h = image.dimensions()
    new_image: ImageWrapper = ImageWrapper.from_dimensions(
        w, h, image.get_mode())

    new_channels = []
    for channel in image.channels:
        new_channel = np.zeros(shape=(w, h))
        for x in range(w):
            for y in range(h):
                new_channel[x, y] = channel[x, y] + step * (
                    calculate_direction(x, y, channel, 'N', c_calculator) +
                    calculate_direction(x, y, channel, 'S', c_calculator) +
                    calculate_direction(x, y, channel, 'E', c_calculator) +
                    calculate_direction(x, y, channel, 'O', c_calculator))
        new_channels.append(new_channel)

    new_image.channels = new_channels
    new_image.draw_image()
    return new_image
Example #10
0
def bilateral_filter(image: ImageWrapper, window_dim: Tuple[int, int],
                     sigma_s: float, sigma_r: float) -> ImageWrapper:
    window_w, window_h = window_dim
    if window_w % 2 == 0 or window_h % 2 == 0:
        raise ValueError("Window dimensions should be odd numbers")

    w, h = image.dimensions()
    new_image: ImageWrapper = ImageWrapper.from_dimensions(
        w, h, image.get_mode())

    new_channels = [np.zeros(shape=(w, h))] * len(image.channels)
    for y in range(h):
        for x in range(w):
            values = calculate_filter(image.channels, x, y, window_dim,
                                      sigma_s, sigma_r)
            for val, channel in zip(values, new_channels):
                channel[x, y] = val

    new_image.channels = new_channels
    new_image.draw_image()
    return new_image