OpenCV solvePnPRansac_0
SolvePnP with RANSAC — pose estimation that shrugs off bad matches
- objectPoints
- imagePoints
- cameraMatrix
- distCoeffs
- rvec
- tvec
- inliers
- bool
- nparray_1
- nparray_2
- nparray_3
solvePnPRansac_0 is the robust sibling of solvePnP_0, and it exists for the exact reason RANSAC always exists: your 2D point detections are never clean. If you're tracking a face or a marker through a real photo, a chunk of your point matches will be garbage - a landmark that slipped, an occlusion, a false detection. Plain solvePnP feeds every match to the solver and lets the outliers drag the pose. solvePnPRansac runs the solve over random subsets, keeps the inliers, and returns a pose that ignores the junk.
That's the difference that matters, and it's why this is the node you'd reach for on real-world input while solvePnP_0 is the node for synthetic or hand-cleaned data.
The inputs
Same core as solvePnP_0 - objectPoints, imagePoints, cameraMatrix, distCoeffs - plus the RANSAC knobs:
- iterationsCount (INT) - max RANSAC iterations. More is more robust, slower. 100–1000 is the usual range.
- reprojectionError (FLOAT) - the max distance, in pixels, a point can be from its projection and still count as an inlier. This is the one you'll tune: too tight and you throw away good matches, too loose and outliers sneak in.
- confidence (FLOAT) - probability (0–1) that the returned pose is the right one. 0.99 is the typical value.
- flags (INT) - same OpenCV
SOLVEPNP_*enum assolvePnP;0(ITERATIVE) to start. - useExtrinsicGuess (BOOLEAN), plus optional rvec / tvec out-parameters.
Outputs: bool (success), then nparray_1 (rvec), nparray_2 (tvec), and - the bonus - nparray_3 (inliers), a list of the indices into imagePoints that survived RANSAC. That inliers output is genuinely useful: it tells you which matches were trusted, which is great for debugging a shaky tracker or feeding a cleanup step.
Wiring it
Standard pack plumbing - everything is NPARRAY, not IMAGE. Image2Nparray in, Nparrays2Image out, batch size 1. If a downstream function demands a single-channel image, remember the README's cvtColor codes (6 = BGR2GRAY).
One honest note: this node doesn't auto-magically make bad detections work. RANSAC needs a majority of good matches - if more than half your points are garbage, it can fail too. The bool output and the inliers count tell you when that's happening.
Install
Pack-level, one install for all 600+ nodes:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python
or ComfyUI Manager → search "opencv-comfyui". Restart. Dependencies are just opencv-contrib-python, numpy, torch - no model downloads, nothing heavy.
Troubleshooting
boolisFalseor inliers is tiny - your matches are mostly bad, orreprojectionErroris set too tight. Loosen it, or improve detection upstream.- NaNs in rvec/tvec - degenerate points or wrong
flags; try0(ITERATIVE). error: (-215:Assertion failed) img.type() == CV_8UC1- a single-channel image requirement was missed; convert withcvtColorcode6.Cannot import name 'guidedFilter'at startup - conflicting OpenCV installs; the README links the known fix.
This is the one I'd actually reach for in a real pipeline. solvePnP_0 is for clean data; solvePnPRansac is for the real world.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| objectPoints | NPARRAY | — | |
| imagePoints | NPARRAY | — | |
| cameraMatrix | NPARRAY | — | |
| distCoeffs | NPARRAY | — | |
| useExtrinsicGuess | BOOLEAN | — | |
| iterationsCount | INT | — | |
| reprojectionError | FLOAT | — | |
| confidence | FLOAT | — | |
| flags | INT | — | |
| rvecopt | NPARRAY | — | |
| tvecopt | NPARRAY | — | |
| inliersopt | NPARRAY | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| bool | BOOLEAN | — |
| nparray_1 | NPARRAY | — |
| nparray_2 | NPARRAY | — |
| nparray_3 | NPARRAY | — |