Skip to content

[Bug] GrayToBinary silently produces wrong output when given a color (BGR/BGRA) image #319

@Jayant-kernel

Description

@Jayant-kernel

Description

The GrayToBinary operator assumes its input is already a single-channel grayscale image, but performs no validation or conversion. When a user connects any BGR or BGRA-output block directly to GrayToBinary, OpenCV's cv2.threshold runs independently on each channel, producing a 3-channel output instead of a true binary grayscale image.

Steps to Reproduce

import cv2, numpy as np
from app.operators.conversions.gray_to_binary import GrayToBinary

# Simulates a BGR image coming from any color operator
img_bgr = np.array([[[50, 150, 200]]], dtype=np.uint8)
result = GrayToBinary({"thresholdValue": 100, "maxValue": 255}).compute(img_bgr)
print(result.shape)   # (1, 1, 3) — NOT a binary grayscale image
print(result)         # [[[  0 255 255]]] — each channel thresholded independently

Expected Behaviour

GrayToBinary should either:

  • Raise a ValueError with a clear message if the input is not single-channel (guiding the user to insert GrayImage first), or
  • Convert BGR/BGRA to grayscale before thresholding (like ColorToBinary does)

Actual Behaviour

Silently thresholds each colour channel independently, returning a 3-channel array. The block name implies a binary (single-channel) output but the output is a 3-channel image with per-channel thresholding applied — incorrect and misleading.

Context

  • ColorToBinary correctly converts to grayscale before thresholding.
  • GrayToBinary is intended for images already in grayscale, but the pipeline UI does not prevent connecting a colour-output block to it.
  • Related fix: [Bug] GrayToBinary default maxValue=0 causes all-black output #180 (fixed default maxValue), but the colour-input path was not addressed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions