📘
Simple Binary Thresholding
Implement a binary thresholding technique to segment a 2D grayscale image based on a given threshold T. This process involves categorizing each pixel into one of two classes, creating a binary image. The concept of image segmentation is crucial in computer vision, as it enables the separation of objects or regions of interest from the rest of the image, which is vital for various applications. Here's how to approach this:
- Iterate over each pixel in the image,
- Compare its intensity value to the threshold T,
- Assign a value of 1 if the pixel's intensity is greater than T, and 0 otherwise.
This technique is widely used in medical imaging applications.
Example:
Input:
image = [[100, 150, 200], [50, 120, 180]] T = 120
Output:
[[0, 1, 1], [0, 0, 1]]
Reasoning:
- The given image is a 2D grayscale image with pixel values:
[[100, 150, 200], [50, 120, 180]]. - We apply the threshold T=120 to each pixel value, comparing it to T to determine the binary output:
- Pixel values greater than T (>120) map to 1
- Pixel values less than or equal to T (≤120) map to 0
- The thresholding process yields the following binary image:
- First row: 100≤120 maps to 0, 150>120 maps to 1, 200>120 maps to 1, resulting in
[0, 1, 1] - Second row: 50≤120 maps to 0, 120≤120 maps to 0, 180>120 maps to 1, resulting in
[0, 0, 1]
- First row: 100≤120 maps to 0, 150>120 maps to 1, 200>120 maps to 1, resulting in
- The final output is
[[0, 1, 1], [0, 0, 1]]
Constraints:
- image is a 2D list of non-negative numbers
- T is a numeric threshold
- Return a 2D binary list (0 or 1)
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.