-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Intensity_based_Segmentation #12491
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
Merged
Merged
Intensity_based_Segmentation #12491
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
57ab4c2
Add files via upload
AtharvMalusare fd914c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9d4ca70
Update intensity-based_segmentation.py
AtharvMalusare 2030b33
Update and rename intensity-based_segmentation.py to intensity_based_…
AtharvMalusare 38dc81d
Update intensity_based_segmentation.py
AtharvMalusare ba88398
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aabf3ed
Apply suggestions from code review
cclauss 91f2373
[0, 1, 1]], dtype=int32)
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
# 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. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.