MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / segment_image

Function segment_image

computer_vision/intensity_based_segmentation.py:9–34  ·  view source on GitHub ↗

Performs image segmentation based on intensity thresholds. Args: image: Input grayscale image as a 2D array. thresholds: Intensity thresholds to define segments. Returns: A labeled 2D array where each region corresponds to a threshold range. Exam

(image: np.ndarray, thresholds: list[int])

Source from the content-addressed store, hash-verified

7
8
9def segment_image(image: np.ndarray, thresholds: list[int]) -> np.ndarray:
10 """
11 Performs image segmentation based on intensity thresholds.
12
13 Args:
14 image: Input grayscale image as a 2D array.
15 thresholds: Intensity thresholds to define segments.
16
17 Returns:
18 A labeled 2D array where each region corresponds to a threshold range.
19
20 Example:
21 >>> img = np.array([[80, 120, 180], [40, 90, 150], [20, 60, 100]])
22 >>> segment_image(img, [50, 100, 150])
23 array([[1, 2, 3],
24 [0, 1, 2],
25 [0, 1, 1]], dtype=int32)
26 """
27 # Initialize segmented array with zeros
28 segmented = np.zeros_like(image, dtype=np.int32)
29
30 # Assign labels based on thresholds
31 for i, threshold in enumerate(thresholds):
32 segmented[image > threshold] = i + 1
33
34 return segmented
35
36
37if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected