-
-
Notifications
You must be signed in to change notification settings - Fork 46.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2be5ad
commit 57ab4c2
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
Check failure on line 7 in computer_vision/intensity-based_segmentation.py GitHub Actions / ruffRuff (I001)
|
||
""" | ||
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() |