背景:要判断跟踪算法在控制目标物走直线的情况下跟踪的轨迹是否为直线,我保存下来跟踪算法跟踪到的目标的中心点在图像上的像素位置,然后拟合出穿过这些点的直线,然后计算这些点距离直线的平均距离来判断跟踪的精度。(这个是不是要考虑,虽然我们控制让目标物走直线,但是这种控制精度高吗,或者说,他自己走着走着,轨迹会发生偏移,这需要有一个参照物,比如让扫地机沿着地板砖缝隙走)
主要是使用python读取csv数据
def readfile(filename):with open(filename, 'r') as f: reader = csv.reader(f.readlines()[1:])for line in reader:try:x = eval(line[1])y = eval(line[2])x_list.append(x)y_list.append(y)except:pass
x_list = []
y_list = []def func(a, b, x):return a*x + b
def linefitting():xs = np.array(x_list)ys = np.array(y_list)para = np.polyfit(xs, ys, deg = 1)print("拟合的直线方程 y=",np.poly1d(para) )y_fitted = func(para[0],para[1], xs)plt.figureplt.plot(xs, ys, 'ro', label='Original curve')plt.plot(xs, y_fitted, '-b', label = 'Fitted curve')plt.savefig('./trajectory.png')plt.legend()plt.show()return para
def calprecision(para):
# 计算所有的点到直线的距离
# y = a*x + b
# a*x - y + b = 0
# (a/b)*x - (1/b)*y + 1 = 0
# 方案一
# a = para[0]/para[1]
# b = -1/para[1]
# c = 1
# 方案二a = para[0]b = -1c = para[1]print("a:", a)print("b:", b)print("c:", c)
# ax+by+c = 0sum = 0down_num = cmath.sqrt(a**2 + b**2)
# print("down_num:", down_num)for i in range(len(x_list)):x = x_list[i]y = y_list[i]dis = abs(a*x + b*y + c)/down_numsum += disprint("总距离",sum)average = sum/len(x_list)print("平均像素距离:",average)