Nodes/ComfyUI CV/cv2.ximgproc.transformFD
ComfyUI Node

cv2.ximgproc.transformFD

Rotate and rescale a silhouette using Fourier descriptors

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.ximgproc.transformFD
  • src
  • t
  • nparray
◄fdContourtrue►

transformFD is the second half of the Fourier-descriptor pair in this module, and it does the thing that makes descriptors worth the trouble: it applies a transform to the descriptor rather than to the pixels. Feed it a descriptor and a four-number transform, and you get the transformed shape back - as a descriptor, or as reconstructed contour points. No resampling, no interpolation loss, and no need for the original contour to still be lying around.

Two obvious uses. Aligning one silhouette to another when you already know the relative pose (shape matching, registration, building an average shape out of several samples), and reconstructing a different version of a shape - rotate a hand-drawn mask by 40°, scale it, move it - from a signature you computed once.

Inputs

  • src - the Fourier descriptor from cv2.ximgproc.fourierDescriptor. The author's tooltip names the source explicitly, and that's the only realistic producer: it's an array of coefficients, not a contour.
  • t - the transform to apply, as a 1×4 array. OpenCV's own docs describe it as the parameter vector the ContourFitting estimator produces (a similarity transform - rotation, scale, translation); a hand-authored array of four numbers is equally valid, since this socket takes any NPARRAY from the pack's literal or array nodes. Be warned that the wrapper's tooltip doesn't spell out the field order, so if your shape comes back rotated but with the wrong offset, that's the first thing to check rather than the last.
  • fdContour (true) - what you get back. true returns a Fourier descriptor (chain another transform); false returns contour points, which is what you want when you're finished and ready to draw or measure.

Output is a plain NPARRAY either way. With fdContour off you get (N, 1, 2) points - exactly what CV Points To Contour expects, which turns them into a CV_CONTOURS value for the pack's drawing and shape-measurement nodes.

The one rule that bites people

Descriptors from two different contours are only comparable if they were computed with the same resampling. fourierDescriptor's nbElt defaults to -1, meaning "keep whatever point count the contour arrived with" - and two contours extracted from two images almost never have the same count. Set nbElt explicitly (the contourSampling wrapper resamples contours to a fixed count for exactly this reason) before you compute descriptors you intend to compare or transform interchangeably.

Also decide up front how many coefficients you're keeping (nbFD). A truncated descriptor reconstructs a smoother contour - handy if the point of the exercise is to get a clean version of a ragged outline, mildly disastrous if careful geometry was the point.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv

Restart ComfyUI, or install ComfyUI CV from ComfyUI Manager. Python ≥3.12, a recent ComfyUI on the V3 node API, and opencv-contrib-python-headless~=5.0.0.93 - that single wheel is the whole dependency list, no models to download. Contrib-only: a non-contrib OpenCV install sharing site-packages/cv2 empties the contrib submodules and removes this node from the menu. tools/repair_opencv_contrib.py --check reports it; --apply fixes it.

Common issues

Overload errors. Almost always a raw contour where a descriptor was expected. Run fourierDescriptor first - the two nodes are a pair, not alternatives.

A shape that comes back mirrored or wildly shifted. Quaternion-free, but still order-sensitive: the four numbers in t and their semantics decide the result, and mirroring is not always what a "shape transform" implies. Test with an identity-ish transform (scale 1, zero rotation/translation) to confirm the plumbing before you trust a real one.

No visible output. The result is an array of coefficients or points, not an image. Convert with CV Points To Contour → draw, or preview the points with the pack's point-drawing nodes.

Treat it as a research tool. OpenCV documents this function and its neighbour in a couple of paragraphs; there's no community corpus of worked examples for either. The pack's wrappers are auto-generated and uncurated, and its README is candid that they're not production-grade without your own review - so if you're building a shape-matching feature on this, validate with your own test shapes rather than trusting a stranger's parameters.

Categoryimage/CV/low-level/ximgproc

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY,IMAGE,MASK - - - 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.
tNPARRAY,IMAGE,MASK - - - 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.
fdContouroptBOOLEANtrue - - - Preset to the OpenCV default (True).

Outputs (1)

NameTypeDescription
nparrayNPARRAY—