Turn an image into a binary mask with OpenCV
Starter or above
Separate bright and dark regions using a grayscale conversion and a fixed binary threshold. This is a useful first step for experiments with silhouettes or high-contrast images.
Workflow
Image Input → cvtColor → threshold → Image Output
Build it in Visionodo
- Create a workspace and add Image Input, cvtColor, threshold and Image Output. Connect them in the order shown above.
- Choose a local still image. Set cvtColor to rgbaToGray so that the threshold works on intensity rather than separate color channels.
- In threshold, choose binary mode, a threshold of 127 and a maximum value of 255. Run the workflow. Pixels above the threshold become white; the remaining pixels become black.
- Raise the threshold to keep fewer bright pixels. Lower it to include more of the scene. Keep the input image fixed while comparing settings.
- Try binaryInv when the foreground is darker than its background. Inspect the preview, save your graph if useful, and use Save Image for an explicit download.
What to expect
A binary image is a mask candidate. A global threshold is useful with stable lighting and clear contrast; it does not identify a subject automatically. Uneven lighting may require adaptiveThreshold instead.
Limits and practical checks
A single threshold rarely works for every photograph. Inspect failure cases before using a mask downstream. Optional blur can reduce noise but can also remove small features.