Skip to content

USAC is incompatible with vector<char> mask in findHomography #19936

@ersincine

Description

@ersincine
System information (version)
  • OpenCV => 4.5.2-dev
  • Operating System / Platform => Ubuntu 18.04.5
  • Compiler => gcc 7.5.0
Detailed description

cv::findHomography with cv::USAC_DEFAULT does not work with a mask of type std::vector<char>. It requires an std::vector<uchar>. This is a problem, because, for example, cv::drawMatches requires a std::vector<char>.

When the method is chosen as cv::RANSAC instead of cv::USAC_DEFAULT, cv::findHomography works correctly.

In short:
The code below works correctly:

std::vector<char> mask;
cv::Mat H = cv::findHomography(obj, scene, mask, cv::RANSAC);

However, the code below throws an exception:

std::vector<char> mask;
cv::Mat H = cv::findHomography(obj, scene, mask, cv::USAC_RANSAC);
Steps to reproduce
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>


int main() {

    cv::Mat img1 = cv::imread(cv::samples::findFile("box.png"), cv::IMREAD_GRAYSCALE);
    cv::Mat img2 = cv::imread(cv::samples::findFile("box_in_scene.png"), cv::IMREAD_GRAYSCALE);

    cv::Ptr<cv::SIFT> sift = cv::SIFT::create();

    std::vector<cv::KeyPoint> keypoints1;
    sift->detect(img1, keypoints1);
    std::vector<cv::KeyPoint> keypoints2;
    sift->detect(img2, keypoints2);

    cv::Mat descriptors1;
    sift->compute(img1, keypoints1, descriptors1);
    cv::Mat descriptors2;
    sift->compute(img2, keypoints2, descriptors2);

    cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE);
    std::vector<cv::DMatch> matches;
    matcher->match(descriptors1, descriptors2, matches);

    std::vector<cv::Point2f> obj;
    std::vector<cv::Point2f> scene;
    for(size_t i = 0; i < matches.size(); ++i) {
        obj.push_back(keypoints1[matches[i].queryIdx].pt);
        scene.push_back(keypoints2[matches[i].trainIdx].pt);
    }

    std::vector<char> mask;
    cv::Mat H = cv::findHomography(obj, scene, mask, cv::USAC_DEFAULT); // OK for cv::RANSAC

    cv::Mat img_matches;
    cv::drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches, cv::Scalar::all(-1), cv::Scalar::all(-1), mask);
    cv::imshow("Matches", img_matches);
    cv::waitKey();

    return 0;
}
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions