Nodes/opencv-comfyui/OpenCV matchShapes_0
ComfyUI Node

OpenCV matchShapes_0

Compare two contours and get a similarity score

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV matchShapes_0
  • contour1
  • contour2
  • float
method
parameter

"Is this shape basically the same as that shape, ignoring size and rotation?" That's the exact question OpenCV matchShapes_0 answers. It wraps cv2.matchShapes, which compares two contours and returns a FLOAT score: lower is more similar, 0 means identical. No image comes out - like Mahalanobis_0 and matchShapes_1's siblings, this one outputs a number you feed into thresholding or comparison logic.

It's a classic computer-vision utility: after you've extracted contours from an image, you can ask how much one shape resembles a reference. Realistic generative use: checking whether a detected region matches an expected object shape, filtering detections by "is this blob roughly the right silhouette," or simply understanding your data. One honest wrinkle: this pack wraps matchShapes but does not ship a contour-extraction node - there's no findContours in it, so the contour NPARRAYs have to come from another source (a different pack, or a custom node). Budget for that.

How the scoring works

The input contours are normalized so the comparison is largely invariant to translation, scale, and rotation - the standard trick is to compare moments of the contour (its shape statistics) rather than raw point coordinates. The method selects which moment-comparison formula:

  • 1 - CONTOURS_MATCH_I1 (the default)
  • 2 - CONTOURS_MATCH_I2
  • 3 - CONTOURS_MATCH_I3

The formulas weight things slightly differently, but for a beginner the answer is "use 1." The parameter input is a legacy tuning value; leave it at 0.0, which the docs treat as "use the method's default."

Inputs:

  • contour1, contour2 - the two contours as NPARRAYs. These come from contour-extraction nodes, not from Image2Nparray - a contour is a list of points, not a picture.
  • method - 1, 2, or 3.
  • parameter - 0.0.

Output: float.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart, or ComfyUI Manager → "opencv". Dependencies: opencv-contrib-python, numpy, torch.

Troubleshooting

  • "My contours won't connect" - contour type matters. If your point array has the wrong shape, you'll get assertion failures. Check your contour source - and remember this pack doesn't provide one.
  • Scores that don't look "similarity-like" - higher = worse here. Don't threshold the wrong way.
  • Batch error - the pack's batch_size==1 rule applies to images; contours are point lists, so this node isn't affected.

The _1 variant is identical (overload-numbering quirk). Honest take: this is a niche tool you reach for when doing shape-level analysis of detected regions. It's the right primitive, faithfully wrapped - you just have to be doing contour work for it to matter.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
contour1NPARRAY
contour2NPARRAY
methodINT
parameterFLOAT

Outputs (1)

NameTypeDescription
floatFLOAT