OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 14 Oct 2016 08:36:27 -0500RANSAC and 2D point cloudshttp://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/I have 2 point clouds in 2D and I want to use RANSAC to determine transformation matrix between them.
I have no pairs of points, just 2 sets of points.
How can I do this in OpenCV?
----------
I tried to write ransac-like scheme for my purposes.
1. Get 4 random points from 1st set and from 2nd set.
2. Compute transform matrix H using getPerspectiveTransform.
3. Warp 1st set of points using H and test how they aligned to the 2nd set of points using some metric(I don't know what metric I should use I tried sum of min distance for all points in 1st set, but it seems it's not work good if points group together after transform)
4. Repeat 1-3 N times and choose best transform according to metric.
maybe I should use different metric?
Also I have idea that I can match points using shape context algorithm and only then use RANSAC(or some other algorithm) to determine transformation.Tue, 28 Aug 2012 02:45:39 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/Answer by sammy for <p>I have 2 point clouds in 2D and I want to use RANSAC to determine transformation matrix between them.
I have no pairs of points, just 2 sets of points.
How can I do this in OpenCV?</p>
<hr>
<p>I tried to write ransac-like scheme for my purposes.</p>
<ol>
<li>Get 4 random points from 1st set and from 2nd set.</li>
<li>Compute transform matrix H using getPerspectiveTransform.</li>
<li>Warp 1st set of points using H and test how they aligned to the 2nd set of points using some metric(I don't know what metric I should use I tried sum of min distance for all points in 1st set, but it seems it's not work good if points group together after transform)</li>
<li>Repeat 1-3 N times and choose best transform according to metric. </li>
</ol>
<p>maybe I should use different metric?</p>
<p>Also I have idea that I can match points using shape context algorithm and only then use RANSAC(or some other algorithm) to determine transformation.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?answer=1836#post-id-1836Just call
cv::Mat transform = cv::findHomography(firstPtVector, secndPtVector, CV_RANSAC);
The last parameter is optional, and it defaults to RANSAC. In C++, the point clouds must be either `std::vector<cv::Point>` either `cv::Mat` with one point per line.Tue, 28 Aug 2012 03:19:16 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?answer=1836#post-id-1836Comment by Mehdi for <p>Just call </p>
<pre><code>cv::Mat transform = cv::findHomography(firstPtVector, secndPtVector, CV_RANSAC);
</code></pre>
<p>The last parameter is optional, and it defaults to RANSAC. In C++, the point clouds must be either <code>std::vector<cv::Point></code> either <code>cv::Mat</code> with one point per line.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=104652#post-id-104652@sammy , under some constraints (you know the relation is an affine transform for example), in most real world cases there is only one transform.Fri, 14 Oct 2016 08:36:27 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=104652#post-id-104652Comment by sammy for <p>Just call </p>
<pre><code>cv::Mat transform = cv::findHomography(firstPtVector, secndPtVector, CV_RANSAC);
</code></pre>
<p>The last parameter is optional, and it defaults to RANSAC. In C++, the point clouds must be either <code>std::vector<cv::Point></code> either <code>cv::Mat</code> with one point per line.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1846#post-id-1846If you do not have pairing, you cannot define anything. There are infinite combinations of possibilities of transforms between two points sets. Tue, 28 Aug 2012 07:03:47 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1846#post-id-1846Comment by mrgloom for <p>Just call </p>
<pre><code>cv::Mat transform = cv::findHomography(firstPtVector, secndPtVector, CV_RANSAC);
</code></pre>
<p>The last parameter is optional, and it defaults to RANSAC. In C++, the point clouds must be either <code>std::vector<cv::Point></code> either <code>cv::Mat</code> with one point per line.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1849#post-id-1849I don't fink so, for example http://www.mrpt.org/Iterative_Closest_Point_(ICP)_and_other_matching_algorithms
I just don't know how to generalize ICP algorithm to perspective transform.Tue, 28 Aug 2012 08:15:01 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1849#post-id-1849Comment by mrgloom for <p>Just call </p>
<pre><code>cv::Mat transform = cv::findHomography(firstPtVector, secndPtVector, CV_RANSAC);
</code></pre>
<p>The last parameter is optional, and it defaults to RANSAC. In C++, the point clouds must be either <code>std::vector<cv::Point></code> either <code>cv::Mat</code> with one point per line.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1842#post-id-1842but we must have pair point in vectors firstPtVector, secndPtVector isn't it? I have just 2 point sets without pairs.Tue, 28 Aug 2012 06:12:38 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1842#post-id-1842Answer by Jacek for <p>I have 2 point clouds in 2D and I want to use RANSAC to determine transformation matrix between them.
I have no pairs of points, just 2 sets of points.
How can I do this in OpenCV?</p>
<hr>
<p>I tried to write ransac-like scheme for my purposes.</p>
<ol>
<li>Get 4 random points from 1st set and from 2nd set.</li>
<li>Compute transform matrix H using getPerspectiveTransform.</li>
<li>Warp 1st set of points using H and test how they aligned to the 2nd set of points using some metric(I don't know what metric I should use I tried sum of min distance for all points in 1st set, but it seems it's not work good if points group together after transform)</li>
<li>Repeat 1-3 N times and choose best transform according to metric. </li>
</ol>
<p>maybe I should use different metric?</p>
<p>Also I have idea that I can match points using shape context algorithm and only then use RANSAC(or some other algorithm) to determine transformation.</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?answer=1861#post-id-1861I don't think RANSAC is a good idea in your case.
RANSAC can be used when you have a number of measurements (e.g. pairs of corresponding points from 2 sets) containing some outliers (e.g. incorrectly matched points). The more outliers you have the more RANSAC iterations are needed to estimate parameters with a given confidence.
In your scenario, choosing matching points randomly, you'll need a really HUGE number of iterations to ensure proper matching is found. Computational cost may be prohibitive.
Can you give more details about your problem. Why are you trying to estimate perspective transform? Are these 2D points projections of some 3D points?
Tue, 28 Aug 2012 12:06:14 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?answer=1861#post-id-1861Comment by mrgloom for <p>I don't think RANSAC is a good idea in your case.
RANSAC can be used when you have a number of measurements (e.g. pairs of corresponding points from 2 sets) containing some outliers (e.g. incorrectly matched points). The more outliers you have the more RANSAC iterations are needed to estimate parameters with a given confidence.
In your scenario, choosing matching points randomly, you'll need a really HUGE number of iterations to ensure proper matching is found. Computational cost may be prohibitive.</p>
<p>Can you give more details about your problem. Why are you trying to estimate perspective transform? Are these 2D points projections of some 3D points?</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1883#post-id-1883The problem is that implementation of ICP that I found have only translate and rotation invariance, but I need to register images with perspective transform.Wed, 29 Aug 2012 08:21:25 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1883#post-id-1883Comment by mrgloom for <p>I don't think RANSAC is a good idea in your case.
RANSAC can be used when you have a number of measurements (e.g. pairs of corresponding points from 2 sets) containing some outliers (e.g. incorrectly matched points). The more outliers you have the more RANSAC iterations are needed to estimate parameters with a given confidence.
In your scenario, choosing matching points randomly, you'll need a really HUGE number of iterations to ensure proper matching is found. Computational cost may be prohibitive.</p>
<p>Can you give more details about your problem. Why are you trying to estimate perspective transform? Are these 2D points projections of some 3D points?</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1868#post-id-1868I need to align raster and vector data, raster data is distorted, I represent the, as point clouds in 2D. Look here there is no need for pair points http://www.mrpt.org/Iterative_Closest_Point_(ICP)_and_other_matching_algorithms but I need non rigid ICP.Wed, 29 Aug 2012 01:11:40 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1868#post-id-1868Comment by Jacek for <p>I don't think RANSAC is a good idea in your case.
RANSAC can be used when you have a number of measurements (e.g. pairs of corresponding points from 2 sets) containing some outliers (e.g. incorrectly matched points). The more outliers you have the more RANSAC iterations are needed to estimate parameters with a given confidence.
In your scenario, choosing matching points randomly, you'll need a really HUGE number of iterations to ensure proper matching is found. Computational cost may be prohibitive.</p>
<p>Can you give more details about your problem. Why are you trying to estimate perspective transform? Are these 2D points projections of some 3D points?</p>
http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1875#post-id-1875If distortion is not too big you may still use ICP. ICP doesn't require that 2 point clouds are exactly identical. One point cloud may contain points corrupted with a noise and ICP will still work.
The catch is that ICP requires point clouds to be roughly aligned. So if corresponding points in your point clouds are quite close you may use ICP. Otherwise you'll need to use some method to roughly align your point clouds (e.g. compute some kind of feature descriptors for each point, use these feature descriptors to create matching between points in both clouds and roughly align point clouds using these matchings) and then use ICP for final registration.Wed, 29 Aug 2012 03:40:58 -0500http://answers.opencv.org/question/1834/ransac-and-2d-point-clouds/?comment=1875#post-id-1875