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

  1. Create a workspace and add Image Input, cvtColor, threshold and Image Output. Connect them in the order shown above.
  2. Choose a local still image. Set cvtColor to rgbaToGray so that the threshold works on intensity rather than separate color channels.
  3. 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.
  4. Raise the threshold to keep fewer bright pixels. Lower it to include more of the scene. Keep the input image fixed while comparing settings.
  5. 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.

Start building free