Ask Your Question

Revision history [back]

OpenCV.js binding issue for perspectiveTransform using Point2fVector as input

I am trying to call findHomography and perspectiveTransform from OpenCV.js. The issue I came across was that the binding of these two functions is only specified for Mat inputs and not std::vector<cv::Point2f> which I want to use. So I went ahead an created new bindings for these functions with these structs as inputs. However, findHomography seems to work while perspectiveTransform results in a runtime error. I am not really sure why since I essentially did the same thing for both. Any help or suggestions would be much appreciated.

Error Message LIVE EXAMPLE

image description

Original Bindings


Mat findHomography_wrapper(const cv::Mat& arg1, const cv::Mat& arg2, int arg3, double arg4, cv::Mat& arg5, const int arg6, const double arg7) {
    return cv::findHomography(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
void perspectiveTransform_wrapper(const cv::Mat& arg1, cv::Mat& arg2, const cv::Mat& arg3) {
    return cv::perspectiveTransform(arg1, arg2, arg3);


function("findHomography", select_overload<Mat(const cv::Mat&, const cv::Mat&, int, double, cv::Mat&, const int, const double)>(&Wrappers::findHomography_wrapper));

function("perspectiveTransform", select_overload<void(const cv::Mat&, cv::Mat&, const cv::Mat&)>(&Wrappers::perspectiveTransform_wrapper));

Custom Bindings


Mat findHomographyEasy(const std::vector<cv::Point2f>& arg1, const std::vector<cv::Point2f>& arg2, int arg3) {
    return cv::findHomography(arg1,arg2,arg3);

void perspectiveTransformEasy(const std::vector<cv::Point2f>& arg1, std::vector<cv::Point2f>& arg2, const cv::Mat& arg3) {
    cv::perspectiveTransform(arg1, arg2, arg3); // also tried with return here


function("findHomographyEasy", &binding_utils::findHomographyEasy); // WORKS PERFECTLY

function("perspectiveTransformEasy", &binding_utils::perspectiveTransformEasy); // CAUSES RUNTIME ERROR

HTML / Javascript Usage

<script src="./opencv.js" type="text/javascript"></script>

<script type="text/javascript">

    function match() {

        // SOME STUFF...

        var H = new cv.Mat();
        H = cv.findHomographyEasy(obj, scene, cv.FM_RANSAC);

        var obj_corners = new cv.Point2fVector();
        obj_corners[0] = new cv.Point(0,0);
        obj_corners[1] = new cv.Point(img1Raw.cols,0);
        obj_corners[2] = new cv.Point(img1Raw.cols, img1Raw.rows);
        obj_corners[3] = new cv.Point(0, img1Raw.rows);

        console.log(img1Raw.cols); // 500
        console.log(img1Raw.rows); // 363

        var scene_corners = new cv.Point2fVector();
        cv.perspectiveTransformEasy(obj_corners, scene_corners, H); // I know issue is here because I have surrounded this line with console.log