Nodes/opencv-comfyui/OpenCV pointPolygonTest_1
ComfyUI Node

OpenCV pointPolygonTest_1

PointPolygonTest_1 — the literal-syntax trap and how to hit-test without losing an hour

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV pointPolygonTest_1
  • contour
  • float
pt
measureDist

Before anything else: pointPolygonTest_1 is the identical twin of pointPolygonTest_0. They're both ports of OpenCV's cv2.pointPolygonTest, generated twice because the function has two overload signatures in OpenCV's type stubs. The pack that made them, opencv-comfyui, auto-generates a node for every overload of every standalone OpenCV function - that's why you see _0 and _1 variants everywhere in this pack. Same behavior, pick either, don't lose sleep over which.

What it does is beautifully simple: given a polygon (a contour) and a point, it returns a float - positive if the point is inside the polygon, negative if outside, zero if it's sitting on the boundary. Flip measureDist to True and that float becomes the actual signed distance in pixels, so you know not just whether the point is inside but how far in.

The trap that eats an hour

This node takes a pt input of type STRING, and here's the thing the README warns about with the "invalid syntax (<unknown>, line 0)" error: composite OpenCV values are entered as Python literals. So the point is typed as (x, y) - parentheses, comma, no spaces unless you want them:

(512, 384)

Not 512, 384, not [512, 384], not 512 384. The pack parses the text box with Python's ast.literal_eval, and anything that isn't a valid Python literal blows up with a cryptic syntax error that gives you zero hint which field it was. It's the single most common way people bounce off this pack, and it's a five-second fix once you know the pattern: every composite-typed field in this pack wants a Python literal - a point is (x, y), a size is (w, h), a color is (r, g, b).

The other gotcha: contour shape

The contour input expects a single contour array, the kind cv2.findContours emits - shape (N, 1, 2) or (N, 2), typically int32. Wire in a list of contours, or a mask directly, and you'll get a shape mismatch that isn't obvious at a glance. If your source is a mask, run it through findContours (also in this pack) and feed the first contour out.

Where it fits in a real workflow

Think of pointPolygonTest_1 as a guard. You've got a face detected somewhere in your frame, a region you've defined, and you only want to run an inpaint or a detail pass if the face is actually inside that region. Feed the detected point and your polygon in, and route the result through a switch or logic node - "only process if inside." It turns a yes/no geometric fact into data your graph can branch on, without running any model. That's the honest use case, and it's the one worth building.

Install

Same as every node in opencv-comfyui: ComfyUI Manager → search "opencv-comfyui", or

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

restart, then make sure OpenCV is present (pip install opencv-contrib-python). Work through Image2Nparray (batch size 1 only - ImageFromBatch if it complains), and don't expect image output here; the output is a scalar float meant for logic, not pixels.

If you're new to the pack, the author said it best: these nodes are "ugly and complex to use. Expect dragons!" But a point-in-polygon test is one of the least dragon-y nodes in the bunch once you've internalized the literal-syntax rule.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
contourNPARRAY
ptSTRING
measureDistBOOLEAN

Outputs (1)

NameTypeDescription
floatFLOAT