Facing errors while converting Eigen::MatrixXd to cv::Mat

I’m trying to convert an Eigen::MatrixXd to cv::Mat and I’m facing the main error

C++ exception with description "OpenCV(4.6.0) /home/ci/opencv/modules/imgproc/src/imgwarp.cpp:3392: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'

My C++ code is

    getPerspectiveTransform(Eigen::MatrixXd quadrangle, Eigen::MatrixXd warpedQuadrangle) {
        cv::Mat transMat;
        cv::Mat quad(4,2,CV_32FC1);
        cv::Mat warpedQuad(4,2,CV_32FC1);
        std::cout << "[        ] quadrangle in cv::Mat " << quad << std::endl;
        transMat = cv::getPerspectiveTransform(quad,warpedQuad);
        return transMat;

check the shapes and whatnot after the eigen2cv calls. most OpenCV APIs are allowed to, and will, resize/retype any output matrices.

What should I be checking? Just Mat shape? I did a checkVector(2,CV_32F) which returned -1 but checkVector(2,CV_64F) returned 4.

1 Like

good, that you checked that !
it now looks, like your eigen Mats are float64, while opencv’s getPerspectiveTransform() expects float32 (only!)

(also see this overload which explicitly wants Point2f[])

crosspost: c++ - cv::getPerspectiveTransform throws Error after cv::eigen2cv - Stack Overflow

Yep that was asked by my team mate :slight_smile: :gorilla:

Yes the StackOverflow answer we received I feel are kind of right :slight_smile: