Пример #1
0
    def convolve2d(img, kernel):
        """
        Performs convolution in 2D array.
        :param img: numpy 2D array: input array
        :param kernel: array 3x3: convolution kernel
        :return: numpy 2D array: result of convolution, image has same shape as input
        """

        kernel = np.flip(kernel, 0)
        kernel = np.flip(kernel, 1)

        output = np.zeros(img.shape)

        window_size = 3

        padded_input = ImageUtilities.pad_image(img, 1)

        h, w = img.shape
        for x in range(0, w):
            for y in range(0, h):
                window = padded_input[y:y + window_size, x:x + window_size]
                window = np.multiply(window, kernel)
                output[y, x] = window.sum()

        return output
Пример #2
0
    def median_filter(img, window_size):
        """
        Filters image using median filter.
        :param img: numpy 2D array: array to filter
        :param window_size: int: size of filter
        :return: numpy 2D array: filtered image, same shape as input
        """

        if Config.USE_CV2_FILTERS:
            return cv2.medianBlur(img, 3)

        output = np.zeros(img.shape)
        padded_input = ImageUtilities.pad_image(img, 1)

        half_win_size = window_size // 2

        h, w = img.shape

        mid = (window_size**2) // 2

        for x in range(0, w):
            for y in range(0, h):
                window = padded_input[y:y + window_size, x:x + window_size]
                window = window.flatten()
                window.sort()
                val = window[mid]
                output[y][x] = val

        return output