Skip to content

Intensity_based_Segmentation #12490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions computer_vision/intensity_based_segmentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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:

Check failure on line 8 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/intensity_based_segmentation.py:4:1: I001 Import block is un-sorted or un-formatted

Check failure on line 8 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/intensity_based_segmentation.py:4:1: I001 Import block is un-sorted or un-formatted

Check failure on line 8 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/intensity_based_segmentation.py:4:1: I001 Import block is un-sorted or un-formatted
"""
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.

Check failure on line 17 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

computer_vision/intensity_based_segmentation.py:17:89: E501 Line too long (90 > 88)

Check failure on line 17 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

computer_vision/intensity_based_segmentation.py:17:89: E501 Line too long (90 > 88)

Check failure on line 17 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

computer_vision/intensity_based_segmentation.py:17:89: E501 Line too long (90 > 88)

Example:
>>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]])
>>> segment_image(img, [50, 100, 150])
array([[1, 2, 3],

Check failure on line 22 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:22:26: W291 Trailing whitespace

Check failure on line 22 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:22:26: W291 Trailing whitespace

Check failure on line 22 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:22:26: W291 Trailing whitespace
[0, 1, 2],

Check failure on line 23 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:23:26: W291 Trailing whitespace

Check failure on line 23 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:23:26: W291 Trailing whitespace

Check failure on line 23 in computer_vision/intensity_based_segmentation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

computer_vision/intensity_based_segmentation.py:23:26: W291 Trailing whitespace
[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()
Loading