Nodes/opencv-comfyui/OpenCV divide_0
ComfyUI Node

OpenCV divide_0

Flat-field correction and ratio masks

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV divide_0
  • src1
  • src2
  • dst
  • nparray
scale
dtype

divide_0 is per-pixel image division - cv2.divide(src1, src2, scale, dtype) - and it's one of those unglamorous operations that solves a real, specific problem people usually hand-wave instead. The headline use: flat-field / illumination correction. Take an image and divide it by a heavily blurred copy of itself, and you cancel out uneven lighting - shadows, vignettes, a bright hotspot - leaving behind the reflectance detail. It's the mathematical inverse of multiplying by a light field, and it's how you normalize an image that's lit badly without running a model over it.

The mechanism is dead simple: every output pixel is saturate(src1 × scale / src2), computed per channel, with scale defaulting to 1. The result is a ratio image - a per-pixel map of how src1 relates to src2. That's the same idea behind a bunch of "compare two things pixel-wise" tricks: building ratio masks from two different exposures, checking where one image is brighter than another, or deriving a normalized version of a mask. Because it's a pointwise op, it's instant and deterministic - squarely in the KB's post-processing layer, the kind of thing you should be doing with math instead of a diffusion pass.

The inputs that matter:

  • src1, src2 - NPARRAYs, same size (and ideally same type). These are the numerator and denominator.
  • scale - FLOAT, default 1. Multiply the result by this - handy if you're dividing to normalize and want to rescale the output range.
  • dtype - INT, output type; -1 means "same as the inputs," which is what you want almost always.

Optional dst is an out-parameter - skip it per the pack README. Output: one nparray, the ratio image.

Two gotchas grounded in how OpenCV actually behaves. First, division by zero: pixel values of 0 in src2 are a real hazard - OpenCV's behavior on 0/0 and x/0 is type-dependent and occasionally ugly (in float images you can get inf/NaN that poison everything downstream). If you're dividing by something that could contain zeros - a blurred copy is fine, a raw mask is not - threshold it or add a tiny epsilon before it reaches this node. Second, src1/src2 must be same-shaped. And note there's no image-divided-by-a-constant form in this pack - if what you want is a constant divided by your image (the scaled reciprocal, for per-pixel inversion), that's the divide_2/divide_3 overload: scale/src2.

Pack-wide plumbing, as always: NPARRAY in and out - Image2Nparray before, Nparrays2Image after, batch_size==1 only.

Install: ships in geroldmeisinger/opencv-comfyui - Manager → search "OpenCV", or git clone https://github.com/geroldmeisinger/opencv-comfyui into custom_nodes, restart, with opencv-contrib-python installed. The pack-wide Cannot import name 'guidedFilter' conflict error from duplicate OpenCV wheels blocks the whole pack. divide_1 is the identical overload twin.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
scaleFLOAT
dtypeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY