def apply_gradient(img, filter_matrix):
        '''
        Apply gradient using a single filter_matrix (3x3).
        '''
        # filter_height, filter_width = util.get_dimensions(
        #     filter_matrix)
        # assert filter_height != 3, "Filter Matrix must have height = 3 instead of " + \
        #     str(filter_height)
        # assert filter_width != 3, "Filter Matrix must have width = 3 instead of " + \
        #     str(filter_width)

        height, width = util.get_dimensions(img)

        # define image with 0s
        new_gradient_image = np.zeros((height, width), np.uint8)
        if len(img.shape) == 2:
            for i in range(1, height - 1):
                for j in range(1, width - 1):
                    grad = ImageFilter.apply_gradient_core(
                        filter_matrix, img, i, j)
                    new_gradient_image[i - 1, j - 1] = abs(grad)
        else:
            R = ImageFilter.apply_gradient(img[:, :, 0], filter_matrix)
            G = ImageFilter.apply_gradient(img[:, :, 1], filter_matrix)
            B = ImageFilter.apply_gradient(img[:, :, 2], filter_matrix)
            output = np.zeros((R.shape[0], R.shape[1], 3), dtype=np.uint8)
            output[:, :, 0] = R
            output[:, :, 1] = G
            output[:, :, 2] = B
            new_gradient_image = output

        return new_gradient_image
    def adjust_brightness(img, br):
        """
        0 < br < 1: decrease brightness
        br = 1: no changes
        br >: increase brightness
        """
        height, width = util.get_dimensions(img)
        obtained = np.zeros((height, width, 3), np.uint8)

        for i in range(height):
            for j in range(width):
                for k in range(img.shape[2]):
                    b = img[i][j][k] * br
                    if b > 255:
                        b = 255
                    obtained[i][j][k] = b
        return obtained.astype(np.uint8)
    def apply_sobel(img):
        # Horizontal sobel matrix
        horizontal = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

        # Vertical sobel matrix
        vertical = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

        height, width = util.get_dimensions(img)

        # define images with 0s
        new_horizontal_image = np.zeros((height, width), np.uint8)
        new_vertical_image = np.zeros((height, width), np.uint8)
        new_gradient_image = np.zeros((height, width), np.uint8)
        if len(img.shape) == 2:
            # # define images with 0s
            # new_horizontal_image = np.zeros((height, width), np.uint8)
            # new_vertical_image = np.zeros((height, width), np.uint8)
            # new_gradient_image = np.zeros((height, width), np.uint8)
            for i in range(1, height - 1):
                for j in range(1, width - 1):
                    horizontal_grad = ImageFilter.apply_gradient_core(
                        horizontal, img, i, j)
                    new_horizontal_image[i - 1, j - 1] = abs(horizontal_grad)

                    vertical_grad = ImageFilter.apply_gradient_core(
                        vertical, img, i, j)
                    new_vertical_image[i - 1, j - 1] = abs(vertical_grad)

                    # Edge Magnitude
                    new_gradient_image[i - 1, j - 1] = np.sqrt(
                        pow(horizontal_grad, 2.0) + pow(vertical_grad, 2.0))
        else:
            R = ImageFilter.apply_sobel(img[:, :, 0])
            G = ImageFilter.apply_sobel(img[:, :, 1])
            B = ImageFilter.apply_sobel(img[:, :, 2])
            output = np.zeros((R.shape[0], R.shape[1], 3), dtype=np.uint8)
            output[:, :, 0] = R
            output[:, :, 1] = G
            output[:, :, 2] = B
            new_gradient_image = output

        return new_gradient_image
    def apply_piecewise_linear(img, coordinates_x, coordinates_y):
        """Apply Piecewise Linear filter on an image basead on an group of coordinates.

        Parameters
        ----------
        img : numpy array
            The target image where the filter would be applied
        coordinates_x : array
            The coordinates X from all points to the interpolated already in the desired order.
        coordinates_y : array
            The coordinates Y from all points to the interpolated already in the desired order.

        Returns
        -------
        numpy array
            an array representing the obtained image after apply the filter
        """
        x = np.array(range(0, _MAX_PIXEL + 1), dtype=np.uint8)
        interp = np.interp(x, coordinates_x, coordinates_y)
        obtained = img.copy()
        height, width = util.get_dimensions(obtained)
        if len(img.shape) == 2:
            for i in range(height):
                for j in range(width):
                    index = int(np.round(obtained[i][j]))
                    obtained[i][j] = interp[index]
        else:
            R = ImageFilter.apply_piecewise_linear(img[:, :, 0], coordinates_x,
                                                   coordinates_y)
            G = ImageFilter.apply_piecewise_linear(img[:, :, 1], coordinates_x,
                                                   coordinates_y)
            B = ImageFilter.apply_piecewise_linear(img[:, :, 2], coordinates_x,
                                                   coordinates_y)
            output = np.zeros((R.shape[0], R.shape[1], 3), dtype=np.uint8)
            output[:, :, 0] = R
            output[:, :, 1] = G
            output[:, :, 2] = B
            obtained = output

        return obtained
Exemplo n.º 5
0
 def adjust_intensity (img, factor):
     '''
     Adjust image hue using a mulplication factor.
     Input: image and factor in [0.0, 1.0].
     Output: image with intensity adjusted
     '''
     height,width = util.get_dimensions(img)
     obtained = np.zeros_like(img)
     for row in range(height):
         for col in range(width):
             r = img[row][col][0]
             g = img[row][col][1]
             b = img[row][col][2]
             h,s,i = converter.rgb_to_hsi(r,g,b)
             new_intensity = i * factor #Intensity value is [0, 1]
             if new_intensity > 1:
                 new_intensity = 1
             r,g,b = converter.hsi_to_rgb(h,s,new_intensity)
             obtained[row][col][0] = r
             obtained[row][col][1] = g
             obtained[row][col][2] = b
     return obtained
Exemplo n.º 6
0
 def adjust_hue (img, factor):
     '''
     Adjust image hue using a mulplication factor.
     Input: image and factor in [0.0, 1.0].
     Output: image with hue adjusted
     '''
     height,width = util.get_dimensions(img)
     obtained = np.zeros_like(img)
     for row in range(height):
         for col in range(width):
             r = img[row][col][0]
             g = img[row][col][1]
             b = img[row][col][2]
             h,s,i = converter.rgb_to_hsi(r,g,b)
             new_hue = h * factor #Hue value is [0, 360]
             if new_hue > 360:
                 new_hue = 360
             r,g,b = converter.hsi_to_rgb(new_hue,s,i)
             obtained[row][col][0] = r
             obtained[row][col][1] = g
             obtained[row][col][2] = b
     return obtained
 def get_geometric_mean(matrix):
     prod_value = np.prod(matrix)
     height, width = util.get_dimensions(matrix)
     counter = height * width
     result = prod_value**(1.0 / counter)
     return np.around(result, decimals=3)
 def get_arithmetic_mean(neighbors):
     sum_value = np.sum(neighbors)
     height, width = util.get_dimensions(neighbors)
     return sum_value / (height * width)