Exemplo n.º 1
0
def avg_filter_defunct(im: np.ndarray, mask) -> np.ndarray:
    array = mask[0]
    # padding = 1
    padding = mask[1]
    # array, padding = mask
    im, im2 = prepare_image(im, padding, 'zero')  # preprocess image
    # get proper divisor by adding up weighted array values
    divisor = 0
    for i in array:
        for j in i:
            divisor = divisor + j

    # get image dimensions
    dimensions = im.shape
    height, width = dimensions
    # loops through image pixels, excluding zero edges
    for i in range(0 + padding, height - (1 + padding)):
        for j in range(0 + padding, width - (1 + padding)):
            # add up each row w/ each entry multiplied by its designated weight
            # hardcoded
            top = im[i - 1, j - 1] * array[0, 0] + im[i - 1, j] * array[
                1, 0] + im[i + 1, j + 1] * array[2, 0]
            mid = im[i, j - 1] * array[0, 1] + im[i, j] * array[1, 1] + im[
                i, j + 1] * array[2, 1]
            bot = im[i + 1, j - 1] * array[0, 2] + im[i + 1, j] * array[
                1, 2] + im[i + 1, j + 1] * array[2, 2]
            total = top + mid + bot  # add up rows
            avg = total / divisor  # average total using calculated divisor
            im2[i, j] = round(avg)  # round result
    # postprocess image to fix type
    im2 = pp_image(im2, False)
    return im2  # return processed image
Exemplo n.º 2
0
def guass_filter_1(im: np.ndarray, kernel: int) -> np.ndarray:
    # hardcode kernel size just in case
    kernel = 1
    # use premade gaussian filter for kernel 1
    _filter = masks.GAUS_3X3
    # create an appropriate guassian filter for given kernel size
    # _filter = create_gauss_conv(kernel)
    padding = kernel
    im, im2 = prepare_image(im, padding, 'zero')  # preprocess image

    dimensions = im.shape
    height, width = dimensions
    # loops through image pixels, excluding zero edges
    for i in range(0 + padding, height - (2 * padding)):
        for j in range(0 + padding, width - (2 * padding)):
            # hardcoded calcs and dimensions
            top = im[i - 1, j - 1] * _filter[0, 0] + im[i - 1, j] * _filter[
                1, 0] + im[i - 1, j + 1] * _filter[2, 0]
            mid = im[i, j - 1] * _filter[0, 1] + im[i, j] * _filter[1, 1] + im[
                i, j + 1] * _filter[2, 1]
            bot = im[i + 1, j - 1] * _filter[0, 2] + im[i + 1, j] * _filter[
                1, 2] + im[i + 1, j + 1] * _filter[2, 2]
            total = top + mid + bot  # add up rows
            im2[i, j] = round(total)  # round result
    # postprocess image to fix type
    im2 = pp_image(im2, False)
    return im2  # return processed image
Exemplo n.º 3
0
def lpl_sharpen(im: np.ndarray, mask) -> (np.ndarray, np.ndarray):
    array, padding = mask  # get filter, kernel/mask
    im_final = im.copy(
    )  # copy image for use in sharpened image before padding
    im, im_lpl = prepare_image(im, padding, 'repeat')  # preprocess images

    # code below is my logic for convulation
    # it is commented out so that the more efficient scipy convolve2d function can be used
    # code below being saved for future improvement and/or comparison with convolve2d
    """
    # get image dimensions
    dimensions = im.shape
    height, width = dimensions

    # loops through image pixels, excluding zero edges
    for i in range(0 + padding, height - (1 + padding)):
        for j in range(0 + padding, width - (1 + padding)):
            # add up each row w/ each entry multiplied by its designated weight
            # hardcoded
            top = im[i - 1, j - 1] * array[0, 0] + im[i - 1, j] * array[1, 0] + im[i - 1, j + 1] * array[2, 0]
            mid = im[i, j - 1] * array[0, 1] + im[i, j] * array[1, 1] + im[i, j + 1] * array[2, 1]
            bot = im[i + 1, j - 1] * array[0, 2] + im[i + 1, j] * array[1, 2] + im[i + 1, j + 1] * array[2, 2]
            total = top + mid + bot  # add up rows
            im3[i, j] = total

    """
    # convolve image with filter
    # specify 'valid' to use prepared padding and prevent double padding
    im_lpl = scipy.signal.convolve2d(im, array, 'valid')
    im_lpl = im_lpl.astype('float64')  # fix type for upcoming manipulation

    # display original laplacian sharpen outcome
    # cv2.imshow('test: ', im_lpl.astype('uint8'))
    # cv2.imshow('test2: ', im_lpl.astype('byte'))
    # cv2.waitKey(0)

    # normalize values
    im_lpl -= np.min(im_lpl)
    im_lpl /= np.max(im_lpl)
    im_lpl *= 255.0

    # display normalized laplacian sharpen outcome
    # cv2.imshow('test: ', im_lpl.astype('uint8'))
    # cv2.imshow('test2: ', im_lpl.astype('byte'))
    # cv2.waitKey(0)

    # add laplacian sharpen to image for final result
    im_final = im_final + im_lpl
    # normalize sharpened image
    im_final -= np.min(im_final)
    im_final /= np.max(im_final)
    im_final *= 255.0

    # postprocess images to fix type
    im_final = pp_image(im_final, False)
    im_lpl = pp_image(im_lpl, False)

    return im_final, im_lpl  # return processed images
Exemplo n.º 4
0
def prewitt_filter(im: np.ndarray):
    # prepare image and get new image
    im, im2 = utility.prepare_image(im, 1, 'repeat')
    # get x and y gradients by convolving with appropriate mask
    fx = scipy.signal.convolve2d(im, PREWITTX, 'valid')
    fy = scipy.signal.convolve2d(im, PREWITTY, 'valid')

    f = np.sqrt(fx ** 2 + fy ** 2)  # get magnitude
    f = f / np.max(f) * 255  # normalize
    d = np.arctan2(fy, fx)  # use arctan function to get radian degree

    # return x gradient, y gradient, magnitude, and directionals
    return fx, fy, f, d
Exemplo n.º 5
0
def guass_filter_3(im: np.ndarray, kernel: int) -> np.ndarray:
    # hardcode kernel just in case
    kernel = 3
    # use premade gaussian filter
    # it's already normalized
    _filter = masks.GAUS_7X7
    # calculate gaussian filter for specified kernel
    # _filter = create_gauss_conv(kernel)
    padding = kernel
    im, im2 = prepare_image(im, padding, 'zero')  # preprocess image

    dimensions = im.shape
    height, width = dimensions
    # loops through image pixels, excluding zero edges
    for i in range(0 + padding, height - (2 * padding)):
        for j in range(0 + padding, width - (2 * padding)):
            # harcoded calculations and dimensions
            line1 = im[i - 3, j - 3] * _filter[0, 0] + im[i - 3, j - 2] * _filter[1, 0] + \
                    im[i - 3, j - 1] * _filter[2, 0] + im[i - 3, j] * _filter[3, 0] + \
                    im[i - 3, j + 1] * _filter[4, 0] + im[i - 3, j + 2] * _filter[5, 0] + \
                    im[i - 3, j + 3] * _filter[6, 0]
            line2 = im[i - 2, j - 3] * _filter[0, 1] + im[i - 2, j - 2] * _filter[1, 1] + \
                    im[i - 2, j - 1] * _filter[2, 1] + im[i - 2, j] * _filter[3, 1] + \
                    im[i - 2, j + 1] * _filter[4, 1] + im[i - 2, j + 2] * _filter[5, 1] + \
                    im[i - 2, j + 3] * _filter[6, 1]
            line3 = im[i - 1, j - 3] * _filter[0, 2] + im[i - 1, j - 2] * _filter[1, 2] + \
                    im[i - 1, j - 1] * _filter[2, 2] + im[i - 1, j] * _filter[3, 2] + \
                    im[i - 1, j + 1] * _filter[4, 2] + im[i - 1, j + 2] * _filter[5, 2] + \
                    im[i - 1, j + 3] * _filter[6, 2]
            line4 = im[i, j - 3] * _filter[0, 3] + im[i, j - 2] * _filter[1, 3] + \
                    im[i, j - 1] * _filter[2, 3] + im[i, j] * _filter[3, 3] + \
                    im[i, j + 1] * _filter[4, 3] + im[i, j + 2] * _filter[5, 3] + \
                    im[i, j + 3] * _filter[6, 3]
            line5 = im[i + 1, j - 3] * _filter[0, 4] + im[i + 1, j - 2] * _filter[1, 4] + \
                    im[i + 1, j - 1] * _filter[2, 4] + im[i + 1, j] * _filter[3, 4] + \
                    im[i + 1, j + 1] * _filter[4, 4] + im[i + 1, j + 2] * _filter[5, 4] + \
                    im[i + 1, j + 3] * _filter[6, 4]
            line6 = im[i + 2, j - 3] * _filter[0, 5] + im[i + 2, j - 2] * _filter[1, 5] + \
                    im[i + 2, j - 1] * _filter[2, 5] + im[i + 2, j] * _filter[3, 5] + \
                    im[i + 2, j + 1] * _filter[4, 5] + im[i + 2, j + 2] * _filter[5, 5] + \
                    im[i + 2, j + 3] * _filter[6, 5]
            line7 = im[i + 3, j - 3] * _filter[0, 6] + im[i + 3, j - 2] * _filter[1, 6] + \
                    im[i + 3, j - 1] * _filter[2, 6] + im[i + 3, j] * _filter[3, 6] + \
                    im[i + 3, j + 1] * _filter[4, 6] + im[i + 3, j + 2] * _filter[5, 6] + \
                    im[i + 3, j + 3] * _filter[6, 6]
            total = line1 + line2 + line3 + line4 + line5 + line6 + line7  # add up rows
            im2[i, j] = round(total)  # round result
    # postprocess image to fix type
    im2 = pp_image(im2, False)
    # im = pp_image(im, False)
    return im2  # return processed image
Exemplo n.º 6
0
def guass_filter(im: np.ndarray,
                 kernel: int,
                 premade: bool = False) -> np.ndarray:
    # check if premade desired
    if premade:
        _filter = masks.kernel_to_filter(kernel)  # use premade guassian filter
    else:
        _filter = create_gauss_conv(
            kernel)  # create a guassian filter for the given kernel

    im, im_proc = prepare_image(im, kernel, 'zero')  # preprocess image
    # convolve image with filter
    # 'valid' specified to use preprocessed padding
    im_proc = scipy.signal.convolve2d(im, _filter, 'valid')

    # post process image and return it
    return pp_image(im_proc, False)
Exemplo n.º 7
0
def med_filter(im: np.ndarray, kernel: int) -> np.ndarray:
    im, im_proc = prepare_image(im, kernel, 'zero')  # preprocess image
    dimensions = im.shape
    # loop through image pixels, excluding padded edges
    # place pixels within sliding window in list
    window = []
    for i in range(0 + kernel, dimensions[0] - (2 * kernel)):
        for j in range(0 + kernel, dimensions[1] - (2 * kernel)):
            for k in range(-kernel, kernel + 1):
                for l in range(-kernel, kernel + 1):
                    window.append(im[i + k, j + l])
            # sort pixels from sliding window and thenq place median value in processed image
            window.sort()
            im_proc[i, j] = window[math.floor(((kernel * 2 + 1)**2) / 2)]
            window.clear()  # clear sliding window list for next iteration
    # return processed image
    return pp_image(im_proc, False)
Exemplo n.º 8
0
def med_filter_defunct(im: np.ndarray, kernel: int) -> np.ndarray:
    padding = kernel
    im, im2 = prepare_image(im, padding, 'zero')  # preprocess image
    # get image dimensions
    dimensions = im.shape
    height, width = dimensions
    # loops through image pixels, excluding zero edges
    window = []  # intialize list var
    for i in range(0 + padding, height - (2 * padding)):
        for j in range(0 + padding, width - (2 * padding)):
            # loop through sliding window size to handle various kernel sizes
            for k in range(-kernel, kernel + 1):
                for l in range(-kernel, kernel + 1):
                    window.append(im[i + k,
                                     j + l])  # add current pixel to list
            window.sort()  # sort list for median selection
            # calculate median location and it in dest pixel
            im2[i, j] = window[math.floor(((kernel * 2 + 1)**2) / 2)]
            window.clear()  # clear list for next calc
    # postprocess image to fix type
    im2 = pp_image(im2, False)
    return im2  # return processed image
Exemplo n.º 9
0
def avg_filter(im: np.ndarray,
               kernel: int = 1,
               weighted: bool = False,
               w_alt: bool = True):
    if weighted:
        if kernel != 1:
            return
        _filter = masks.WTD1_3X3 if w_alt else masks.WTD2_3X3
    else:
        _filter = np.ones((2 * kernel + 1, 2 * kernel + 1), dtype='int')

    im, im_proc = prepare_image(im, kernel, 'zero')  # preprocess image
    # loop through filter to get divisor
    div = 0  # init
    for i in _filter:
        for j in i:
            div = div + j

    im_proc = scipy.signal.convolve2d(im, _filter, 'valid')
    im_proc /= div

    return pp_image(im_proc, False)
Exemplo n.º 10
0
def box_filter_defunct(im: np.ndarray) -> np.ndarray:
    im_pad, final = prepare_image(im, 1, 'zero')  # prepare image
    # get dimensions of image
    dimensions = im_pad.shape
    height, width = dimensions
    # loop through pixels(excluding zero pads)
    for i in range(0, height - 2):
        for j in range(0, width - 2):
            # add up all pixels in 3x3 area centered on current pixel
            # hardcoded
            top = im_pad[i - 1, j - 1] + im_pad[i - 1, j] + im_pad[i + 1,
                                                                   j + 1]
            mid = im_pad[i, j - 1] + im_pad[i, j] + im_pad[i, j + 1]
            bot = im_pad[i + 1, j - 1] + im_pad[i + 1, j] + im_pad[i + 1,
                                                                   j + 1]
            total = top + mid + bot
            # average total
            avg = total / 9
            # place rounded average back into the current pixel
            final[i, j] = round(avg)
    # postprocess image to correct type
    final = pp_image(final, False)
    return final  # return the processed image