MATLAB exercise program (random sampling consistent ransac)

Source: Internet
Author: User

Ransac is used in image mosaic and sometimes in image comprehension algorithms.

Algorithm Overview (from image processing, analysis and Machine Vision (3rd):

1. suppose we want to set n data points x = {x1, X1 ,..., xn} fits into a model determined by at least m points (M <= N, for a straight line, m = 2 ). (Here I am actually generating two data with different mean and covariance Gaussian distributions)

2. Set iteration count K to 1.

3. Randomly select m items from X and fit a model. (I have selected two items for linear fitting)

4. given the deviation ε, calculate the number of residual deviations of X to the model ε. If the number of elements exceeds a threshold t, the algorithm is terminated by refitting the model based on the consistent point set (the least squares or their variants can be used. (The deviation here is 1, and the threshold is 2/3 of the Data Count)

5. Set K = k + 1. If K is smaller than a given K, skip to step 2. Otherwise, use the consistent point set model with the maximum to date, or the algorithm fails.

The running effect is as follows. The red circle shows all the data, and the Blue Cross shows the data that fits the fitting model.

The Matlab code is as follows:

Main. m

Close all; clear all; clc; % actual data mu = [0 0]; % mean S = [1 2.5; 2.5 8]; % covariance data1 = mvnrnd (MU, s, 200); % generate 200 Gaussian distribution data % noise data mu = [2 2]; S = [8 0; 0 8]; data2 = mvnrnd (MU, S, 100); % generate 100 noisy data = [data1; data2]; plot (data (:, 1), data (:, 2), 'ro '); % Show All data K = 100; % set the maximum number of iterations Sigma = 1; % set the deviation between the fitting line and the data distance pretotal = 0; % Number of data that fits the fitting model k = 1; while pretotal <size (data, 1) * 2/3 & K <K % 2/3 of the Data conforms to the fitting model or reaches the maximum number of iterations, you can exit sampindex = floor (1 + (SIZE (data, 1)-1) * rand (); % generates two random indexes, which are used to search for samples. The floor rounded down samp1 = data (sampindex (1 ),:); % sample samp2 = data (sampindex (2), :); line = mytls ([samp1; samp2]); % sample, or other variant Fitting Methods mask = ABS (line * [data ones (SIZE (data, 1), 1)] '); % calculate the distance from each data to the fitting line Total = sum (mask <sigma ); % calculate the number of data whose distance from the straight line is smaller than a certain threshold value. If total> pretotal % find the fitting line with the most data matching the fitting line. pretotal = total; bestline = line; % find the best fitting line end K = k + 1; end % Show the data mask = ABS (bestline * [data ones (SIZE (data, 1 ), 1)] ') <sigma; Hold on; for I = 1: length (mask) If mask (I) plot (data (I, 1), data (I, 2), '+'); endend

Mytls. m (two known straight points)

% Here is the equations for solving the following three equations % A * X1 + B * Y1 + c = 0% A * X2 + B * y2 + c = 0% a ^ 2 + B ^ 2 = 1% return coefficient [a B C] function line = mytls (data) X = data (1, :); y = data (2, :); k = (Y (1)-y (2)/(x (1) -X (2); % the slope of the straight line. In some cases, it must be determined individually. A = SQRT (1-1/(1 + k ^ 2) is ignored here )); B = SQRT (1-A ^ 2); if k> 0% if the slope is greater than 0, A, B is different from B =-B; end c =-A * x (1) -B * Y (1); line = [a B C]; End

For more details, see the following webpage:

1. http://en.wikipedia.org/wiki/Ransac

2. http://www.cnblogs.com/xrwang/archive/2011/03/09/ransac-1.html

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.