|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "../precomp.hpp"
|
|
|
#include "../usac.hpp"
|
|
|
|
|
|
namespace cv { namespace usac {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UniformSamplerImpl : public UniformSampler {
|
|
|
private:
|
|
|
std::vector<int> points_random_pool;
|
|
|
int sample_size, points_size = 0;
|
|
|
RNG rng;
|
|
|
public:
|
|
|
|
|
|
UniformSamplerImpl (int state, int sample_size_, int points_size_)
|
|
|
: rng(state)
|
|
|
{
|
|
|
sample_size = sample_size_;
|
|
|
setPointsSize (points_size_);
|
|
|
}
|
|
|
void setNewPointsSize (int points_size_) override {
|
|
|
setPointsSize(points_size_);
|
|
|
}
|
|
|
void generateSample (std::vector<int> &sample) override {
|
|
|
int random_pool_size = points_size;
|
|
|
for (int i = 0; i < sample_size; i++) {
|
|
|
|
|
|
const int array_random_index = rng.uniform(0, random_pool_size);
|
|
|
|
|
|
|
|
|
sample[i] = points_random_pool[array_random_index];
|
|
|
|
|
|
std::swap(points_random_pool[array_random_index],
|
|
|
points_random_pool[--random_pool_size]);
|
|
|
}
|
|
|
}
|
|
|
private:
|
|
|
void setPointsSize (int points_size_) {
|
|
|
CV_Assert (sample_size <= points_size_);
|
|
|
|
|
|
if (points_size_ > points_size)
|
|
|
points_random_pool = std::vector<int>(points_size_);
|
|
|
|
|
|
if (points_size != points_size_) {
|
|
|
points_size = points_size_;
|
|
|
|
|
|
for (int i = 0; i < points_size; i++)
|
|
|
points_random_pool[i] = i;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
Ptr<UniformSampler> UniformSampler::create(int state, int sample_size_, int points_size_) {
|
|
|
return makePtr<UniformSamplerImpl>(state, sample_size_, points_size_);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProsacSimpleSamplerImpl : public ProsacSimpleSampler {
|
|
|
protected:
|
|
|
int points_size, subset_size, t_n_prime, kth_sample_number,
|
|
|
max_prosac_samples_count, largest_sample_size, sample_size;
|
|
|
double t_n;
|
|
|
Ptr<UniformRandomGenerator> random_gen;
|
|
|
public:
|
|
|
ProsacSimpleSamplerImpl (int state, int points_size_, int sample_size_,
|
|
|
int max_prosac_samples_count_) : random_gen(UniformRandomGenerator::create(state)) {
|
|
|
|
|
|
CV_Assert(sample_size_ <= points_size_);
|
|
|
sample_size = sample_size_;
|
|
|
points_size = points_size_;
|
|
|
max_prosac_samples_count = max_prosac_samples_count_;
|
|
|
initialize ();
|
|
|
}
|
|
|
|
|
|
void generateSample (std::vector<int> &sample) override {
|
|
|
if (kth_sample_number > max_prosac_samples_count) {
|
|
|
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size, points_size);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
kth_sample_number++;
|
|
|
|
|
|
|
|
|
if (kth_sample_number >= t_n_prime && subset_size < largest_sample_size) {
|
|
|
|
|
|
double t_n_plus1 = (subset_size + 1) * t_n / (subset_size + 1 - sample_size);
|
|
|
t_n_prime += static_cast<int>(ceil(t_n_plus1 - t_n));
|
|
|
t_n = t_n_plus1;
|
|
|
subset_size++;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (t_n_prime < kth_sample_number) {
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size, subset_size);
|
|
|
} else {
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size-1, subset_size-1);
|
|
|
sample[sample_size-1] = subset_size-1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void setSampleNumber (int k) {
|
|
|
kth_sample_number = k;
|
|
|
|
|
|
|
|
|
if (kth_sample_number > max_prosac_samples_count)
|
|
|
return;
|
|
|
else {
|
|
|
t_n = max_prosac_samples_count;
|
|
|
t_n_prime = 1;
|
|
|
subset_size = sample_size;
|
|
|
for (int i = 0; i < sample_size; i++)
|
|
|
t_n *= static_cast<double>(subset_size - i) / (points_size - i);
|
|
|
|
|
|
while (kth_sample_number > t_n_prime) {
|
|
|
double t_n_plus1 = static_cast<double>(subset_size + 1) * t_n / (subset_size + 1 - sample_size);
|
|
|
t_n_prime += static_cast<int>(ceil(t_n_plus1 - t_n));
|
|
|
t_n = t_n_plus1;
|
|
|
subset_size++;
|
|
|
}
|
|
|
if (subset_size > points_size)
|
|
|
subset_size = points_size;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void setNewPointsSize (int points_size_) override {
|
|
|
CV_Assert(sample_size <= points_size_);
|
|
|
points_size = points_size_;
|
|
|
initialize ();
|
|
|
}
|
|
|
private:
|
|
|
void initialize () {
|
|
|
largest_sample_size = points_size;
|
|
|
subset_size = sample_size;
|
|
|
t_n = max_prosac_samples_count;
|
|
|
t_n_prime = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sample_size; i++)
|
|
|
t_n *= static_cast<double>(subset_size - i) / (points_size - i);
|
|
|
|
|
|
kth_sample_number = 0;
|
|
|
}
|
|
|
};
|
|
|
Ptr<ProsacSimpleSampler> ProsacSimpleSampler::create(int state, int points_size_, int sample_size_,
|
|
|
int max_prosac_samples_count_) {
|
|
|
return makePtr<ProsacSimpleSamplerImpl>(state, points_size_, sample_size_,
|
|
|
max_prosac_samples_count_);
|
|
|
}
|
|
|
|
|
|
|
|
|
class ProsacSamplerImpl : public ProsacSampler {
|
|
|
protected:
|
|
|
std::vector<int> growth_function;
|
|
|
|
|
|
|
|
|
|
|
|
int points_size, sample_size, subset_size, termination_length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int growth_max_samples;
|
|
|
|
|
|
|
|
|
int kth_sample_number;
|
|
|
Ptr<UniformRandomGenerator> random_gen;
|
|
|
public:
|
|
|
void setTerminationLength (int termination_length_) override {
|
|
|
termination_length = termination_length_;
|
|
|
}
|
|
|
|
|
|
|
|
|
int getKthSample () const override {
|
|
|
return kth_sample_number;
|
|
|
}
|
|
|
|
|
|
|
|
|
const std::vector<int> & getGrowthFunction () const override {
|
|
|
return growth_function;
|
|
|
}
|
|
|
|
|
|
ProsacSamplerImpl (int state, int points_size_, int sample_size_,
|
|
|
int growth_max_samples_) : random_gen(UniformRandomGenerator::create(state)) {
|
|
|
CV_Assert(sample_size_ <= points_size_);
|
|
|
|
|
|
sample_size = sample_size_;
|
|
|
points_size = points_size_;
|
|
|
|
|
|
growth_max_samples = growth_max_samples_;
|
|
|
growth_function = std::vector<int>(points_size);
|
|
|
|
|
|
kth_sample_number = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double T_n = growth_max_samples;
|
|
|
for (int i = 0; i < sample_size; i++)
|
|
|
T_n *= static_cast<double> (sample_size-i) / (points_size-i);
|
|
|
|
|
|
int T_n_prime = 1;
|
|
|
|
|
|
|
|
|
for (int n = 0; n < sample_size; n++)
|
|
|
growth_function[n] = T_n_prime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int n = sample_size; n < points_size; n++) {
|
|
|
double Tn_plus1 = static_cast<double>(n + 1) * T_n / (n + 1 - sample_size);
|
|
|
growth_function[n] = T_n_prime + (int) ceil(Tn_plus1 - T_n);
|
|
|
|
|
|
|
|
|
T_n = Tn_plus1;
|
|
|
T_n_prime = growth_function[n];
|
|
|
}
|
|
|
|
|
|
|
|
|
termination_length = points_size;
|
|
|
subset_size = sample_size;
|
|
|
kth_sample_number = 0;
|
|
|
}
|
|
|
|
|
|
void generateSample (std::vector<int> &sample) override {
|
|
|
if (kth_sample_number > growth_max_samples) {
|
|
|
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size, points_size);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
kth_sample_number++;
|
|
|
|
|
|
|
|
|
|
|
|
if (kth_sample_number >= growth_function[subset_size-1] && subset_size < termination_length)
|
|
|
subset_size++;
|
|
|
|
|
|
|
|
|
|
|
|
if (growth_function[subset_size-1] < kth_sample_number) {
|
|
|
if (subset_size >= termination_length) {
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size, subset_size);
|
|
|
} else {
|
|
|
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size-1, subset_size-1);
|
|
|
sample[sample_size-1] = subset_size-1;
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
random_gen->generateUniqueRandomSet(sample, sample_size, subset_size);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void setSampleNumber (int k) {
|
|
|
kth_sample_number = k;
|
|
|
|
|
|
|
|
|
if (kth_sample_number > growth_max_samples)
|
|
|
return;
|
|
|
else {
|
|
|
subset_size = sample_size;
|
|
|
while (kth_sample_number > growth_function[subset_size-1]) {
|
|
|
subset_size++;
|
|
|
if (subset_size >= points_size){
|
|
|
subset_size = points_size;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (termination_length < subset_size)
|
|
|
termination_length = subset_size;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void setNewPointsSize (int ) override {
|
|
|
CV_Error(cv::Error::StsError, "Changing points size in PROSAC requires to change also "
|
|
|
"termination criteria! Use PROSAC simpler version");
|
|
|
}
|
|
|
};
|
|
|
|
|
|
Ptr<ProsacSampler> ProsacSampler::create(int state, int points_size_, int sample_size_,
|
|
|
int growth_max_samples_) {
|
|
|
return makePtr<ProsacSamplerImpl>(state, points_size_, sample_size_, growth_max_samples_);
|
|
|
}
|
|
|
|
|
|
|
|
|
class ProgressiveNapsacImpl : public ProgressiveNapsac {
|
|
|
private:
|
|
|
int max_progressive_napsac_iterations, points_size;
|
|
|
|
|
|
int kth_sample_number, grid_layers_number, sample_size, sampler_length;
|
|
|
|
|
|
const Ptr<UniformRandomGenerator> random_generator;
|
|
|
ProsacSamplerImpl one_point_prosac, prosac_sampler;
|
|
|
|
|
|
|
|
|
const std::vector<Ptr<NeighborhoodGraph>> * layers;
|
|
|
|
|
|
std::vector<int> growth_function;
|
|
|
std::vector<int> hits_per_point;
|
|
|
std::vector<int> subset_size_per_point;
|
|
|
std::vector<int> current_layer_per_point;
|
|
|
public:
|
|
|
|
|
|
|
|
|
ProgressiveNapsacImpl (int state,int points_size_, int sample_size_,
|
|
|
const std::vector<Ptr<NeighborhoodGraph>> &layers_, int sampler_length_) :
|
|
|
|
|
|
random_generator (UniformRandomGenerator::create(state)),
|
|
|
one_point_prosac (random_generator->getRandomNumber(INT_MAX), points_size_,
|
|
|
1 ,points_size_),
|
|
|
prosac_sampler (random_generator->getRandomNumber(INT_MAX), points_size_,
|
|
|
sample_size_, 200000), layers(&layers_) {
|
|
|
CV_Assert(sample_size_ <= points_size_);
|
|
|
sample_size = sample_size_;
|
|
|
points_size = points_size_;
|
|
|
sampler_length = sampler_length_;
|
|
|
grid_layers_number = static_cast<int>(layers_.size());
|
|
|
|
|
|
|
|
|
growth_function = std::vector<int>(points_size);
|
|
|
|
|
|
|
|
|
max_progressive_napsac_iterations = sampler_length * points_size;
|
|
|
|
|
|
const int local_sample_size = sample_size - 1;
|
|
|
double T_n = max_progressive_napsac_iterations;
|
|
|
for (int i = 0; i < local_sample_size; i++)
|
|
|
T_n *= static_cast<double> (local_sample_size - i) / (points_size - i);
|
|
|
|
|
|
|
|
|
int T_n_prime = 1;
|
|
|
for (int n = 0; n < points_size; n++) {
|
|
|
if (n + 1 <= local_sample_size) {
|
|
|
growth_function[n] = T_n_prime;
|
|
|
continue;
|
|
|
}
|
|
|
double Tn_plus1 = (n+1) * T_n / (n + 1 - local_sample_size);
|
|
|
growth_function[n] = T_n_prime + static_cast<int>(ceil(Tn_plus1 - T_n));
|
|
|
T_n = Tn_plus1;
|
|
|
T_n_prime = growth_function[n];
|
|
|
}
|
|
|
|
|
|
subset_size_per_point = std::vector<int>(points_size, sample_size);
|
|
|
hits_per_point = std::vector<int>(points_size, 0);
|
|
|
current_layer_per_point = std::vector<int>(points_size, 0);
|
|
|
|
|
|
kth_sample_number = 0;
|
|
|
}
|
|
|
|
|
|
void generateSample (std::vector<int> &sample) override {
|
|
|
|
|
|
|
|
|
if (kth_sample_number > max_progressive_napsac_iterations) {
|
|
|
prosac_sampler.generateSample(sample);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
kth_sample_number++;
|
|
|
|
|
|
|
|
|
one_point_prosac.generateSample(sample);
|
|
|
const int initial_point = sample[0];
|
|
|
|
|
|
|
|
|
|
|
|
int &iters_of_init_pt = ++hits_per_point[initial_point];
|
|
|
int &subset_size_of_init_pt = subset_size_per_point[initial_point];
|
|
|
|
|
|
while (iters_of_init_pt > growth_function[subset_size_of_init_pt - 1] && subset_size_of_init_pt < points_size)
|
|
|
subset_size_of_init_pt++;
|
|
|
|
|
|
|
|
|
int ¤t_layer = current_layer_per_point[initial_point];
|
|
|
|
|
|
bool is_last_layer = false;
|
|
|
do {
|
|
|
|
|
|
|
|
|
if (current_layer >= grid_layers_number) {
|
|
|
is_last_layer = true;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((int)layers->at(current_layer)->getNeighbors(initial_point).size() < subset_size_of_init_pt) {
|
|
|
++current_layer;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
|
|
|
break;
|
|
|
} while (true);
|
|
|
|
|
|
|
|
|
if (!is_last_layer) {
|
|
|
|
|
|
|
|
|
const std::vector<int> &neighbors = layers->at(current_layer)->getNeighbors(initial_point);
|
|
|
|
|
|
|
|
|
|
|
|
sample[sample_size - 1] = initial_point;
|
|
|
|
|
|
|
|
|
|
|
|
sample[sample_size - 2] = neighbors[subset_size_of_init_pt - 1];
|
|
|
|
|
|
|
|
|
random_generator->generateUniqueRandomSet(sample, sample_size - 2, subset_size_of_init_pt - 1);
|
|
|
|
|
|
for (int i = 0; i < sample_size - 2; i++) {
|
|
|
sample[i] = neighbors[sample[i]];
|
|
|
++hits_per_point[sample[i]];
|
|
|
}
|
|
|
++hits_per_point[sample[sample_size - 2]];
|
|
|
}
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
prosac_sampler.setSampleNumber(kth_sample_number);
|
|
|
prosac_sampler.generateSample (sample);
|
|
|
sample[sample_size - 1] = initial_point;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void setNewPointsSize (int ) override {
|
|
|
CV_Error(cv::Error::StsError, "Changing points size requires changing neighborhood graph! "
|
|
|
"You must reinitialize P-NAPSAC!");
|
|
|
}
|
|
|
};
|
|
|
Ptr<ProgressiveNapsac> ProgressiveNapsac::create(int state, int points_size_, int sample_size_,
|
|
|
const std::vector<Ptr<NeighborhoodGraph>> &layers, int sampler_length_) {
|
|
|
return makePtr<ProgressiveNapsacImpl>(state, points_size_, sample_size_,
|
|
|
layers, sampler_length_);
|
|
|
}
|
|
|
|
|
|
|
|
|
class NapsacSamplerImpl : public NapsacSampler {
|
|
|
private:
|
|
|
const Ptr<NeighborhoodGraph> neighborhood_graph;
|
|
|
const Ptr<UniformRandomGenerator> random_generator;
|
|
|
bool do_uniform = false;
|
|
|
std::vector<int> points_large_neighborhood;
|
|
|
int points_large_neighborhood_size, points_size, sample_size;
|
|
|
public:
|
|
|
|
|
|
NapsacSamplerImpl (int state, int points_size_, int sample_size_,
|
|
|
const Ptr<NeighborhoodGraph> &neighborhood_graph_) :
|
|
|
neighborhood_graph (neighborhood_graph_),
|
|
|
random_generator(UniformRandomGenerator::create(state, points_size_, sample_size_)) {
|
|
|
|
|
|
CV_Assert(points_size_ >= sample_size_);
|
|
|
|
|
|
points_size = points_size_;
|
|
|
sample_size = sample_size_;
|
|
|
points_large_neighborhood = std::vector<int>(points_size);
|
|
|
|
|
|
points_large_neighborhood_size = 0;
|
|
|
|
|
|
|
|
|
for (int pt_idx = 0; pt_idx < points_size; pt_idx++)
|
|
|
if ((int)neighborhood_graph->getNeighbors(pt_idx).size() >= sample_size-1)
|
|
|
points_large_neighborhood[points_large_neighborhood_size++] = pt_idx;
|
|
|
|
|
|
|
|
|
if (points_large_neighborhood_size == 0)
|
|
|
do_uniform = true;
|
|
|
|
|
|
|
|
|
random_generator->setSubsetSize(sample_size-1);
|
|
|
}
|
|
|
|
|
|
void generateSample (std::vector<int> &sample) override {
|
|
|
if (do_uniform)
|
|
|
|
|
|
random_generator->generateUniqueRandomSet(sample, points_size);
|
|
|
else {
|
|
|
|
|
|
int initial_point = points_large_neighborhood
|
|
|
[random_generator->getRandomNumber(points_large_neighborhood_size)];
|
|
|
|
|
|
const std::vector<int> &neighbors = neighborhood_graph->getNeighbors(initial_point);
|
|
|
|
|
|
|
|
|
random_generator->generateUniqueRandomSet(sample, (int)neighbors.size());
|
|
|
for (int i = 0; i < sample_size-1; i++)
|
|
|
sample[i] = neighbors[sample[i]];
|
|
|
|
|
|
|
|
|
sample[sample_size-1] = initial_point;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void setNewPointsSize (int ) override {
|
|
|
CV_Error(cv::Error::StsError, "Changing points size requires changing neighborhood graph!"
|
|
|
" You must reinitialize NAPSAC!");
|
|
|
}
|
|
|
};
|
|
|
Ptr<NapsacSampler> NapsacSampler::create(int state, int points_size_, int sample_size_,
|
|
|
const Ptr<NeighborhoodGraph> &neighborhood_graph_) {
|
|
|
return makePtr<NapsacSamplerImpl>(state, points_size_, sample_size_, neighborhood_graph_);
|
|
|
}
|
|
|
}}
|
|
|
|