Repository: opencv
Issue: Default Z-Axis For SolvePnP Charuco Board Seems to be Into Board - Inconsistent Indexing of Charuco Corners to Previous Versions
Labels: bug

### System Information

**OpenCV Versions:** 
Unexpected behavior in opencv-python==4.10.0 , expected behavior in opencv-contrib-python==4.5.5.62
****Operating System / Platform:** Ubuntu 22.04
Compiler and Compiler Version**: GCC 11.4.0
Python Version: 3.10.12




### Detailed description

I'm working on a Charuco based camera calibration. My calibration previously worked in opencv-contrib-python==4.5.5.62 , and I was working on porting it to opencv-python=4.10.0 to get around the breaking changes introduced in 4.7.

However, I noticed inconsistencies between the order of corners returned by the the charuco detector, that lead to a rotation of 180 degrees about the X axis for estimated board pose (with SolvePnp) between versions. While the earlier version's Z axis points out of the board, in the later version, it points into the board (from SolvePnp)

I noticed that the indexing of the internal corners returned by the new charuco detector (after 4.7) seems to start at the 
top left corner. However, the indexing of the internal corners returned by the old charuco detector (prior to 4.7) seems to start at the bottom left corner. 

In order for the Z axis to point out of the board towards the camera, which is what I'd expect according to convention, I found
that I needed to renumber the indexing of the internal corners to start at the bottom left corner for the newer version  to match the behavior of the older version when input to SolvePnp. I understand that 4.7 was a breaking change, and as such can change the behavior for later versions, but I'd expect the Z axis to still point out of the board, which doesn't happen with the default behavior that I observed.

I'm wondering here if I made a mistake with the way I am choosing the inputs for SolvePNP, or if this axis was flipped intentionally to be into board between versions, or if this is unintended behavior. I also tried changing the inputs for SolvePNP (using object points instead of chessboard corners for 3d, and using aruco tag corners instead of internal chessboard corners for 2d) for the newer version, but found that this resulted in the Z axis still being into the board.

### Prior to 4.7.0
![image](https://github.com/user-attachments/assets/8100886f-de7f-47f3-b7d3-fd40059d524f)
![image](https://github.com/user-attachments/assets/60c19661-f3a7-43a4-a347-2da398bbf89a)
![image](https://github.com/user-attachments/assets/13d28119-b53b-4d6a-9138-8d352388d869)
![image](https://github.com/user-attachments/assets/fef4812b-8e61-4bfc-8751-996aacdb2c3f)
![image](https://github.com/user-attachments/assets/a5f51efe-2959-4c4e-8dba-7f000aa9b841)
![image](https://github.com/user-attachments/assets/a6504442-9178-49e4-b0b3-92e79c51509d)


## After 4.7
![image](https://github.com/user-attachments/assets/c62061dd-2dda-4539-ae1b-cc88d8520aa5)
![image](https://github.com/user-attachments/assets/3da6c6bb-1dbb-4ffa-8f8a-62e11471a05c)
![image](https://github.com/user-attachments/assets/925d86dc-ed3e-450e-afbc-8080aeee7415)
![image](https://github.com/user-attachments/assets/4d963ac6-42fd-4102-a69f-b38c51cf5f60)
![image](https://github.com/user-attachments/assets/af7f2c50-09f5-46f9-b0cf-5e7b0873ef3d)
![image](https://github.com/user-attachments/assets/c4a836cf-ed57-4992-b0e2-4110652729d1)

### After 4.7.0 with enforcing ascending IDS from bottom left corner (Ad Hoc Bug Fix for Consistency):
![image](https://github.com/user-attachments/assets/8734385e-8928-432c-985c-0989df065123)
![image](https://github.com/user-attachments/assets/2ee5fc36-74b5-4c60-a226-9311dfbf8798)
![image](https://github.com/user-attachments/assets/08ab8fa6-0042-4d50-93b7-97eb9cf71489)
![image](https://github.com/user-attachments/assets/c1f355b1-2a43-4bd4-a806-ca47a1600ac2)
![image](https://github.com/user-attachments/assets/3b786f06-7e82-4707-a87e-f3dd014a7b71)
![image](https://github.com/user-attachments/assets/5eccc8ec-9f55-431f-838f-2d9839fdf729)


Potentially Related Issues:
https://github.com/opencv/opencv_contrib/issues/2921 : axis seems to be into board here 
https://github.com/opencv/opencv/issues/23152 : broken backwards compatibility 


### Steps to reproduce


First, download the gist from https://gist.github.com/glvov-bdai/d3d1c9f03d3d5ca6e61af296342498ff

Then, to recreate the figures I linked above, run the following in separate terminals (close figure to get next figure)

## Prior to 4.7.0 | In one terminal, for the older version (Out of Board)
```
python3 -m venv ~/venvs/older_cv2
source ~/venvs/older_cv2/bin/activate
pip install numpy==1.26.4
pip install opencv-contrib-python==4.5.5.62
pip install matplotlib
python3 opencv_charuco_detection.py
```

## After 4.7 | In another terminal, for the newer version (Into Board)
```
python3 -m venv ~/venvs/newer_cv2
source ~/venvs/newer_cv2/bin/activate
pip install opencv-python==4.10.0.84
pip install matplotlib
python3 opencv_charuco_detection.py --legacy
```


## In another terminal, for the newer version (After 4.7.0 with enforcing ascending IDS from bottom left corner (Ad Hoc Bug Fix for Consistency) (Out of Board):
```
source ~/venvs/newer_cv2/bin/activate
python3 opencv_charuco_detection.py --legacy --enforce_ascending_ids
```

### 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)