Repository: opencv
Issue: cv2.solvePnPRansac may be buggy
Labels: bug,category: calib3d

### System Information

opencv 4.7.0
Ubuntu 20.04
Python 3.8.10


### Detailed description

I can track successfully the pose of a camera using cv2.solvePnP but not using cv2.solvePnPRansac.
I've spent a day trying variations on the cv2.solvePnPRansac parameters, for example passing Nx1x3 and Nx1x2 points rather than Nx3 and Nx2. I've tried different reprojection errors and number of iterations and all the method available through flags.  The result is the same, cv2.solvePnP works but cv2.solvePnPRansac doesn't. See self-contained example to reproduce. 

solvePnPRansac returns the same pose as given in by rvec_i, tvec_i when useExtrinsicGuess=True
solvePnP return the correct pose. 

The example uses data for a time step only, but in the real system I'm using pnp in a loop processing a stream of points. So when I use solvePnPRansac the pose gets stuck not changing, whereas with solvePnP the pose evolves correctly.  I would expect solvePnPRansac to give the same or similar results to solvePnP and be robust to outliers but I'm not getting the expected results. 


### Steps to reproduce

```
import numpy as np
np.set_printoptions(linewidth=10000, precision = 3, edgeitems= 100, suppress=True)

import cv2 


K = np.array([[424.861,   0.   , 424.861],
            [  0.   , 421.488, 239.519],
            [  0.   ,   0.   ,   1.   ]])

rvec_i = np.array([ 0.046, -0.102, -0.029])
tvec_i = np.array([-0.004,  0.021, -0.88 ])


w_points = np.array([[ 1.917, -0.394, -0.004,  0.215, -0.356, -0.886, -0.16 ,  0.26 ,  0.037,  0.15 , -0.39 ,  0.263,  0.361, -0.068, -0.13 , -0.311, -0.008,  0.662, -0.829, -0.193,  1.65 ,  0.121, -0.142, -0.231,  1.069,  0.705, -0.51 ,  0.096,  0.496, -0.099,  0.336,  0.653,  0.71 ],
                            [ 0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368,  0.368],
                            [ 2.637,  1.988,  2.005,  1.915,  1.684,  2.329,  3.325,  1.459,  1.625,  1.772,  2.577,  3.243,  2.342,  1.658,  1.537,  1.723,  1.823,  3.167,  3.359,  1.708,  2.65 ,  1.925,  2.127,  1.856,  2.495,  2.152,  2.22 ,  1.627,  1.544,  1.609,  2.883,  1.669,  1.923]])


p_points = np.array([[784, 182, 343, 432, 125,  66, 335, 504, 349, 409, 254, 412, 458, 290, 230, 163, 334, 485, 213, 226, 729, 392, 298, 232, 625, 579, 177, 384, 629, 265, 432, 667, 619],
                    [285, 359, 349, 356, 415, 331, 278, 462, 416, 380, 307, 277, 313, 412, 452, 404, 375, 277, 281, 403, 287, 357, 338, 375, 298, 323, 335, 414, 422, 426, 288, 385, 345]])


print("before                        rvec: {} tvec: {}".format(rvec_i.ravel(), tvec_i.ravel()))            
retval, rvec, tvec, inliers = cv2.solvePnPRansac(
    w_points.astype(np.float32).T,
    p_points.astype(np.float32).T,                                             
    K, None, rvec_i[:,None].copy(),tvec_i[:,None].copy(), True,
    iterationsCount=1000, reprojectionError=8, 
    flags=cv2.SOLVEPNP_ITERATIVE)   
print("solvePnPRansac  retval: {} rvec: {} tvec: {} n_inliers: {}".format(retval, rvec.ravel(), tvec.ravel(), "None" if inliers is None else inliers.shape[0]))            

retval, rvec, tvec = cv2.solvePnP(
    w_points.astype(np.float32).T, 
    p_points.astype(np.float32).T,                                             
    K, None, rvec_i.copy(), tvec_i.copy(), True)     
print("solvePnP        retval: {} rvec: {} tvec: {}".format(retval, rvec, tvec))       
```

Output:
```
before                        rvec: [ 0.046 -0.102 -0.029] tvec: [-0.004  0.021 -0.88 ]
solvePnPRansac  retval: False rvec: [ 0.046 -0.102 -0.029] tvec: [-0.004  0.021 -0.88 ] n_inliers: None
solvePnP        retval: True rvec: [ 0.056 -0.106 -0.019] tvec: [-0.001  0.029 -0.909]
```


### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [x] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)