Showing posts with label RANSAC. Show all posts
Showing posts with label RANSAC. Show all posts

Monday, December 19, 2011

Robust Nonlinear Fitting by RANSAC (Outlier Tolerent Nonlinear Regression)

Robust Nonlinear Optimization MATLAB Code Download [File->Download]

Please have a look in RANSAC_4_Nonlinear.m This code is not heavily tested. Therefore, please be warned! 

All credit goes to the RANSAC toolbox creator! I just tested it for a simple nonlinear function. 

Contents

%
% Objective: RANSAC for Robust Nonlinear Model Estimation
% Requirements: Optimization Toolbox (I am sorry, but its possible to modify the code to remove this dependency!)
% Important Acknowledgement: This RANSAC code is based on the RANSAC Toolbox [1][2]
% Thanks to the author for sharing but offcourse I do not take any
% responsibility neither for my or anyone else code! :-)
% --------------------------------------------------
% ACKNOWLEGEMENT:
%---------------------------------------------------
% [1] http://www.mathworks.com/matlabcentral/linkexchange/links/1814-ransac-toolbox
% [2] http://vision.ece.ucsb.edu/~zuliani/Research/RANSAC/RANSAC.shtml

%---------------------------------------------------

%---------------------------------------------------
% Input:
%---------------------------------------------------
% This simple script will try to fit the following nonlinear model
% Y=offset+gain*(X.^gamma); The equation is formally defined in the
% function titled "mygogcurve"
% Here, X is the independent variable and saved in the first row of matrix "xdataydata"
% And Y is the dependent variable and saved in the second row of matrix
% titled "xdataydata". Our Objective is to find the 3 parameters (offset,gain and gamma respectively)

% User defined functions

% estimate_gog_line: This function is used by main RANSAC.m in two ways.
% First, its used to compute k, where k means minimum number of data
% required to estimate the model. Here, I understand that we need at least 3
% points to estimate my nonlinear model. But to capture the nonlinearity, I have forcefully demands at
% least 5% data to start the model. See the function code to see the detail.

% residual_error_gog_line: this function returns squared error(E) and degrees of freedom(d for dof)
% Since, we are searching for 3 parameters dof will be k-3. Squared error
% is computed by fit=Theta(1)*(X(1,:).^Theta(2))+Theta(3); E=(fit-(X(2,:))).^2;

%---------------------------------------------------
% Output:
%---------------------------------------------------
% Estimated parameters in matrix "parameter_hat" (offset,gain and gamma respectively)
% --------------------------------------------------
% SEE ALSO:
%---------------------------------------------------

% Nonlinear model part is written by:
% SHEIKH FARIDUL Hasan
% hasan.sheikh.faridul@univ-st-etienne.fr
% Hasan.Sheikh-Faridul@technicolor.com
% https://sites.google.com/site/infofaridulhasan/

Clearing and loading fresh data

close all
clear all
% clc
tic

load RGB_im2_im2

xdata=RGB_ref(:,1)'; % You may test xdata=RGB_ref(:,2)'; with ydata=RGB_test(:,2)'; OR xdata=RGB_ref(:,3)'; with ydata=RGB_test(:,3)';
ydata=RGB_test(:,1)';
xdataydata=[xdata; ydata];

setting RANSAC options

options.epsilon = 1e-6;
options.P_inlier = 0.95;
options.sigma = 1;
options.est_fun = @estimate_gog_line;
options.man_fun = @residual_error_gog_line;
options.mode = 'MSAC';
options.Ps = [];
options.notify_iters = [];
options.min_iters = 50;
options.fix_seed = false;
options.reestimate = true;
options.stabilize = false;
options.max_iters=100;

Running RANSAC

[results, options] = RANSAC(xdataydata, options);
Starting RANSAC
Minimal sample set dimension = 16
Squared noise threshold = 22.708231, (assuming Gaussian noise, for sigma = 1.000000)
Iteration =     1/       37. Inliers =    313/   336 (rank is r = -3.65524372)
Iteration =     2/       16. Inliers =    325/   336 (rank is r = -1.64444872)
Iteration =    10/       16. Inliers =    325/   336 (rank is r = -1.56384809)
Iteration =    24/       16. Inliers =    325/   336 (rank is r = -1.56014290)
Iteration =    28/       16. Inliers =    325/   336 (rank is r = -1.54867447)
Iteration =    45/       16. Inliers =    325/   336 (rank is r = -1.54532717)
Restimating the parameter vector... Done
Final number of inliers = 325/336
Converged in 51 iterations (0.898319 seconds)

GOG Parameter estimation from Inliers using levenberg-marquardt algorithm

ind = results.CS;

options_myfit=optimset('Algorithm',{'levenberg-marquardt',.005});
[parameter_hat,resnorm,residual,exitflag,output,lambda,jacobian] = lsqnonlin(@mygogcurve,[0;1;1],[],[],options_myfit,xdataydata(1, ind),xdataydata(2, ind));

toc
Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.



Elapsed time is 1.054063 seconds.

Results Visualization

figure;
hold on
plot(xdataydata(1, ind),xdataydata(2, ind), '.g')
plot(xdataydata(1, ~ind), xdataydata(2, ~ind), '.r')
plot([1:255],parameter_hat(1)+parameter_hat(2)*([1:255].^parameter_hat(3)),'b')
legend('Inliers', 'Outliers','LM-fit from inliers')

xlabel('x-reference color')
ylabel('y-test color')
title('RANSAC outlier detection and GOG Param by LM estimation')

Conclusion: Will be completed later