From 5f2eb9f99afec31cbd03218e5bade9b690442c0c Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Thu, 4 Aug 2022 17:25:12 +0800 Subject: [PATCH 01/24] add algorithm local binary pattern --- .../filters/local_binary_pattern.py | 78 +++++++++++++++++++ .../test_digital_image_processing.py | 20 +++++ 2 files changed, 98 insertions(+) create mode 100644 digital_image_processing/filters/local_binary_pattern.py diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py new file mode 100644 index 000000000000..34aecc7673cc --- /dev/null +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -0,0 +1,78 @@ +import cv2 +import numpy as np + + +def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: + """ + Comparing local neighborhood pixel value with threshold value of centre pixel. + Exception is required when neighborhood value of a center pixel value is null. + i.e. values present at boundaries. + + :param image: The image we're working with + :param x: x-coordinate of the center pixel + :param y: The y coordinate of the pixel + :param center: center pixel value + :return: The value of the pixel is being returned. + """ + + value = 0 + + try: + if image[x][y] >= center: + value = 1 + except: + + pass + return value + + +def local_binary_value(image: np.ndarray, x: int, y: int) -> int: + """ + It takes an image, an x and y coordinate, and returns the decimal value of the local binary pattern + of the pixel at that coordinate + + :param image: the image to be processed + :param x: x coordinate of the pixel + :param y: the y coordinate of the pixel + :return: The decimal value of the binary value of the pixels around the center pixel. + """ + binary_value = [] + center = image[x][y] + powers = [1, 2, 4, 8, 16, 32, 64, 128] + decimal_val = 0 + + # Starting from top right,assigning value to pixels clockwise + binary_value.append(get_neighbors_pixel(image, x - 1, y + 1, center)) + binary_value.append(get_neighbors_pixel(image, x, y + 1, center)) + binary_value.append(get_neighbors_pixel(image, x - 1, y, center)) + binary_value.append(get_neighbors_pixel(image, x + 1, y + 1, center)) + binary_value.append(get_neighbors_pixel(image, x + 1, y, center)) + binary_value.append(get_neighbors_pixel(image, x + 1, y - 1, center)) + binary_value.append(get_neighbors_pixel(image, x, y - 1, center)) + binary_value.append(get_neighbors_pixel(image, x - 1, y - 1, center)) + + # Converting the binary value to decimal. + for i in range(len(binary_value)): + decimal_val += binary_value[i] * powers[i] + + return decimal_val + + +if __name__ == "main": + + # Reading the image and converting it to grayscale. + image = cv2.imread("../image_data/lena.jpg", cv2.IMREAD_GRAYSCALE) + + cv2.imshow("Original Image", image) + + # Create a numpy array as the same height and width of read image + lbp_image = np.zeros((image.shape[0], image.shape[1])) + + # Iterating through the image and calculating the local binary pattern value for each pixel. + for i in range(0, image.shape[0]): + for j in range(0, image.shape[1]): + lbp_image[i][j] = local_binary_value(image, i, j) + + cv2.imshow("Local Binary Pattern", lbp_image) + cv2.waitKey(0) + cv2.destroyAllWindows() diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index 40f2f7b83b6d..c46700371aac 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -1,6 +1,7 @@ """ PyTest's for Digital Image Processing """ +import numpy as np from cv2 import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uint8 from PIL import Image @@ -15,6 +16,8 @@ from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs +from digital_image_processing.filters import local_binary_pattern as lbp + img = imread(r"digital_image_processing/image_data/lena_small.jpg") gray = cvtColor(img, COLOR_BGR2GRAY) @@ -91,3 +94,20 @@ def test_nearest_neighbour( nn = rs.NearestNeighbour(imread(file_path, 1), 400, 200) nn.process() assert nn.output.any() + + +def test_local_binary_pattern(): + file_path: str = "digital_image_processing/image_data/lena.jpg" + + # Reading the image and converting it to grayscale. + image = imread(file_path, 1) + + # Create a numpy array as the same height and width of read image + lbp_image = np.zeros((image.shape[0], image.shape[1])) + + # Iterating through the image and calculating the local binary pattern value for each pixel. + for i in range(0, image.shape[0]): + for j in range(0, image.shape[1]): + lbp_image[i][j] = lbp.local_binary_value(image, i, j) + + assert lbp_image.any() From 31eadf6ed58a6adf262a3593dfa2113066086659 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Thu, 4 Aug 2022 17:43:50 +0800 Subject: [PATCH 02/24] fix failed test for local binary pattern --- digital_image_processing/test_digital_image_processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index c46700371aac..0e3b78417110 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -100,7 +100,7 @@ def test_local_binary_pattern(): file_path: str = "digital_image_processing/image_data/lena.jpg" # Reading the image and converting it to grayscale. - image = imread(file_path, 1) + image = imread(file_path, 0) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) From a7a15323f36c5592821ac1deddd4b65a00fe9a57 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 4 Aug 2022 09:44:54 +0000 Subject: [PATCH 03/24] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 843ff77bb67b..281c821ae4ab 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -229,6 +229,7 @@ * [Convolve](digital_image_processing/filters/convolve.py) * [Gabor Filter](digital_image_processing/filters/gabor_filter.py) * [Gaussian Filter](digital_image_processing/filters/gaussian_filter.py) + * [Local Binary Pattern](digital_image_processing/filters/local_binary_pattern.py) * [Median Filter](digital_image_processing/filters/median_filter.py) * [Sobel Filter](digital_image_processing/filters/sobel_filter.py) * Histogram Equalization From afe232072258f81ca90c6af1073968f3aab6b817 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Thu, 4 Aug 2022 17:54:25 +0800 Subject: [PATCH 04/24] fix detected precommit-error --- .../filters/local_binary_pattern.py | 11 +++++++---- .../test_digital_image_processing.py | 7 ++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 34aecc7673cc..dfc5c70e407a 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -28,13 +28,15 @@ def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: def local_binary_value(image: np.ndarray, x: int, y: int) -> int: """ - It takes an image, an x and y coordinate, and returns the decimal value of the local binary pattern - of the pixel at that coordinate + It takes an image, an x and y coordinate, and returns the + decimal value of the local binary patternof the pixel + at that coordinate :param image: the image to be processed :param x: x coordinate of the pixel :param y: the y coordinate of the pixel - :return: The decimal value of the binary value of the pixels around the center pixel. + :return: The decimal value of the binary value of the pixels + around the center pixel. """ binary_value = [] center = image[x][y] @@ -68,7 +70,8 @@ def local_binary_value(image: np.ndarray, x: int, y: int) -> int: # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) - # Iterating through the image and calculating the local binary pattern value for each pixel. + # Iterating through the image and calculating the + # local binary pattern value for each pixel. for i in range(0, image.shape[0]): for j in range(0, image.shape[1]): lbp_image[i][j] = local_binary_value(image, i, j) diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index 0e3b78417110..e943c95a8b1b 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -19,7 +19,7 @@ from digital_image_processing.filters import local_binary_pattern as lbp -img = imread(r"digital_image_processing/image_data/lena_small.jpg") +img = imread("digital_image_processing/image_data/lena_small.jpg") gray = cvtColor(img, COLOR_BGR2GRAY) @@ -100,12 +100,13 @@ def test_local_binary_pattern(): file_path: str = "digital_image_processing/image_data/lena.jpg" # Reading the image and converting it to grayscale. - image = imread(file_path, 0) + image = imread(file_path, COLOR_BGR2GRAY) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) - # Iterating through the image and calculating the local binary pattern value for each pixel. + # Iterating through the image and calculating the local binary pattern value + # for each pixel. for i in range(0, image.shape[0]): for j in range(0, image.shape[1]): lbp_image[i][j] = lbp.local_binary_value(image, i, j) From a902666afb4c870e1389f9642b54465528f10090 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Thu, 4 Aug 2022 18:06:59 +0800 Subject: [PATCH 05/24] fix precommit error --- .../filters/local_binary_pattern.py | 12 ++++++------ .../test_digital_image_processing.py | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index dfc5c70e407a..c79fefe6d95b 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -20,22 +20,22 @@ def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: try: if image[x][y] >= center: value = 1 - except: + except Exception as ex: + print(ex) - pass return value def local_binary_value(image: np.ndarray, x: int, y: int) -> int: """ - It takes an image, an x and y coordinate, and returns the - decimal value of the local binary patternof the pixel + It takes an image, an x and y coordinate, and returns the + decimal value of the local binary patternof the pixel at that coordinate :param image: the image to be processed :param x: x coordinate of the pixel :param y: the y coordinate of the pixel - :return: The decimal value of the binary value of the pixels + :return: The decimal value of the binary value of the pixels around the center pixel. """ binary_value = [] @@ -70,7 +70,7 @@ def local_binary_value(image: np.ndarray, x: int, y: int) -> int: # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) - # Iterating through the image and calculating the + # Iterating through the image and calculating the # local binary pattern value for each pixel. for i in range(0, image.shape[0]): for j in range(0, image.shape[1]): diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index e943c95a8b1b..f3c1befd00ec 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -19,7 +19,7 @@ from digital_image_processing.filters import local_binary_pattern as lbp -img = imread("digital_image_processing/image_data/lena_small.jpg") +img = imread(r"digital_image_processing/image_data/lena_small.jpg") gray = cvtColor(img, COLOR_BGR2GRAY) @@ -100,7 +100,7 @@ def test_local_binary_pattern(): file_path: str = "digital_image_processing/image_data/lena.jpg" # Reading the image and converting it to grayscale. - image = imread(file_path, COLOR_BGR2GRAY) + image = imread(file_path, 0) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) From 097d9cee918d5d83c9b0ff38bd79aad0940fa039 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Thu, 4 Aug 2022 22:34:38 +0800 Subject: [PATCH 06/24] final check --- .../filters/local_binary_pattern.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index c79fefe6d95b..ff42a2fcc04c 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -20,8 +20,8 @@ def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: try: if image[x][y] >= center: value = 1 - except Exception as ex: - print(ex) + except Exception: + pass return value @@ -63,9 +63,9 @@ def local_binary_value(image: np.ndarray, x: int, y: int) -> int: if __name__ == "main": # Reading the image and converting it to grayscale. - image = cv2.imread("../image_data/lena.jpg", cv2.IMREAD_GRAYSCALE) - - cv2.imshow("Original Image", image) + image = cv2.imread( + "digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE + ) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) From d4a9574a7a939192a43aac5c4ef2d05f7fa016b1 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Thu, 4 Aug 2022 23:25:31 +0800 Subject: [PATCH 07/24] Add descriptive name for parameters x and y --- .../filters/local_binary_pattern.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index ff42a2fcc04c..fe40bdd21991 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -2,15 +2,15 @@ import numpy as np -def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: +def get_neighbors_pixel(image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int) -> int: """ Comparing local neighborhood pixel value with threshold value of centre pixel. Exception is required when neighborhood value of a center pixel value is null. i.e. values present at boundaries. :param image: The image we're working with - :param x: x-coordinate of the center pixel - :param y: The y coordinate of the pixel + :param x_coordinate: x-coordinate of the pixel + :param y_coordinate: The y coordinate of the pixel :param center: center pixel value :return: The value of the pixel is being returned. """ @@ -18,7 +18,7 @@ def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: value = 0 try: - if image[x][y] >= center: + if image[x_coordinate][y_coordinate] >= center: value = 1 except Exception: pass @@ -26,32 +26,32 @@ def get_neighbors_pixel(image: np.ndarray, x: int, y: int, center: int) -> int: return value -def local_binary_value(image: np.ndarray, x: int, y: int) -> int: +def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int: """ It takes an image, an x and y coordinate, and returns the decimal value of the local binary patternof the pixel at that coordinate :param image: the image to be processed - :param x: x coordinate of the pixel - :param y: the y coordinate of the pixel + :param x_coordinate: x coordinate of the pixel + :param y_coordinate: the y coordinate of the pixel :return: The decimal value of the binary value of the pixels around the center pixel. """ binary_value = [] - center = image[x][y] + center = image[x_coordinate][y_coordinate] powers = [1, 2, 4, 8, 16, 32, 64, 128] decimal_val = 0 # Starting from top right,assigning value to pixels clockwise - binary_value.append(get_neighbors_pixel(image, x - 1, y + 1, center)) - binary_value.append(get_neighbors_pixel(image, x, y + 1, center)) - binary_value.append(get_neighbors_pixel(image, x - 1, y, center)) - binary_value.append(get_neighbors_pixel(image, x + 1, y + 1, center)) - binary_value.append(get_neighbors_pixel(image, x + 1, y, center)) - binary_value.append(get_neighbors_pixel(image, x + 1, y - 1, center)) - binary_value.append(get_neighbors_pixel(image, x, y - 1, center)) - binary_value.append(get_neighbors_pixel(image, x - 1, y - 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center)) + binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center)) # Converting the binary value to decimal. for i in range(len(binary_value)): From d0343feca7acdb4ea512d1e43b0f974958ea6220 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sat, 6 Aug 2022 23:21:43 +0800 Subject: [PATCH 08/24] Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss --- digital_image_processing/filters/local_binary_pattern.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index fe40bdd21991..31e393cc6d32 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -54,10 +54,7 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center)) # Converting the binary value to decimal. - for i in range(len(binary_value)): - decimal_val += binary_value[i] * powers[i] - - return decimal_val + return sum(binary_value * power for binary_value, power in zip(binary_values, powers)) if __name__ == "main": From df151ba9dab6402a310c35d8640097e612ab5cd7 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sat, 6 Aug 2022 23:21:51 +0800 Subject: [PATCH 09/24] Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss --- digital_image_processing/filters/local_binary_pattern.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 31e393cc6d32..31ad3212aa49 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -38,7 +38,7 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) :return: The decimal value of the binary value of the pixels around the center pixel. """ - binary_value = [] + binary_values = [] center = image[x_coordinate][y_coordinate] powers = [1, 2, 4, 8, 16, 32, 64, 128] decimal_val = 0 From 56183501527c226df5299fa560b3ee6a78875739 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sat, 6 Aug 2022 23:24:36 +0800 Subject: [PATCH 10/24] Update digital_image_processing/filters/local_binary_pattern.py Co-authored-by: Christian Clauss --- .../filters/local_binary_pattern.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 31ad3212aa49..593c382dc501 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -14,16 +14,7 @@ def get_neighbors_pixel(image: np.ndarray, x_coordinate: int, y_coordinate: int, :param center: center pixel value :return: The value of the pixel is being returned. """ - - value = 0 - - try: - if image[x_coordinate][y_coordinate] >= center: - value = 1 - except Exception: - pass - - return value + return int(image[x_coordinate][y_coordinate] or (center - 1) >= center) def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int: From be25fd6b9cb4b8cf3bb8602fcfafd17d94b3349d Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sat, 6 Aug 2022 23:31:50 +0800 Subject: [PATCH 11/24] Update local_binary_pattern.py --- .../filters/local_binary_pattern.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 593c382dc501..f54732023e10 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -35,14 +35,14 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) decimal_val = 0 # Starting from top right,assigning value to pixels clockwise - binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center)) - binary_value.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center)) + binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center)) # Converting the binary value to decimal. return sum(binary_value * power for binary_value, power in zip(binary_values, powers)) From d281c65188f6b6d8d6ace1878574ae0b13cdc478 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sat, 6 Aug 2022 23:42:45 +0800 Subject: [PATCH 12/24] undo changes made on get_neighbors_pixel() --- .../filters/local_binary_pattern.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index f54732023e10..eec4fc710761 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -14,7 +14,15 @@ def get_neighbors_pixel(image: np.ndarray, x_coordinate: int, y_coordinate: int, :param center: center pixel value :return: The value of the pixel is being returned. """ - return int(image[x_coordinate][y_coordinate] or (center - 1) >= center) + value = 0 + + try: + if image[x_coordinate][y_coordinate] >= center: + value = 1 + except Exception: + pass + + return value def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int: From 18a50ddd45c00bf1a84e3e9ee221580cbaf02934 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Sat, 6 Aug 2022 23:56:33 +0800 Subject: [PATCH 13/24] files formatted by black --- .../filters/local_binary_pattern.py | 41 ++++++++++++++----- .../test_digital_image_processing.py | 2 +- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index eec4fc710761..0b9b5cecf8cb 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -2,7 +2,9 @@ import numpy as np -def get_neighbors_pixel(image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int) -> int: +def get_neighbors_pixel( + image: np.ndarray, x_coordinate: int, y_coordinate: int, center: int +) -> int: """ Comparing local neighborhood pixel value with threshold value of centre pixel. Exception is required when neighborhood value of a center pixel value is null. @@ -40,20 +42,37 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) binary_values = [] center = image[x_coordinate][y_coordinate] powers = [1, 2, 4, 8, 16, 32, 64, 128] - decimal_val = 0 # Starting from top right,assigning value to pixels clockwise - binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center)) - binary_values.append(get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center)) + binary_values.append( + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center) + ) + binary_values.append( + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center) + ) # Converting the binary value to decimal. - return sum(binary_value * power for binary_value, power in zip(binary_values, powers)) + return sum( + binary_value * power for binary_value, power in zip(binary_values, powers) + ) if __name__ == "main": diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index f3c1befd00ec..c64f6f6bd263 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -105,7 +105,7 @@ def test_local_binary_pattern(): # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) - # Iterating through the image and calculating the local binary pattern value + # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0, image.shape[0]): for j in range(0, image.shape[1]): From 9afa4166f8948fe7088466d313168a0252e98369 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sun, 7 Aug 2022 00:02:11 +0800 Subject: [PATCH 14/24] Update digital_image_processing/filters/local_binary_pattern.py ok thanks Co-authored-by: Christian Clauss --- .../filters/local_binary_pattern.py | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 0b9b5cecf8cb..1f8e85eed67f 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -39,35 +39,20 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) :return: The decimal value of the binary value of the pixels around the center pixel. """ - binary_values = [] center = image[x_coordinate][y_coordinate] powers = [1, 2, 4, 8, 16, 32, 64, 128] - # Starting from top right,assigning value to pixels clockwise - binary_values.append( - get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center) - ) - binary_values.append( - get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center) - ) + # Starting from the top right, assigning value to pixels clockwise + binary_values = [ + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center), + get_neighbors_pixel(image, x_coordinate, y_coordinate + 1, center), + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate, center), + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate + 1, center), + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate, center), + get_neighbors_pixel(image, x_coordinate + 1, y_coordinate - 1, center), + get_neighbors_pixel(image, x_coordinate, y_coordinate - 1, center), + get_neighbors_pixel(image, x_coordinate - 1, y_coordinate - 1, center), + ] # Converting the binary value to decimal. return sum( From 2744b8315bf3c5834efb9c0a8f06e4d322df9c1f Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Sun, 7 Aug 2022 01:01:53 +0800 Subject: [PATCH 15/24] add test for get_neighbors_pixel() function --- .../test_digital_image_processing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index c64f6f6bd263..9bff963fbe9a 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -102,6 +102,18 @@ def test_local_binary_pattern(): # Reading the image and converting it to grayscale. image = imread(file_path, 0) + # Test for get_neighbors_pixel function() return not None + x_coordinate = 0 + y_coordinate = 0 + center = image[x_coordinate][y_coordinate] + + neighbors_pixels = lbp.get_neighbors_pixel( + image, x_coordinate, y_coordinate, center + ) + + assert neighbors_pixels is not None + + # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) From 5eec711aa6d8c45aecdd23dfb6f5f014ed8ae961 Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Sun, 7 Aug 2022 01:33:17 +0800 Subject: [PATCH 16/24] reviewed --- digital_image_processing/filters/local_binary_pattern.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 1f8e85eed67f..292076cd5e95 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -42,6 +42,10 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) center = image[x_coordinate][y_coordinate] powers = [1, 2, 4, 8, 16, 32, 64, 128] + # skip get_neighbors_pixel if center is null + if center is None: + return 0 + # Starting from the top right, assigning value to pixels clockwise binary_values = [ get_neighbors_pixel(image, x_coordinate - 1, y_coordinate + 1, center), @@ -63,9 +67,7 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) if __name__ == "main": # Reading the image and converting it to grayscale. - image = cv2.imread( - "digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE - ) + image = cv2.imread("digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) From 8611328241cf11a5f5f9c0adbf72ed03b1293bec Mon Sep 17 00:00:00 2001 From: zhexuanl Date: Sun, 7 Aug 2022 11:07:50 +0800 Subject: [PATCH 17/24] fix get_neighbors_pixel --- .../filters/local_binary_pattern.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 292076cd5e95..e73aa59bfa53 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -16,15 +16,11 @@ def get_neighbors_pixel( :param center: center pixel value :return: The value of the pixel is being returned. """ - value = 0 try: - if image[x_coordinate][y_coordinate] >= center: - value = 1 - except Exception: - pass - - return value + return int(image[x_coordinate][y_coordinate] >= center) + except (IndexError, TypeError): + return 0 def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) -> int: @@ -67,7 +63,9 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) if __name__ == "main": # Reading the image and converting it to grayscale. - image = cv2.imread("digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE) + image = cv2.imread( + "digital_image_processing/image_data/lena.jpg", cv2.IMREAD_GRAYSCALE + ) # Create a numpy array as the same height and width of read image lbp_image = np.zeros((image.shape[0], image.shape[1])) @@ -78,6 +76,6 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) for j in range(0, image.shape[1]): lbp_image[i][j] = local_binary_value(image, i, j) - cv2.imshow("Local Binary Pattern", lbp_image) + cv2.imshow("local binary pattern", lbp_image) cv2.waitKey(0) cv2.destroyAllWindows() From 484c508e9de618dac99eff7549572cf6657abd42 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Sun, 7 Aug 2022 11:44:06 +0800 Subject: [PATCH 18/24] Update test_digital_image_processing.py --- digital_image_processing/test_digital_image_processing.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index 9bff963fbe9a..1f42fddf297a 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -13,11 +13,10 @@ from digital_image_processing.edge_detection import canny as canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg +from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs -from digital_image_processing.filters import local_binary_pattern as lbp - img = imread(r"digital_image_processing/image_data/lena_small.jpg") gray = cvtColor(img, COLOR_BGR2GRAY) From 757629729ae6267b1f09e52b952a008b9c47d1d0 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 24 Aug 2022 04:33:26 +0000 Subject: [PATCH 19/24] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e7c5cf660bf7..a7305395a67b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -152,6 +152,7 @@ * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) * [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py) * [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py) + * [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py) * [Merge Two Binary Trees](data_structures/binary_tree/merge_two_binary_trees.py) * [Non Recursive Segment Tree](data_structures/binary_tree/non_recursive_segment_tree.py) * [Number Of Possible Binary Trees](data_structures/binary_tree/number_of_possible_binary_trees.py) From 36e2d0b3ebe279bd208e8f227ea92df47d32dc6e Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:35:21 +0800 Subject: [PATCH 20/24] Create code_quality.yml --- code_quality.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 code_quality.yml diff --git a/code_quality.yml b/code_quality.yml new file mode 100644 index 000000000000..f89d2eeb72f5 --- /dev/null +++ b/code_quality.yml @@ -0,0 +1,18 @@ +name: Qodana +on: + workflow_dispatch: + pull_request: + push: + branches: + - main + - 'releases/*' + +jobs: + qodana: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: 'Qodana Scan' + uses: JetBrains/qodana-action@v2022.2.1 From 18d8ed02aadd39a072354af65847c185590cdc16 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:37:18 +0800 Subject: [PATCH 21/24] Create code_quality.yml --- .github/workflows/code_quality.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/code_quality.yml diff --git a/.github/workflows/code_quality.yml b/.github/workflows/code_quality.yml new file mode 100644 index 000000000000..1ddca505774a --- /dev/null +++ b/.github/workflows/code_quality.yml @@ -0,0 +1,23 @@ +name: Qodana +on: + workflow_dispatch: + pull_request: + push: + branches: + - main + - 'releases/*' + +jobs: + qodana: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: 'Qodana Scan' + uses: JetBrains/qodana-action@v2022.2.1 + + + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json From b79183785c2c612b82b2d14996537d77efa034fe Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:37:37 +0800 Subject: [PATCH 22/24] Delete code_quality.yml --- code_quality.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 code_quality.yml diff --git a/code_quality.yml b/code_quality.yml deleted file mode 100644 index f89d2eeb72f5..000000000000 --- a/code_quality.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Qodana -on: - workflow_dispatch: - pull_request: - push: - branches: - - main - - 'releases/*' - -jobs: - qodana: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: 'Qodana Scan' - uses: JetBrains/qodana-action@v2022.2.1 From 2bdac453a13202e3d793791540b246c56a33d134 Mon Sep 17 00:00:00 2001 From: zhexuanl <63616187+zhexuanl@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:39:03 +0800 Subject: [PATCH 23/24] Update code_quality.yml --- .github/workflows/code_quality.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/code_quality.yml b/.github/workflows/code_quality.yml index 1ddca505774a..ddf5b7bd1753 100644 --- a/.github/workflows/code_quality.yml +++ b/.github/workflows/code_quality.yml @@ -15,9 +15,7 @@ jobs: with: fetch-depth: 0 - name: 'Qodana Scan' - uses: JetBrains/qodana-action@v2022.2.1 - - - uses: github/codeql-action/upload-sarif@v2 + uses: JetBrains/qodana-action@v2022.2.1 + - uses: github/codeql-action/upload-sarif@v2 with: sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json From f587a058d265a1753db9b141238a3be20d2a9d73 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 24 Aug 2022 06:45:18 +0200 Subject: [PATCH 24/24] Delete code_quality.yml --- .github/workflows/code_quality.yml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 .github/workflows/code_quality.yml diff --git a/.github/workflows/code_quality.yml b/.github/workflows/code_quality.yml deleted file mode 100644 index ddf5b7bd1753..000000000000 --- a/.github/workflows/code_quality.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Qodana -on: - workflow_dispatch: - pull_request: - push: - branches: - - main - - 'releases/*' - -jobs: - qodana: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: 'Qodana Scan' - uses: JetBrains/qodana-action@v2022.2.1 - - uses: github/codeql-action/upload-sarif@v2 - with: - sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json