OpenCV patchNaNs_0
Patch broken pixels before they poison your tensor
- a
- nparray
NaN is the silent killer of the OpenCV-in-ComfyUI pipeline. Do a division by zero, feed log(0), run a transform over an empty region, and suddenly some of your pixels are NaN - not black, not white, undefined. If you let that through to Nparrays2Image, it gets divided by 255, cast to float32, and baked into your tensor as NaN, where it can quietly wreck a VAE decode or a ControlNet preprocessor with no obvious error message. patchNaNs_0 is the guard rail: it replaces every NaN in an array with a value you choose.
How it works
It's a one-to-one wrapper around OpenCV's cv2.patchNaNs(a, val), which literally walks the array and converts every NaN element to val. Nothing fancy, nothing AI - a few microseconds of housekeeping that keeps the rest of your graph honest. The wrapper passes your a straight through, and the returned nparray is the same array with NaN cells overwritten.
The inputs that matter
a(NPARRAY) - the array to clean. Usually the output of another OpenCV node that can produce NaNs: optical flow, gradient math, warps,phase, anything where you divided two arrays.val(FLOAT) - the replacement value.0.0is the OpenCV default and is the sane choice for pixel data; use0.5if you want a mid-gray fill instead. It's required here (the generated wrapper makes it mandatory, even though OpenCV would default it), so you always set it explicitly.
The single output is nparray - same shape, same type, just NaN-free. Wire it to Nparrays2Image or keep chaining OpenCV ops on it.
How to install it
This is one of ~600 auto-generated wrappers in the opencv-comfyui pack - install the pack once:
- ComfyUI Manager → search opencv-comfyui → Install, restart ComfyUI.
- Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
Needs opencv-contrib-python (the README's pip install opencv-python-contrib - same package, different name). You likely already have it. No models, no downloads.
Common issues & troubleshooting
- Batch gotcha: the pack only handles
batch_size==1. IfImage2NparraythrowsOnly images with batch_size==1 are supported!, grab one frame withImageFromBatch(length=1) before converting. - It only fixes NaN, not infinity.
cv2.patchNaNsignoresinf/-infby design. If your math is overflowing, patch the source of the overflow first - OpenCV'scheckRangevariant in this pack can help you find it. - Does nothing visible when there are no NaNs. That's correct behavior. It's a cheap no-op that keeps a poison pixel from ever reaching your tensor.
If you're staring at a black splotch or a decode that "just looks wrong" and your OpenCV chain includes a division or a logarithm somewhere, this node is usually the fix. It's small, boring, and genuinely the one I reach for.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| a | NPARRAY | — | |
| val | FLOAT | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |