Exemplo n.º 1
0
def linear_adjustment(a_img: ImageImpl) -> ImageImpl:
    """
    Given a matrix image representation apply, if necessary, linear transformation
    to bring values in the pixel value range (0, 255).
    :param a_img: numpy array of 2 or 3 dimensions.
    :return: np.ndarray of same shape with values in range
    """

    min_value = a_img.min_value()
    max_value = a_img.max_value()

    if MAX_PIXEL_VALUE >= max_value and MIN_PIXEL_VALUE <= min_value:
        # values are in range
        return a_img  # pixels should be ints no floats
    # if values are out of range, adjust based on current values

    if max_value == min_value:
        # a transformation should only shift values in this case
        slope = 0
    else:
        slope = (MAX_PIXEL_VALUE - MIN_PIXEL_VALUE) / (max_value - min_value)
    if max_value == min_value:
        if max_value > MAX_PIXEL_VALUE:
            constant = MAX_PIXEL_VALUE
        elif min_value < MIN_PIXEL_VALUE:
            constant = MIN_PIXEL_VALUE
        else:
            constant = max_value
    else:
        # as we want the tranformation to map MIN_PIXEL_VALUE to the min_value found
        # we just solve y = mx + b for known x, y and m
        constant = MIN_PIXEL_VALUE - slope * min_value

    return a_img.mul_scalar(slope).add_scalar(constant)
Exemplo n.º 2
0
def negative_img_fun(a_img: ImageImpl) -> ImageImpl:
    """
    Given an image matrix representation, invert pixel values.
    Following the function:
    F: PixelDomain -> PixelDomain/
    F(r) = -r + Max_Pixel_value
    :param a_img: matrix image representation
    :return: transformed matrix
    """

    return a_img.mul_scalar(-1).add_scalar(MAX_PIXEL_VALUE)
Exemplo n.º 3
0
def susan_detection(a_img: ImageImpl, threshold: int, low_filter: float,
                    high_filter: float, color: int) -> ImageImpl:
    """
    :param a_img:
    :param kernel_size:
    :param threshold:
    :return:
    """
    # circ_kernel = circular_kernel(7)
    circ_kernel = np.array([
        [0, 0, 1, 1, 1, 0, 0],
        [0, 1, 1, 1, 1, 1, 0],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [0, 1, 1, 1, 1, 1, 0],
        [0, 0, 1, 1, 1, 0, 0],
    ])

    original = ImageImpl(a_img.get_array())
    a_img.apply_filter(
        circ_kernel,
        lambda m: _calculate_c_for_susan(m, circ_kernel, threshold))
    # a_img.array = np.uint8(a_img.array > 0.75)

    border_img = np.uint8(a_img.array > low_filter)
    b_img = border_img - np.uint8(a_img.array > high_filter)
    border_img = np.zeros((border_img.shape[0], border_img.shape[1], 3))
    # because we only have 3 channels
    color = color % 3
    border_img[:, :, color] = b_img[:, :, 0]

    border_img = ImageImpl(border_img)

    result = border_img.mul_scalar(255)

    original = original.to_rgb()

    # result.add_image(original)
    return original.add(result)