OpenCV cartToPolar_0
Gradient magnitude and direction in one shot — the cartToPolar node
- x
- y
- magnitude
- angle
- nparray_0
- nparray_1
cartToPolar is the vector-math helper you meet in every OpenCV gradient tutorial: you have two images holding the horizontal and vertical components of something - usually Sobel gradients, sometimes optical flow - and you want the magnitude and angle of each vector, which are the polar coordinates of the Cartesian pair. This node wraps that conversion. It's one of those tiny utility nodes you'd never search for and can suddenly not live without.
Why it's useful. Gradients are the backbone of a lot of image analysis. Magnitude tells you how strong an edge is at each pixel; direction tells you which way it runs. Optical-flow algorithms output x and y displacement fields - convert them with cartToPolar and you get speed and direction-of-motion maps, which are exactly what you want for motion analysis or stylized flow effects. You can also use it to build a magnitude map for your own edge/threshold logic before deciding whether to bother with a full Canny pass.
How it works. The math per pixel: magnitude = sqrt(x² + y²), angle = atan2(y, x). atan2 is the key detail - it gives angles in the full -π..π range (or -180..180 degrees), not the ambiguous 0..90 you'd get from a plain arctangent, so you can tell an edge going one way from an edge going the other.
Inputs and outputs. x and y (NPARRAY, same shape), and angleInDegrees (BOOLEAN) - set it and the angle output comes back in degrees instead of radians. The optional magnitude and angle inputs are out-parameters the generator exposed; you can ignore them. Outputs are nparray_0 (magnitude) and nparray_1 (angle). Note that these outputs are typically single-channel float images - if you try to pipe one straight into Nparrays2Image expecting a viewable picture, you may hit the README's 'NoneType' object has no attribute 'shape' (or just get something that doesn't look like an image), because a float magnitude map is not a normal RGB frame. It's data, not a display.
Install and quirks. Same pack, same rules: opencv-comfyui works in BGR 0..255 uint8 nparrays, batch size 1 only, bridge with Image2Nparray / Nparrays2Image. No models. ComfyUI Manager → search "OpenCV", or:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-python-contrib
Restart after. cartToPolar_1 is the identical twin - the pack numbers overloads, and MatLike/UMat variants come out as duplicates. Grab either. And as the author says, expect dragons.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| x | NPARRAY | — | |
| y | NPARRAY | — | |
| angleInDegrees | BOOLEAN | — | |
| magnitudeopt | NPARRAY | — | |
| angleopt | NPARRAY | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| nparray_0 | NPARRAY | — |
| nparray_1 | NPARRAY | — |