cv::findHomography needs a parameter for RANSAC iterations (Feature #1544)


Added by Nghia Ho about 13 years ago. Updated over 9 years ago.


Status:Open Start date:
Priority:Normal Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:calibration, 3d
Target version:-
Difficulty: Pull request:

Description

The cv::findHomography function lacks a parameter to control the number of RANSAC iterations. OpenCV by default sets it to a max of 2000. If there is truly a match between the src and dst image then the number of iterations performed is much lower in practice (due to adaptive calculation). The converse is true, if there are no valid matches then you're likely to reach 2000 iterations. This is a problem if you need it to run within a fixed amount of time eg. for real-time.

Here is the result of my own testing using the following code:

 1
 2#include <opencv2/calib3d/calib3d.hpp>
 3#include <iostream>
 4#include <cstdio>
 5#include <sys/time.h>
 6#include <numeric>
 7
 8double TimeDiff(timeval t1, timeval t2)
 9{
10    double t;
11    t = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
12    t += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
13
14    return t;
15}
16
17int main()
18{
19    // number of matches
20    int n = 20; 
21
22    // imaginary image
23    int w = 640;
24    int h = 480;
25
26    cv::Mat src(n, 2, CV_32F);
27    cv::Mat dst(n, 2, CV_32F);
28
29    for(int i=0; i < n; i++) {
30        src.at<float>(i,0) = w*(rand()/(1.0+RAND_MAX));
31        src.at<float>(i,1) = h*(rand()/(1.0+RAND_MAX));
32
33        dst.at<float>(i,0) = w*(rand()/(1.0+RAND_MAX));
34        dst.at<float>(i,1) = h*(rand()/(1.0+RAND_MAX));
35    }
36
37    timeval t1, t2;
38    std::vector <uchar> mask;
39
40    gettimeofday(&t1, NULL);
41    cv::findHomography(src, dst, CV_RANSAC, 3.0, mask);
42    gettimeofday(&t2, NULL);
43
44    std::cout << "inliers = " << std::accumulate(mask.begin(), mask.end(), 0) << endl;
45    std::cout << TimeDiff(t1,t2) << " ms" << std::endl;
46}

The code above will produce a src/dst matrix that will have NO valid matches. Running this I get

inliers = 5
75.93 ms

The number of RANSAC iteration done was 1354 (I modified the OpenCV code to print it out). For some real-time application, 75ms and above is too high. It would be nice to allow the user to choose the maximum number of iterations, suited for their needs.

It is trivial to add an extra parameter with a default value to cvFindHomography eg.

1CV_IMPL int
2cvFindHomography( const CvMat* objectPoints, const CvMat* imagePoints, CvMat* H, int method, double ransacReprojThreshold, CvMat* mask, int maxiters )

and the best part is it won't break anything! (shouldn't anyway)


Issue hierarchy

Feature #1544: cv::findHomography needs a parameter for RANSAC iterationsOpenVadim Pisarevsky

Feature #1557: cv::findHomography needs a parameter for RANSAC confidenceOpenVadim Pisarevsky


Associated revisions

Revision 9bfa346c
Added by Andrey Pavlenko over 11 years ago

Merge pull request #1544 from ilya-lavrenov:ocl_blendLinear

History

Updated by Alexander Shishkov about 13 years ago

  • Description changed from The cv::findHomography function lacks a parameter to control the number of RA... to The cv::findHomography function lacks a parameter to control the number of RA... More

Updated by Alexander Shishkov about 13 years ago

  • Description changed from The cv::findHomography function lacks a parameter to control the number of RA... to The cv::findHomography function lacks a parameter to control the number of RA... More

Updated by Alexander Shishkov almost 13 years ago

  • Target version deleted ()
  • Description changed from The cv::findHomography function lacks a parameter to control the number of RA... to The cv::findHomography function lacks a parameter to control the number of RA... More

Updated by Alexander Shishkov almost 13 years ago

  • Target version deleted ()

Updated by Maksim Shabunin over 9 years ago

Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/4321

Also available in: Atom PDF