% OPTIMALRANSAC - Robustly fits a model to data with the Optimal RANSAC algorithm % % Usage: % % [M, sinliers] = optimalRansac(x, fittingfn, distfn, s, t, acc, ... % maxDataTrials, MaxTrials, maxit, nrtrials, low, ner) % % % Arguments: % x - Data sets to which we are seeking to fit a model M % It is assumed that x is of size [d x Npts] % where d is the dimensionality of the data and Npts is % the number of data points. % % fittingfn - Handle to a function that fits a model to s % data from x. It is assumed that the function is of the % form: % M = fittingfn(x) % Note it is possible that the fitting function can return % multiple models (for example up to 3 fundamental matrices % can be fitted to 7 matched points). In this case it is % assumed that the fitting function returns a cell array of % models. % If this function cannot fit a model it should return M as % an empty matrix. % % distfn - Handle to a function that evaluates the % distances from the model to data x. % It is assumed that the function is of the form: % [inliers, M] = distfn(M, x, t) % This function must evaluate the distances between points % and the model returning the indices of elements in x that % are inliers, that is, the points that are within distance % 't' of the model. Additionally, if M is a cell array of % possible models 'distfn' will return the model that has the % most inliers. If there is only one model this function % must still copy the model to the output. After this call M % will be a non-cell object representing only one model. % % s - The minimum number of samples from x required by % fittingfn to fit a model. % % t - The distance threshold between a data point and the model % used to decide whether the point is an inlier or not. % Is used in all sampling, resampling and rescoring. % acc - The same as above but is used to prune the set. If acclow [M, inliers, ninliers]= resample(x, M, s, t, inliers, ninliers, fittingfn, distfn, nrtrials, maxit, low); % Clean the set up! if acclow && maxinl==ninliers if sum(inliers==sinliers)==maxinl % Yes we got the same set twice nes=nes+1; else % Not the same set nes=0; sinliers=inliers; end elseif ninliers>maxinl nes=0; maxinl=ninliers; sinliers=inliers; elseif ninliers==maxinl-1 nes=0; maxinl=ninliers; sinliers=inliers; end end end