Nodes/opencv-comfyui/OpenCV sortIdx_0
ComfyUI Node

OpenCV sortIdx_0

Sort values, keep the indices — sortIdx is the permutation node

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV sortIdx_0
  • src
  • dst
  • nparray
flags

sort_0 sorts your values. sortIdx_0 sorts them and throws the values away - you get back the permutation, the "which original position ends up where" map. That distinction sounds academic until you need it, and then it's exactly the node you wanted: instead of a sorted list of pixels or scores, you get an array of indices you can use to rank, reorder, or select from the original data without losing where everything came from.

Same family as sort_0, same flags bitmask, one crucial difference in the output. If sort_0 is the pixel-sorting glitch node, sortIdx_0 is its planner: it tells you which pixels would move where if you sorted, which is what you actually need for masked pixel-sorting, ranking-based selection, or any workflow that reorders one array by the ordering of another.

The inputs

  • src (NPARRAY) - the array to sort. From an image via Image2Nparray (batch size 1), or any nparray in your graph.
  • flags (INT) - same bitmask as cv2.sort: direction 0 ascending / 16 descending, axis 0 rows / 1 columns. 0, 1, 16, 17 are the values that matter.
  • dst (NPARRAY, optional) - out-parameter; unwired is fine, the README recommends skipping out-params anyway.

Output: a single nparray of integer indices. Same shape as src, but each element is the original index that lands in that sorted position.

Where this actually helps

A couple of real uses in a graph:

  • Ranking without destroying data. Sort a confidence/score array and use the indices to pick the top-N items from a parallel array that holds the actual content. The values stay put; the ranking is a separate map.
  • Masked pixel sorting. Compute the sort order of your image's columns, then apply that same permutation selectively to a region - the index map lets you reuse one ordering across several arrays (the pixels and a mask together).
  • Deterministic reordering for feeding data to nodes that expect monotonic or fixed-order input.

The caveat: the output is an integer index array, which is not an image. Run it into Nparrays2Image and you'll hit the README's documented 'NoneType' object has no attribute 'shape' wall. Index maps are data, not pixels.

Install

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

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

Troubleshooting

  • 'NoneType' object has no attribute 'shape' - you fed an index array to Nparrays2Image; it's data, not a picture.
  • Indices look wrong - double-check flags: 0/1 are ascending, 16/17 descending, and the axis bit decides rows vs columns.
  • Only images with batch_size==1 are supported! - split with ImageFromBatch (length=1).
  • Cannot import name 'guidedFilter' at startup - conflicting OpenCV packages; README links the fix.

It's the quiet one in the sorting family, but for anything that needs ordering information rather than ordered values, it's the right tool.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
flagsINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY