import numpy as np
import matplotlib.pyplot as plt
import random
import math
SIZE = 50
X = np.linspace(0, 10, SIZE)
Y = 3 * X + 10
fig = plt.figure()
ax1 = fig.add_subplot(1,1, 1)
ax1.set_title("RANSAC")
random_x = []
random_y = []
for i in range(SIZE):
random_x.append(X[i] + random.uniform(-0.5, 0.5))
random_y.append(Y[i] + random.uniform(-0.5, 0.5))
for i in range(SIZE):
random_x.append(random.uniform(0,10))
random_y.append(random.uniform(10,40))
RANDOM_X = np.array(random_x)
RANDOM_Y = np.array(random_y)
ax1.scatter(RANDOM_X, RANDOM_Y)
ax1.set_xlabel("x")
ax1.set_ylabel("y")
iters = 100000
sigma = 0.25
best_a = 0
best_b = 0
pretotal = 0
P = 0.99
for i in range(iters):
sample_index = random.sample(range(SIZE * 2),2)
x_1 = RANDOM_X[sample_index[0]]
x_2 = RANDOM_X[sample_index[1]]
y_1 = RANDOM_Y[sample_index[0]]
y_2 = RANDOM_Y[sample_index[1]]
a = (y_2 - y_1) / (x_2 - x_1)
b = y_1 - a * x_1
total_inlier = 0
for index in range(SIZE * 2):
y_estimate = a * RANDOM_X[index] + b
if abs(y_estimate - RANDOM_Y[index]) < sigma:
total_inlier = total_inlier + 1
if total_inlier > pretotal:
iters = math.log(1 - P) / math.log(1 - pow(total_inlier / (SIZE * 2), 2))
pretotal = total_inlier
best_a = a
best_b = b
if total_inlier > SIZE:
break
Y = best_a * RANDOM_X + best_b
ax1.plot(RANDOM_X, Y)
text = "best_a = " + str(best_a) + "\nbest_b = " + str(best_b)
plt.text(5,10, text,
fontdict={
'size': 8, 'color': 'r'})
plt.show()
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 50;
int main() {
double X[SIZE];
double Y[SIZE];
for (int i = 0; i < SIZE; i++) {
X[i] = i * (10.0 / (SIZE - 1));
Y[i] = 3 * X[i] + 10;
}
srand(time(NULL));
double random_x[SIZE * 2];
double random_y[SIZE * 2];
for (int i = 0; i < SIZE; i++) {
random_x[i] = X[i] + (rand() % 11 - 5) / 10.0;
random_y[i] = Y[i] + (rand() % 11 - 5) / 10.0;
random_x[i + SIZE] = (rand() % 11) / 10.0;
random_y[i + SIZE] = (rand() % 31) / 10.0 + 1;
}
int iters = 100000;
double sigma = 0.25;
double best_a = 0;
double best_b = 0;
int pretotal = 0;
double P = 0.99;
for (int i = 0; i < iters; i++) {
int sample_index[2];
sample_index[0] = rand() % (SIZE * 2);
sample_index[1] = rand() % (SIZE * 2);
while (sample_index[1] == sample_index[0]) {
sample_index[1] = rand() % (SIZE * 2);
}
double x_1 = random_x[sample_index[0]];
double x_2 = random_x[sample_index[1]];
double y_1 = random_y[sample_index[0]];
double y_2 = random_y[sample_index[1]];
double a = (y_2 - y_1) / (x_2 - x_1);
double b = y_1 - a * x_1;
int total_inlier = 0;
for (int index = 0; index < SIZE * 2; index++) {
double y_estimate = a * random_x[index] + b;
if (abs(y_estimate - random_y[index]) < sigma) {
total_inlier++;
}
}
if (total_inlier > pretotal) {
iters = log(1 - P) / log(1 - pow(total_inlier / (SIZE * 2), 2));
pretotal = total_inlier;
best_a = a;
best_b = b;
}
if (total_inlier > SIZE) {
break;
}
}
for (int i = 0; i < SIZE; i++) {
Y[i] = best_a * X[i] + best_b;
}
printf("best_a: %f,bast_b: %f",best_a,best_b);
}