Nodes/ComfyUI CV/cv2.bitwise_or
ComfyUI Node

cv2.bitwise_or

Union two masks and stop hand-drawing them twice

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.bitwise_or
  • src1
  • src2
  • mask
  • result

A mask is a region. Two masks are two regions. bitwise_or is the moment they become one region, and that's a move you make constantly: combine the blob the detector found with the patch you drew by hand, merge the two inpaint areas into one job, union the sub-masks a segmentation split apart.

On images it's bit surgery again, same as the rest of the family. On masks it's set union, which is exactly what you want and exactly what it says.

How it works

Per-element bitwise OR across src1 and src2, with an optional mask restricting which output elements get written. Same size and type required; output echoes src1's format, so a MASK in gives a MASK back out and this node disappears neatly into a mask chain.

src2 also accepts a scalar - the author's tooltip calls it "second input array or a scalar" - so a constant from CV Scalar can be ORed in when you want to force something, though that's rarely the intent.

And the honest caveat, same as bitwise_and: OR on soft masks is not max. 200 OR 100 = 236. Union semantics only hold for binary masks (0/255). If your masks are feathered gradients and you want "whichever is stronger", you want cv2_max, which is also wrapped in this pack.

Inputs and outputs

  • src1 (COMFY_MATCHTYPE_V3) - first array; decides the echo.
  • src2 (NPARRAY,IMAGE,MASK) - second array, same size and type.
  • mask (optional) - restrict which elements are written.
  • result - echoes src1.

What it's for

  • Combining detections. Two blobs that should be one region - a hand plus the object it's holding, a face plus the hair - unioned into a single mask before a crop or an inpaint.
  • Merging hand-drawn and automatic regions. The pack's annotate-then-segment workflows are built on this: the automatic mask gets the bulk, you draw the correction, OR them.
  • Adding a shape to a mask. Draw a filled polygon onto the mask (the drawing wrappers accept a MASK canvas) and union it in with the original.
  • Padding a detection. Generate a bigger box mask, union it with the tight mask, and you've grown the region without dilating its shape. (cv2_dilate does the proper thing for that, but a box union is the crude version people actually use.)
  • Region arithmetic before a paste. Several regions into one mask, then one paste - instead of several pastes with the seams in between.

Install

ComfyUI Manager → comfyui_cv (ComfyUI CV), or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Restart ComfyUI. Python ≥ 3.12 and a recent V3-API ComfyUI; behaviour is curated against OpenCV 5.0.0.93. The contrib wheel is what the pack declares - install a non-contrib opencv-python over it and the contrib submodules empty out without warning.

Where people get burned

Size mismatch. Two masks from different resolutions can't be ORed. The pack's curated mask nodes work at the target resolution for this reason; if yours didn't, resize or re-draw to match before the union.

Bit-level union of soft masks. 200 OR 100 = 236, which brightens in a way that isn't "the stronger of the two". Use cv2_max for a proper soft union, or binarize first if binary is what you meant.

Type mismatch. A MASK (single-channel uint8) against an IMAGE (3-channel) raises. Union masks with masks, images with images.

Assuming OR is a blend. It's not - it writes one value where either is present. If you wanted a weighted mix, that's cv2_addWeighted or the pack's blend subgraphs.

Chaining five ORs when a merge node exists. For many contours or many mask batches the pack's curated CV Overlay Masks or a merge/subgraph is tidier than a chain of booleans - and a chain of ORs on a big mask batch is also a chain of full-frame operations, which shows up in your run time.

Categoryimage/CV/low-level/cv2 B

Inputs (3)

NameTypeDefaultDescription
src1COMFY_MATCHTYPE_V3first input array or a scalar. The image output(s) echo this input's format. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
src2NPARRAY,IMAGE,MASKsecond input array or a scalar. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
maskoptNPARRAY,IMAGE,MASKoptional operation mask, CV_8U, CV_8S or CV_Bool single channel array, that specifies elements of the output array to be changed. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.

Outputs (1)

NameTypeDescription
resultCOMFY_MATCHTYPE_V3Echoes the 'src1' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY.