Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@j3knk
Copy link
Contributor

@j3knk j3knk commented Jun 27, 2024

Fixes #25318

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov asmorkalov added this to the 4.11.0 milestone Jun 28, 2024
@asmorkalov asmorkalov self-requested a review June 28, 2024 09:23
@asmorkalov
Copy link
Contributor

The solution is not complete:

python3 ./repro.py 
Traceback (most recent call last):
  File "./repro.py", line 21, in <module>
    imagePoints, _ = cv2.projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs)
cv2.error: OpenCV(4.10.0-dev) /home/alexander/Projects/OpenCV/opencv-master/modules/calib3d/src/calibration.cpp:613: error: (-5:Bad argument) Translation vector must be 1x3 or 3x1 floating-point vector in function 'cvProjectPoints2Internal'

The similar should be done for tvec.

@j3knk
Copy link
Contributor Author

j3knk commented Jul 4, 2024

@asmorkalov could you show which data format you used as input? The translation vector should not be related to this issue. To me this looks more like the type assertion in projectPoints reacts to some invalid input

if( (CV_MAT_DEPTH(t_vec->type) != CV_64F && CV_MAT_DEPTH(t_vec->type) != CV_32F) ||
(t_vec->rows != 1 && t_vec->cols != 1) ||
t_vec->rows*t_vec->cols*CV_MAT_CN(t_vec->type) != 3 )
CV_Error( cv::Error::StsBadArg,
"Translation vector must be 1x3 or 3x1 floating-point vector" );

@j3knk
Copy link
Contributor Author

j3knk commented Jul 4, 2024

@asmorkalov could you show which data format you used as input? The translation vector should not be related to this issue. To me this looks more like the type assertion in projectPoints reacts to some invalid input

if( (CV_MAT_DEPTH(t_vec->type) != CV_64F && CV_MAT_DEPTH(t_vec->type) != CV_32F) ||
(t_vec->rows != 1 && t_vec->cols != 1) ||
t_vec->rows*t_vec->cols*CV_MAT_CN(t_vec->type) != 3 )
CV_Error( cv::Error::StsBadArg,
"Translation vector must be 1x3 or 3x1 floating-point vector" );

I realized just now that the original issue python code uses invalid data input. projectPoints expects tvec to be a 3x1 or 1x3 vector, not a 3x3 matrix.
The C++ example in the comments uses the right format.
#25318 (comment)

@vpisarev
Copy link
Contributor

vpisarev commented Jul 9, 2024

@j3knk, thank you for the patch! Could you please add a test that reproduces error in the original code?

@j3knk
Copy link
Contributor Author

j3knk commented Jul 10, 2024

@vpisarev I have used this example code from #25318 (comment) for testing. Did not have the time to create some randomized tests yet, but this triggers the original bug for sure.

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/calib3d.hpp"

int main( int argc, char* argv[] )
{
    using type = float;
    cv::Mat objectPoints = (cv::Mat_<type>(3,3) << 181.24588 ,  87.80361 ,  11.421074,
                                                    87.17948 , 184.75563 ,  37.223446,
                                                    22.558456,  45.495266, 246.05797);
    cv::Mat rvec = (cv::Mat_<type>(3,3) << 0.9357548 , -0.28316498,  0.21019171,
                                            0.30293274,  0.9505806 , -0.06803132,
                                            -0.18054008,  0.12733458,  0.9752903);
    cv::Mat tvec = (cv::Mat_<type>(3,1) << 69.32692 ,  17.602057, 135.77672);
    cv::Mat cameraMatrix = (cv::Mat_<type>(3,3) << 214.0047  ,  26.98735 , 253.37799,
                                                    189.8172  ,  10.038101,  18.862494,
                                                    114.07123 , 200.87277 , 194.56332);

    cv::Mat distCoeffs = cv::Mat_<type>(4, 1, (type)0);

    cv::Mat out;
    cv::projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs, out);

    std::cout << out << std::endl;
    return 0;
}

@j3knk
Copy link
Contributor Author

j3knk commented Jul 13, 2024

Equivalent in Python:

import cv2
import numpy as np

objectPoints = np.array([[181.24588 ,  87.80361 ,  11.421074],
       [ 87.17948 , 184.75563 ,  37.223446],
       [ 22.558456,  45.495266, 246.05797 ]], dtype=np.float32)
rvec = np.array([[ 0.9357548 , -0.28316498,  0.21019171],
       [ 0.30293274,  0.9505806 , -0.06803132],
       [-0.18054008,  0.12733458,  0.9752903 ]], dtype=np.float32)
tvec = np.array([ 69.32692 ,  17.602057, 135.77672 ], dtype=np.float32)
cameraMatrix = np.array([[214.0047  ,  26.98735 , 253.37799 ],
       [189.8172  ,  10.038101,  18.862494],
       [114.07123 , 200.87277 , 194.56332 ]], dtype=np.float32)
distCoeffs = distCoeffs = np.zeros((4, 1), dtype=np.float32)

imagePoints, _ = cv2.projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs)
print(imagePoints)

@asmorkalov asmorkalov requested a review from vpisarev July 15, 2024 06:26
@asmorkalov asmorkalov assigned asmorkalov and vpisarev and unassigned asmorkalov Jul 15, 2024
@asmorkalov asmorkalov merged commit e90935e into opencv:4.x Jul 15, 2024
@asmorkalov asmorkalov mentioned this pull request Jul 16, 2024
@j3knk j3knk deleted the calib3d/fix_projectpoints branch July 23, 2024 19:47
fengyuentau pushed a commit to fengyuentau/opencv that referenced this pull request Aug 15, 2024
calib3d: fix Rodrigues CV_32F and CV_64F type mismatch in projectPoints opencv#25824

Fixes opencv#25318

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected error when call cv2.projectPoints

3 participants