Random sampling consistency Algorithm (RANSAC) example and source code--Reprint

Source: Internet
Author: User

Reprinted from Wang Xianrong http://www.cnblogs.com/xrwang/p/SampleOfRansac.html

Wang Xianrong

About two years ago, the "random sampling consistency algorithm RANSAC" was translated, and at the end of the article it was promised to write the algorithm's C # sample program. Unfortunately time flies, in a blink of an eye for a long time to write, really sorry. This article uses a random sampling consistency algorithm to detect lines and circles, and to provide source code downloads.

First, RANSAC detection process

Here to retell the RANSAC inspection process, the detailed process see the previous translation article:

The input of the RANSAC algorithm is a set of observational data, a parametric model that can be interpreted or adapted to observational data, and some credible parameters.

Ransac The goal by repeatedly selecting a set of random subsets in the data. The selected subset is assumed to be an insider, and is validated using the following method:
1. There is a model adapted to the hypothetical insider point, where all the unknown parameters can be calculated from the hypothetical insider points.
2. Use the model obtained in 1 to test all other data, if a point applies to the estimated model, it is considered an insider point.
3. If there are enough points to be classified as hypothetical insider points, then the estimated model is reasonable enough.
4. The model is then re-evaluated with all the hypothetical insider points, as it is only estimated by the initial assumption-points.
5. Finally, the model is evaluated by estimating the error rate of the insider and the model.
This process is repeated for a fixed number of times, and each time the resulting model is either discarded because it is too small, or is chosen better than the existing model.

Ii. access to observational data

We do not have experimental (test) data, which is replaced by manually entered data--recording your click coordinates in PictureBox as the observational data.

Get sample points

Third, the detection line

3.1 Related knowledge of straight line

(1) Any two points on the plane can determine a straight line;

(2) The general mathematical expression of straight line is: ax+by+c=0. This form of expression has three unknowns and requires three points to solve the a,b,c three parameters. Because the randomly selected three points are not necessarily in a straight line, this method is discarded in the program.

(3) The straight line can be expressed by the two formulas of Y=ax+b and x=c. These two forms have only one or two unknowns, and only two points can solve the a,b,c three parameters. We use this form to get a straight line from two randomly selected points.

3.2 Linear class

Line has a list of properties and methods associated with the line, as follows:

(1) Properties

A In the A--y=ax+b

B in the B--y=ax+b

C in the C--x=c

(2) Constructors

Public line (PointF p1, PointF p2)

Two points P1 and P2 are provided to calculate the property a,b,c of the line.

(3) method

getdistance--gets the distance between the points and the line;

gety--Gets the y-coordinate of the point on the line according to the X coordinate;

tostring--gets the equation for the line.

Line class

3.3 The process of detecting a straight line

(1) Randomly select two points from the observation point to obtain a straight line through the point;

(2) using the Straight line in (1) to test other observation points, the distance from point to line to determine whether the observation point is an insider point or an outlier;

(3) If there are enough points in the office and more than the original "best" line, then the line of the iteration is set to the "best" line;

(4) Repeat (1) ~ (3) steps until the best line is found.

Careful you have found that I omitted the standard RANSAC detection process to re-estimate the model steps, I deliberately, I feel trouble and useless, so clicked, O (∩_∩) o~.

Get Line

Iv. Detection of the Circle

4.1 Related knowledge of the circle

(1) Three points in the plane that are not in the same line can determine a circle;

(2) The mathematical expression of the circle is: (x-a) (y-b) 2=R2

Where (A, b) is the center and r is the radius.

4.2 Round Class

The Circle Class (circle) encapsulates the properties and methods associated with the circle, and the list is as follows:

(1) Properties

X-coordinate of a--center

The y-coordinate of the b--center

r--radius of the Circle

(2) Constructors

Public Circle (PointF p1, PointF p2, PointF p3)

Provides three points p1,p2 and P3 to calculate the properties of the Circle A,b,r.

(3) method

getdistance--gets the distance between the point and the Circle (week), indicating that the point is close or away from the circle;

tostring--gets the equation for the circle.

Circle Class

3.3 The process of detecting a circle

(1) Randomly select three points from the observation point and try to get a circle through the three points;

(2) using the circle in (1) to test other observation points, the distance from the point to the circle to determine whether the observation point is an insider point or an outlier;

(3) If there are enough points in the Bureau and more than the original "best" circle, the circle of the iteration is set to the "best" circle;

(4) Repeat (1) ~ (3) steps until the best circle is found.

Get Circle

Five, the source code of this article

Click here to download the source code for this article.

Random sampling consistency Algorithm (RANSAC) example and source code--Reprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.