首页 > 解决方案 > 计算机视觉(使用 opencv c++ 对齐),出现错误

问题描述

我正在尝试在 Visual Studio 中使用getPerspectiveTransformgetAffineTransform运行,但是当我运行代码时,我会看到此消息,并且代码根本无法正常工作。

这是我的代码的一部分:

    //Mat im_src = imread("img_name.png",-1);
    // Five corners of the source image
    vector<Point2f> pts_src;
    pts_src.push_back(Point2f(106, 115));
    pts_src.push_back(Point2f(141, 111));
    pts_src.push_back(Point2f(129, 134));
    pts_src.push_back(Point2f(109, 150));
    pts_src.push_back(Point2f(147, 146));


    // Read destination
    // Five corners of the book in destination image.
    vector<Point2f> pts_dst;
    pts_dst.push_back(Point2f(30.2946, 51.6963));
    pts_dst.push_back(Point2f(65.5318, 51.5014));
    pts_dst.push_back(Point2f(48.0252, 71.7366));
    pts_dst.push_back(Point2f(33.5493, 92.3655));
    pts_dst.push_back(Point2f(62.7299, 92.2041));

    // Calculate Homography
    Mat h = cv::getPerspectiveTransform(pts_src, pts_dst);
    //The error is due to the previous line 
    // Output image
    Mat im_out;
    // Warp source image to destination based on homography
    Size imgSize(112, 96);
    warpPerspective(img, im_out, h, imgSize);

    // Display images
    imshow("Source Image", im_src);
    imshow("Warped Source Image", im_out);

这是发生的错误:

这是发生的错误

标签: c++visual-studioopencv

解决方案


根据opencv文档:

https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#Mat%20getPerspectiveTransform(InputArray%20src,%20InputArray%20dst)

cv::getPerspectiveTransform()

使用两个 4 元素向量。您传递了 5 元素向量 - 这可能是您的问题的根源。如果您想使用更多积分,您应该考虑

cv::findHomography()

带有 RANSAC 标志


推荐阅读