nptest1 = np.array(line1_yx)
print("nptest1", nptest1)
line1 = cv2.fitLine(nptest1, cv2.DIST_L2, 0, 0.01, 0.0)
k1 = line1[1] / line1[0]
b1 = line1[3] - k1 * line1[2]
print('第1排line1: \ny = {:0.8f}x + {:0.8f}'.format(k1[0], b1[0]))
ran_k, ran_b = fit_line_by_ransac(nptest1)
print('line1: y=%s*x+%s' %(ran_k, ran_b))
import numpy as np
import random
import math
def fit_line_by_ransac(point_list, sigma, iters = 1000, P = 0.99):
# 使用RANSAC算法拟合直线
# 迭代最大次数 iters = 1000
# 数据和模型之间可接受的差值 sigma
# 希望的得到正确模型的概率P = 0.99
# 最好模型的参数估计
best_a = 0#直线斜率
best_b = 0#直线截距
n_total = 0#内点数目
for i in range(iters):
# 随机选两个点去求解模型
sample_index = random.sample(range(len(point_list)), 2)
x_1 = point_list[sample_index[0]][0]
y_1 = point_list[sample_index[0]][1]
x_2 = point_list[sample_index[1]][0]
y_2 = point_list[sample_index[1]][1]
if x_2 == x_1:
continue
# y = ax + b 求解出a,b
a = (y_2 - y_1) / (x_2 - x_1)
b = y_1 - a * x_1
# 算出内点数目
total_inlier = 0
for index in range(len(point_list)):
y_estimate = a * point_list[index][0] + b
if abs(y_estimate - point_list[index][1]) < sigma:
total_inlier += 1
# 判断当前的模型是否比之前估算的模型好
if total_inlier > n_total:
iters = math.log(1 - P) / math.log(1 - pow(total_inlier/len(point_list), 2))
n_total = total_inlier
best_a = a
best_b = b
# 判断是否当前模型已经符合超过一半的点
if total_inlier > len(point_list)//2:
break
return best_a, best_b
if __name__ == '__main__':
#测试
points = [(1,3), (5,11), (8,18), (9,22), (10, 19), (19,37)]
a,b = fit_line_by_ransac(points, sigma=3)
print('line: y=%s*x+%s' %(a, b))