Ejemplo n.º 1
0
def matrix_to_image(matrix: List[List[tuple]], mode=None) -> ImageWrapper:
    w: int = len(matrix)
    h: int = len(matrix[0])
    result: ImageWrapper = ImageWrapper.from_dimensions(w, h, mode)
    for x in range(w):
        for y in range(h):
            result.set_pixel(x, y, tuple([int(a) for a in matrix[x][y]]))
    return result
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def harris_corner_detection(img: ImageWrapper, percentile: float,
                            gauss_filter_sigma: float,
                            k: float) -> ImageWrapper:
    if len(img.channels) > 1:
        channel = rgb2gray(img.channels)
    else:
        channel = img.channels[0]

    gx, gy = directional_derivatives(channel)

    ix2 = np.ndarray(shape=channel.shape)
    iy2 = np.ndarray(shape=channel.shape)
    ixy = np.ndarray(shape=channel.shape)

    w, h = channel.shape

    im = ImageWrapper.from_dimensions(h, w)
    im.channels = [channel, channel, channel]
    im.draw_image()
    plt.imshow(im.image_element)

    plt.show()

    for x in range(w):
        for y in range(h):
            ix2[x, y] = np.power(gx[x, y], 2)
            iy2[x, y] = np.power(gy[x, y], 2)
            ixy[x, y] = gx[x, y] * gy[x, y]

    ix2 = gaussian_window(ix2, gauss_filter_sigma)
    iy2 = gaussian_window(iy2, gauss_filter_sigma)
    ixy = gaussian_window(ixy, gauss_filter_sigma)

    r = np.ndarray(shape=channel.shape)

    for x in range(w):
        for y in range(h):
            r[x, y] = (ix2[x, y] * iy2[x, y] - np.power(
                ixy[x, y], 2)) - k * np.power(ix2[x, y] + iy2[x, y], 2)

    r_max = r.max()
    r_min = r.min()

    normalized_r = np.ndarray(shape=r.shape)
    for x in range(w):
        for y in range(h):
            normalized_r[x, y] = 255 * (r[x, y] - r_min) / (r_max - r_min)

    imr = ImageWrapper.from_dimensions(w, h)
    imr.channels = [normalized_r, normalized_r, normalized_r]
    imr.draw_image()
    plt.imshow(imr.image_element)

    plt.show()

    r_threshold = np.percentile(r, percentile)

    print(r_threshold)

    new_img = ImageWrapper.from_dimensions(h, w, 'RGB')
    for x in range(w):
        for y in range(h):
            if r[x, y] >= r_threshold:
                new_img.channels[0][x, y] = 255
                new_img.channels[1][x, y] = 0
                new_img.channels[2][x, y] = 0
            else:
                for i in range(3):
                    if len(img.channels) > 1:
                        new_img.channels[i][x, y] = img.channels[i][x, y]
                    else:
                        new_img.channels[i][x, y] = channel[x, y]

    new_img.draw_image()

    return new_img