Nodes/comfyui_cv/CV Shape Distance
ComfyUI Node

CV Shape Distance

How different are two contours, as one number (cv2.ShapeContextDistanceExtractor / cv2.HausdorffDistanceExtractor). These live in OpenCV's 'shape' module as CLASSES, so no raw wrapper reaches them. Shape Context builds a log-polar histogram around every sample point and solves the correspondence between the two shapes, so it tolerates bending and scale - it is the strong matcher and much slower. It is NOT rotation tolerant unless 'rotation_invariant' is on (measured: a 40-degree turn costs 1.93 off, 0.010 on). Neither method is a mirror detector - use the 'mirror_mode' policy on 'CV Filter Contours By Shape' for that. Hausdorff is the worst-case nearest-point distance: cheap, purely geometric, and very sensitive to a single outlier point. Compare with cv2_matchShapes (Hu moments), which is faster still but only sees seven global numbers. SMALLER = more similar; feed the distance into a comparison node to accept/reject a match.

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Shape Distance
  • contours_a
  • contours_b
  • distance
methodShape Context (deformation-aware)
sample_points100
index_a0
index_b0
angular_bins12
radial_bins4
hausdorff_normL2 (euclidean)
rank_proportion0.60
rotation_invariantfalse
Categoryimage/CV/contours

Inputs (11)

NameTypeDefaultDescription
contours_aCV_CONTOURSFirst shape. A whole contour LIST is accepted; the contour selected by 'index_a' is used.
contours_bCV_CONTOURSSecond shape, compared against the first.
methodCOMBOShape Context (deformation-aware)Shape Context handles deformation and returns a unitless matching cost; Hausdorff returns a distance in PIXELS, so it only makes sense on shapes that are already aligned and at the same scale.
sample_pointsINT1004–1000Both contours are resampled to this many points before comparing, which is what makes contours of different lengths comparable. More points = finer and slower (Shape Context cost grows quickly).
index_aoptINT00–10000Which contour of 'contours_a' to use (0 = the largest, since 'CV Find Contours' sorts by area).
index_boptINT00–10000Which contour of 'contours_b' to use.
angular_binsoptINT124–64Shape Context only: angular bins of the log-polar histogram.
radial_binsoptINT42–32Shape Context only: radial bins of the log-polar histogram.
hausdorff_normoptCOMBOL2 (euclidean)Hausdorff only: the point-to-point norm used.
rank_proportionoptFLOAT0.600–1Hausdorff only: use this RANK instead of the true maximum (0.6 = the 60th-percentile nearest-point distance), which makes it robust to a few outliers. 1.0 is the classic worst-case Hausdorff distance.
rotation_invariantoptBOOLEANfalseShape Context only: measure each log-polar histogram against the local tangent instead of a fixed axis, which is what actually makes it rotation tolerant (measured on a 40-degree turn: 1.930 off, 0.010 on; on a 20-degree turn 0.028 off, 0.009 on). It DEGRADES past about 160 degrees, and it is not a mirror test - a reflected shape lands among the large-rotation scores. Off by default so saved graphs keep their numbers.

Outputs (1)

NameTypeDescription
distanceFLOATShape dissimilarity - 0 means identical. Unitless for Shape Context, PIXELS for Hausdorff.