Nodes/ComfyUI CV/CV Pick Value (sorted)
ComfyUI Node

CV Pick Value (sorted)

Pull one number out of an array — 'the angle of the hand with the largest radius'

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
CV Pick Value (sorted)
  • values
  • key
  • value
  • index
◄orderlargest first►
◄rank1►

Most of computer vision produces arrays, and most interesting questions have scalar answers. Which detection was the biggest? What's the angle of the hand with the largest radius? What's the closest distance? CV Pick Value (sorted) is the two-line bridge between those worlds: sort the values, take the rank-th one.

It sounds trivial, and it is - that's why it's useful. The alternative is a Python node or a Math Expression contraption, and both of those get rebuilt every time.

How it works

Four inputs, two of them the point:

  • values - the (N,) numeric array to pick from.
  • order - largest first (default) or smallest first.
  • rank - 1 = first in the chosen order. Clamped to the array length, so rank 5 on a 3-element array gives you the smallest rather than an error.
  • key (optional) - sort by these values instead, same length, while the output still comes from values. This is the feature that makes it worth a node: it's the difference between "the biggest score" and "the angle belonging to the biggest radius". Feed radii as key, angles as values, and you get the angle of the largest blob without a sorting node pair.

Outputs are value (FLOAT) and index (INT) - the position in the original input array, which is how you get back to a row of a detection table, an id, or a label. index is -1 when the input is empty, and value is 0.0; the node never raises on an empty array. That's consistent with how the rest of this pack behaves (fail soft, branch on a flag), but note there's no found output here - so gate on an upstream count if empty-input ambiguity would be a real problem.

Where it fits

The natural neighbours are CV Array Statistic (which reduces a whole array to one value per channel - median, mean, std) and CV Array To Numbers (which emits a list of floats so a downstream graph runs once per value, e.g. an Inspire foreach over detections). Pick one value when you want a single number to drive a widget or a filename; go to the list node when you want the graph to run per item.

Typical uses: picking the best match out of a match-scores array, pulling the dominant motion magnitude out of an optical-flow statistic, choosing the largest connected component's area, or resolving "which of these candidate boxes wins" after CV NMS Boxes.

Install

Manager → search ComfyUI CV, or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
cd comfyui_cv && pip install "opencv-contrib-python-headless~=5.0.0.93"

Restart afterwards. Python ≥ 3.12 and a ComfyUI on the V3 node API. No models, no downloads, nothing contrib-specific for this node - but the pack as a whole declares the pinned contrib headless wheel.

Common issues

  • "values has N entries but key has M" - a deliberate mismatch error. Both arrays must describe the same items, in the same order.
  • You expected an empty input to error - it doesn't. 0.0 and index = -1 is a valid result; wire a count in front of it if you need to distinguish "nothing found" from "the value is zero".
  • rank past the end gives you the other extreme - that's the clamp. Check the array length, or wire a count.
  • INT and FLOAT sockets won't connect downstream - the pack's sockets don't auto-coerce. value is a FLOAT; cast it (e.g. a basic-data-handling convert node) if a consumer wants an integer.
  • Multi-dimensional array misbehaving - flatten first; this node wants (N,). CV Permute Axes or a reshape upstream is the fix.

Standard pack disclaimer: heavy LLM-assisted development, explicitly not production-ready, updates not promised. This one is arithmetic you can verify by hand, which makes it one of the low-risk nodes in the collection.

Categoryimage/CV/points

Inputs (4)

NameTypeDefaultDescription
valuesNPARRAY(N,) numeric array.
orderCOMBOlargest firstSort direction before applying rank (by values, or by 'key' if connected).
rankINT11–40961 = first in the chosen order; clamped to N.
keyoptNPARRAYSort by these values instead (same length); the output still comes from 'values'.

Outputs (2)

NameTypeDescription
valueFLOAT—
indexINTPosition in the input array (-1 when empty).