Nodes/opencv-comfyui/OpenCV textureFlattening_0
ComfyUI Node

OpenCV textureFlattening_0

Make your seamless-clone composites stop looking pasted

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV textureFlattening_0
  • src
  • mask
  • dst
  • nparray
low_threshold
high_threshold
kernel_size

This is the one genuinely useful node in the opencv-comfyui pack that most people will never find, because it comes from OpenCV's photo module and has no flashy Comfy equivalent. cv2.textureFlattening exists for a very specific job: when you blend one image into another with seamless cloning, the color match across the boundary is only as good as the texture on either side. Flat skin over a textured background looks wrong. Flatten the texture where you don't care about it, and the clone suddenly reads as "photographed," not "Photoshopped."

It's a deterministic pixel operation - nothing here runs a diffusion model. That's the whole point of reaching into this pack: a $0, millisecond OpenCV primitive instead of burning a generation pass to fix something a filter does in a frame (the post-processing KB makes exactly this argument about the whole layer). If you're doing face compositing, product shots on textured backdrops, or any inpainting-then-stitching flow, this is a tool worth knowing.

How it works

You give it an image and a mask. Inside the mask, texture is preserved; outside the mask, textureFlattening smooths the texture away while leaving color and larger structure alone - it's a texture-aware bilateral-ish smoothing that collapses fine detail into flat color. The result is a version of the image where non-masked regions are clean enough to act as a good canvas for a Poisson-style clone. The three tuning knobs are:

  • low_threshold / high_threshold (FLOAT) - edge-detection thresholds that decide what counts as "texture" versus "edge to keep."
  • kernel_size (INT) - the smoothing kernel size; bigger means flatter, and slower.

Start with the OpenCV defaults in mind (low around 30, high around 45, kernel 3) and nudge from there. The optional dst is an out-parameter - leave it unwired.

The single output is nparray, which wires straight into another OpenCV node or into Nparrays2Image to get back a Comfy IMAGE.

Installation

Get the pack via ComfyUI Manager (search "OpenCV" or geroldmeisinger/opencv-comfyui), or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Restart ComfyUI. No models to download. The catch: this node needs the contrib build of OpenCV, because textureFlattening lives in cv2.photo, which only ships with opencv-contrib-python - which is exactly what the pack's requirements.txt installs. If you installed a bare OpenCV for another node, pip install opencv-contrib-python will fix the missing module.

Common issues

  • error: (-215:Assertion failed) img.type() == CV_8UC1 - the function wants grayscale-ish inputs in some configurations. Convert with cvtColor (code=6) first, per the README.
  • invalid syntax (<unknown>, line 0) - composite parameters are entered as Python literals and parsed with ast.literal_eval; if you typed a literal wrong, this is the error you get.
  • The pack's nodes only take NPARRAY at batch size 1. Feed a batched image straight in and Image2Nparray throws Only images with batch_size==1 are supported - run it through an ImageFromBatch (length=1) first.
  • If you hit Cannot import name 'guidedFilter' from 'cv2.ximgproc' at startup, you have two conflicting OpenCV installs - it's the pack's known dependency trap, and the README links the fix.
Categoryimage/OpenCV

Inputs (6)

NameTypeDefaultDescription
srcNPARRAY
maskNPARRAY
low_thresholdFLOAT
high_thresholdFLOAT
kernel_sizeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY