您的位置:首页 > 编程语言 > MATLAB

Python and Matlab绘制曲线图

2016-03-26 17:15 781 查看
前两天老师让标定一下视频中跟踪目标的真实位置(详见OpenCV记录鼠标左键点击位置),并与跟踪位置做一下对比。就是画一个误差图。刚开始用matlab画了一下,之前一直想学python,所以就用python又画一下。

下面是两者的效果:

MATLAB



python



很简单,下面直接上代码吧。

—————————————-MATLAB——————————————-

%本文件用于计算图像追踪中真实点与跟踪点之间的误差
realdata = xlsread('realpoint.xlsx');%读取excel中的数据
trackingdata = xlsread('trackingpoint.xlsx');
realdatax = realdata(:, 1);
realdatay = realdata(:, 2);
trackingdatax = trackingdata(:, 1);
trackingdatay = trackingdata(:, 2);
xerror = abs(trackingdatax - realdatax);
yerror = abs(trackingdatay - realdatay);
distanterror = sqrt(xerror.^2 + yerror.^2);
figure('numbertitle','off','name','The error of x direction','color','white');
plot(xerror);title('The error of x direction');xlabel('frames');ylabel('error(pixel)');
figure('numbertitle','off','name','The error of y direction','color','white');
plot(yerror);title('The error of y direction');xlabel('frames');ylabel('error(pixel)');
figure('numbertitle','off','name','The error of relative distance','color','white');
plot(distanterror);title('The error of relative distance');xlabel('frames');ylabel('error(pixel)');


—————————————-Python——————————————-

#编程环境:Anaconda3 + pycharm5
import xlrd
import numpy as np
import pylab as pl

realdatafname = 'realpoint.xlsx'
trackingdatafname = 'trackingpoint.xlsx'
realdatabk = xlrd.open_workbook(realdatafname)  # 获取xls文件
trackingdatabk = xlrd.open_workbook(trackingdatafname)  # 获取xls文件
realdatash = realdatabk.sheet_by_name('Sheet1')  #通过名称获取bk中的sheet
trackingdatash = trackingdatabk.sheet_by_name('Sheet1')  #通过名称获取bk中的sheet

# nrows = realdatash.nrows  # 获取行数
# ncols = realdatash.ncols  # 获取列数
# print('nrows %d, ncols %d' % (nrows,ncols))
# cell_value = realdatash.cell_value(1, 1)  # 获取单元格(1,1)的值
# print(cell_value)

realdatax = np.array(realdatash.col_values(0))  # 获取第0列值的列表,并将其转换为array类型
realdatay = np.array(realdatash.col_values(1))
trackingdatax = np.array(trackingdatash.col_values(0))  # 获取第0列值的列表,并将其转换为array类型
trackingdatay = np.array(trackingdatash.col_values(1))
# print(realdatax)
errorx = np.absolute(trackingdatax - realdatax)
errory = np.absolute(trackingdatay - realdatay)
errordistant = np.sqrt(errorx * errorx + errory * errory)
pl.figure(1)
pl.xlabel('frames')
pl.ylabel('xerror(pixel)')
pl.title('The error of x direction')
pl.plot(errorx)  # use pylab to plot x and y
pl.figure(2)
pl.xlabel('frames')
pl.ylabel('yerror(pixel)')
pl.title('The error of y direction')
pl.plot(errory)  # use pylab to plot x and y
pl.figure(3)
pl.xlabel('frames')
pl.ylabel('xerror(pixel)')
pl.title('The error of x direction')
pl.plot(errordistant)  # use pylab to plot x and y
pl.show()   # show the plot on the screen


注:程序运行需要保存于excel的数据,the whole project 详见:https://github.com/NodYoung/MATLAB-and-Python-read-excel-data-and-plot-the-line-graph
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: