From 57ab4c273d009ef2548ee2a3b9f3ab9c84711cbe Mon Sep 17 00:00:00 2001 From: SUDO_USER <110802232+AtharvMalusare@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:44:07 +0530 Subject: [PATCH 1/8] Add files via upload --- .../intensity-based_segmentation.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 computer_vision/intensity-based_segmentation.py diff --git a/computer_vision/intensity-based_segmentation.py b/computer_vision/intensity-based_segmentation.py new file mode 100644 index 000000000000..f32bba6d17aa --- /dev/null +++ b/computer_vision/intensity-based_segmentation.py @@ -0,0 +1,62 @@ +# Importing necessary libraries +import numpy as np +from PIL import Image +import matplotlib.pyplot as plt +from typing import List + +def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: + """ + Performs image segmentation based on intensity thresholds. + Args: + image (np.ndarray): Input grayscale image as a 2D array. + thresholds (List[int]): A list of intensity thresholds to define segments. + Returns: + np.ndarray: A labeled 2D array where each region corresponds to a threshold range. + Example: + >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) + >>> segment_image(img, [50, 100, 150]) + array([[1, 2, 3], + [0, 1, 2], + [0, 0, 1]]) + """ + # Initialize an empty array to store segment labels + segmented = np.zeros_like(image, dtype=np.int32) + + # Iterate over thresholds and label pixels in corresponding intensity ranges + for i, threshold in enumerate(thresholds): + segmented[image > threshold] = i + 1 + + return segmented + +if __name__ == "__main__": + # Path to the image file + image_path = "path_to_image" # Replace with the path to your local image file + + # Load and preprocess the image + original_image = Image.open(image_path).convert("L") # Convert image to grayscale + image_array = np.array(original_image) # Convert image to a numpy array + + # Specify intensity thresholds for segmentation + thresholds = [50, 100, 150, 200] # Define your desired thresholds + + # Apply segmentation to the image + segmented_image = segment_image(image_array, thresholds) + + # Visualize the results + plt.figure(figsize=(12, 6)) + + # Display the original image + plt.subplot(1, 2, 1) + plt.title("Original Grayscale Image") + plt.imshow(image_array, cmap="gray") + plt.axis("off") + + # Display the segmented image with labeled regions + plt.subplot(1, 2, 2) + plt.title("Segmented Image") + plt.imshow(segmented_image, cmap="tab20") # Use a colormap for better distinction + plt.axis("off") + + # Show the plots + plt.tight_layout() + plt.show() From fd914c8a158da37ffd3f5b2a7aa1e83024d4d38e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:17:08 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/intensity-based_segmentation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/computer_vision/intensity-based_segmentation.py b/computer_vision/intensity-based_segmentation.py index f32bba6d17aa..72ca744a7129 100644 --- a/computer_vision/intensity-based_segmentation.py +++ b/computer_vision/intensity-based_segmentation.py @@ -4,6 +4,7 @@ import matplotlib.pyplot as plt from typing import List + def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: """ Performs image segmentation based on intensity thresholds. @@ -28,6 +29,7 @@ def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: return segmented + if __name__ == "__main__": # Path to the image file image_path = "path_to_image" # Replace with the path to your local image file From 9d4ca702ad5f83a3ad5ad4e2c30de7a427dbd738 Mon Sep 17 00:00:00 2001 From: SUDO_USER <110802232+AtharvMalusare@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:54:43 +0530 Subject: [PATCH 3/8] Update intensity-based_segmentation.py --- computer_vision/intensity-based_segmentation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/computer_vision/intensity-based_segmentation.py b/computer_vision/intensity-based_segmentation.py index 72ca744a7129..4bc3ba09b956 100644 --- a/computer_vision/intensity-based_segmentation.py +++ b/computer_vision/intensity-based_segmentation.py @@ -1,10 +1,10 @@ +# Source:"https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" # Importing necessary libraries import numpy as np from PIL import Image import matplotlib.pyplot as plt from typing import List - def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: """ Performs image segmentation based on intensity thresholds. @@ -29,7 +29,6 @@ def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: return segmented - if __name__ == "__main__": # Path to the image file image_path = "path_to_image" # Replace with the path to your local image file From 2030b33ac53f8f566c340d48895015bd341d4abd Mon Sep 17 00:00:00 2001 From: SUDO_USER <110802232+AtharvMalusare@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:58:27 +0530 Subject: [PATCH 4/8] Update and rename intensity-based_segmentation.py to intensity_based_segmentation.py --- .../intensity-based_segmentation.py | 63 ------------------- .../intensity_based_segmentation.py | 57 +++++++++++++++++ 2 files changed, 57 insertions(+), 63 deletions(-) delete mode 100644 computer_vision/intensity-based_segmentation.py create mode 100644 computer_vision/intensity_based_segmentation.py diff --git a/computer_vision/intensity-based_segmentation.py b/computer_vision/intensity-based_segmentation.py deleted file mode 100644 index 4bc3ba09b956..000000000000 --- a/computer_vision/intensity-based_segmentation.py +++ /dev/null @@ -1,63 +0,0 @@ -# Source:"https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" -# Importing necessary libraries -import numpy as np -from PIL import Image -import matplotlib.pyplot as plt -from typing import List - -def segment_image(image: np.ndarray, thresholds: List[int]) -> np.ndarray: - """ - Performs image segmentation based on intensity thresholds. - Args: - image (np.ndarray): Input grayscale image as a 2D array. - thresholds (List[int]): A list of intensity thresholds to define segments. - Returns: - np.ndarray: A labeled 2D array where each region corresponds to a threshold range. - Example: - >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) - >>> segment_image(img, [50, 100, 150]) - array([[1, 2, 3], - [0, 1, 2], - [0, 0, 1]]) - """ - # Initialize an empty array to store segment labels - segmented = np.zeros_like(image, dtype=np.int32) - - # Iterate over thresholds and label pixels in corresponding intensity ranges - for i, threshold in enumerate(thresholds): - segmented[image > threshold] = i + 1 - - return segmented - -if __name__ == "__main__": - # Path to the image file - image_path = "path_to_image" # Replace with the path to your local image file - - # Load and preprocess the image - original_image = Image.open(image_path).convert("L") # Convert image to grayscale - image_array = np.array(original_image) # Convert image to a numpy array - - # Specify intensity thresholds for segmentation - thresholds = [50, 100, 150, 200] # Define your desired thresholds - - # Apply segmentation to the image - segmented_image = segment_image(image_array, thresholds) - - # Visualize the results - plt.figure(figsize=(12, 6)) - - # Display the original image - plt.subplot(1, 2, 1) - plt.title("Original Grayscale Image") - plt.imshow(image_array, cmap="gray") - plt.axis("off") - - # Display the segmented image with labeled regions - plt.subplot(1, 2, 2) - plt.title("Segmented Image") - plt.imshow(segmented_image, cmap="tab20") # Use a colormap for better distinction - plt.axis("off") - - # Show the plots - plt.tight_layout() - plt.show() diff --git a/computer_vision/intensity_based_segmentation.py b/computer_vision/intensity_based_segmentation.py new file mode 100644 index 000000000000..dffcc8d13753 --- /dev/null +++ b/computer_vision/intensity_based_segmentation.py @@ -0,0 +1,57 @@ +# Source:"https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" +# Importing necessary libraries +import numpy as np +from PIL import Image +import matplotlib.pyplot as plt + +def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: + """ + Performs image segmentation based on intensity thresholds. + + Args: + image (np.ndarray): Input grayscale image as a 2D array. + thresholds (list[int]): Intensity thresholds to define segments. + + Returns: + np.ndarray: A labeled 2D array where each region corresponds to a threshold range. + + Example: + >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) + >>> segment_image(img, [50, 100, 150]) + array([[1, 2, 3], [0, 1, 2], [0, 1, 1]]) + """ + # Initialize segmented array with zeros + segmented = np.zeros_like(image, dtype=np.int32) + + # Assign labels based on thresholds + for i, threshold in enumerate(thresholds): + segmented[image > threshold] = i + 1 + + return segmented + +if __name__ == "__main__": + # Load the image + image_path = "path_to_image" # Replace with your image path + original_image = Image.open(image_path).convert("L") + image_array = np.array(original_image) + + # Define thresholds + thresholds = [50, 100, 150, 200] + + # Perform segmentation + segmented_image = segment_image(image_array, thresholds) + + # Display the results + plt.figure(figsize=(10, 5)) + + plt.subplot(1, 2, 1) + plt.title("Original Image") + plt.imshow(image_array, cmap="gray") + plt.axis("off") + + plt.subplot(1, 2, 2) + plt.title("Segmented Image") + plt.imshow(segmented_image, cmap="tab20") + plt.axis("off") + + plt.show() From 38dc81df0bb1a587d447ed5fefa99b65f0ff6cea Mon Sep 17 00:00:00 2001 From: SUDO_USER <110802232+AtharvMalusare@users.noreply.github.com> Date: Mon, 30 Dec 2024 20:01:34 +0530 Subject: [PATCH 5/8] Update intensity_based_segmentation.py --- computer_vision/intensity_based_segmentation.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/computer_vision/intensity_based_segmentation.py b/computer_vision/intensity_based_segmentation.py index dffcc8d13753..1552311b2306 100644 --- a/computer_vision/intensity_based_segmentation.py +++ b/computer_vision/intensity_based_segmentation.py @@ -1,4 +1,5 @@ -# Source:"https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" +# Source: "https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" + # Importing necessary libraries import numpy as np from PIL import Image @@ -18,7 +19,9 @@ def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: Example: >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) >>> segment_image(img, [50, 100, 150]) - array([[1, 2, 3], [0, 1, 2], [0, 1, 1]]) + array([[1, 2, 3], + [0, 1, 2], + [0, 1, 1]]) """ # Initialize segmented array with zeros segmented = np.zeros_like(image, dtype=np.int32) From ba883986e25c3b4bd0540fe5075dc11b784dca94 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:40:18 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/intensity_based_segmentation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/computer_vision/intensity_based_segmentation.py b/computer_vision/intensity_based_segmentation.py index 1552311b2306..4179f74c6670 100644 --- a/computer_vision/intensity_based_segmentation.py +++ b/computer_vision/intensity_based_segmentation.py @@ -5,6 +5,7 @@ from PIL import Image import matplotlib.pyplot as plt + def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: """ Performs image segmentation based on intensity thresholds. @@ -19,8 +20,8 @@ def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: Example: >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) >>> segment_image(img, [50, 100, 150]) - array([[1, 2, 3], - [0, 1, 2], + array([[1, 2, 3], + [0, 1, 2], [0, 1, 1]]) """ # Initialize segmented array with zeros @@ -32,6 +33,7 @@ def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: return segmented + if __name__ == "__main__": # Load the image image_path = "path_to_image" # Replace with your image path From aabf3ed680a3da0ea62f18c369e6843a9ecdd0b8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 30 Dec 2024 16:04:49 +0100 Subject: [PATCH 7/8] Apply suggestions from code review --- computer_vision/intensity_based_segmentation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/computer_vision/intensity_based_segmentation.py b/computer_vision/intensity_based_segmentation.py index 4179f74c6670..cc6f01223e9d 100644 --- a/computer_vision/intensity_based_segmentation.py +++ b/computer_vision/intensity_based_segmentation.py @@ -1,9 +1,9 @@ # Source: "https://www.ijcse.com/docs/IJCSE11-02-03-117.pdf" # Importing necessary libraries +import matplotlib.pyplot as plt import numpy as np from PIL import Image -import matplotlib.pyplot as plt def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: @@ -11,11 +11,11 @@ def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: Performs image segmentation based on intensity thresholds. Args: - image (np.ndarray): Input grayscale image as a 2D array. - thresholds (list[int]): Intensity thresholds to define segments. + image: Input grayscale image as a 2D array. + thresholds: Intensity thresholds to define segments. Returns: - np.ndarray: A labeled 2D array where each region corresponds to a threshold range. + A labeled 2D array where each region corresponds to a threshold range. Example: >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]]) From 91f2373aaf0f0587241ea98337a77fe797dba861 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 30 Dec 2024 16:09:07 +0100 Subject: [PATCH 8/8] [0, 1, 1]], dtype=int32) --- computer_vision/intensity_based_segmentation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/intensity_based_segmentation.py b/computer_vision/intensity_based_segmentation.py index cc6f01223e9d..7f2b1141acc4 100644 --- a/computer_vision/intensity_based_segmentation.py +++ b/computer_vision/intensity_based_segmentation.py @@ -22,7 +22,7 @@ def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray: >>> segment_image(img, [50, 100, 150]) array([[1, 2, 3], [0, 1, 2], - [0, 1, 1]]) + [0, 1, 1]], dtype=int32) """ # Initialize segmented array with zeros segmented = np.zeros_like(image, dtype=np.int32)