|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def get_neighbors_pixel( |
| 6 | + image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int |
| 7 | +) -> int: |
| 8 | + """ |
| 9 | + Comparing local neighborhood pixel value with threshold value of centre pixel. |
| 10 | + Exception is required when neighborhood value of a center pixel value is null. |
| 11 | + i.e. values present at boundaries. |
| 12 | +
|
| 13 | + :param image: The image we're working with |
| 14 | + :param x_coordinate: x-coordinate of the pixel |
| 15 | + :param y_coordinate: The y coordinate of the pixel |
| 16 | + :param center: center pixel value |
| 17 | + :return: The value of the pixel is being returned. |
| 18 | + """ |
| 19 | + |
| 20 | + try: |
| 21 | + return int(image[x_coordinate][y_coordinate] >= center) |
| 22 | + except (IndexError, TypeError): |
| 23 | + return 0 |
| 24 | + |
| 25 | + |
| 26 | +def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int: |
| 27 | + """ |
| 28 | + It takes an image, an x and y coordinate, and returns the |
| 29 | + decimal value of the local binary patternof the pixel |
| 30 | + at that coordinate |
| 31 | +
|
| 32 | + :param image: the image to be processed |
| 33 | + :param x_coordinate: x coordinate of the pixel |
| 34 | + :param y_coordinate: the y coordinate of the pixel |
| 35 | + :return: The decimal value of the binary value of the pixels |
| 36 | + around the center pixel. |
| 37 | + """ |
| 38 | + center = image[x_coordinate][y_coordinate] |
| 39 | + powers = [1, 2, 4, 8, 16, 32, 64, 128] |
| 40 | + |
| 41 | + # skip get_neighbors_pixel if center is null |
| 42 | + if center is None: |
| 43 | + return 0 |
| 44 | + |
| 45 | + # Starting from the top right, assigning value to pixels clockwise |
| 46 | + binary_values = [ |
| 47 | + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center), |
| 48 | + get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center), |
| 49 | + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center), |
| 50 | + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center), |
| 51 | + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center), |
| 52 | + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center), |
| 53 | + get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center), |
| 54 | + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center), |
| 55 | + ] |
| 56 | + |
| 57 | + # Converting the binary value to decimal. |
| 58 | + return sum( |
| 59 | + binary_value * power for binary_value, power in zip(binary_values, powers) |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "main": |
| 64 | + |
| 65 | + # Reading the image and converting it to grayscale. |
| 66 | + image = cv2.imread( |
| 67 | + "digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE |
| 68 | + ) |
| 69 | + |
| 70 | + # Create a numpy array as the same height and width of read image |
| 71 | + lbp_image = np.zeros((image.shape[0], image.shape[1])) |
| 72 | + |
| 73 | + # Iterating through the image and calculating the |
| 74 | + # local binary pattern value for each pixel. |
| 75 | + for i in range(0, image.shape[0]): |
| 76 | + for j in range(0, image.shape[1]): |
| 77 | + lbp_image[i][j] = local_binary_value(image, i, j) |
| 78 | + |
| 79 | + cv2.imshow("local binary pattern", lbp_image) |
| 80 | + cv2.waitKey(0) |
| 81 | + cv2.destroyAllWindows() |
0 commit comments