python中Matplotlib库显示界面中进行鼠标交互绘制操作

来源:互联网 发布:php技术文档模板 编辑:程序博客网 时间:2024/06/05 10:47


近期在用Python做医学图像处理系统制作,逐步跟大家分享我遇到的问题以及解决方案。今天来跟大家说一下在matplotlib库显示界面中进行鼠标交互绘制等操作。

先上图,下图为用matplotlib进行显示的脊柱图像(.dicom格式),后期跟大家介绍.dicom文件的读取以及相应图像处理方式。

本文要做的就是在此界面用鼠标进行交互绘制操作。

#鼠标事件def OnClick(event):    global Coords1x,Coords1y    global Coords3x,Coords3y    #ax = plt.gca()    if event.button ==1:        Coords1x = event.xdata        Coords1y = event.ydata    if event.button == 3:        Coords3x = event.xdata        Coords3y = event.ydatadef OnMouseMotion(event):    global Coords2x,Coords2y,x1,y1    if event.button == 1:        Coords2x = event.xdata        Coords2y = event.ydata        x1 = [Coords1x,Coords2x]        y1 = [Coords1y,Coords2y]        ax = plt.gca()        lines = ax.plot(x1,y1,picker = 20)        ax.figure.canvas.draw()        #删除之前的线条,进行更新        l = lines.pop(0)        l.remove()        del l    elif event.button == 3:        Coords4x = event.xdata        Coords4y = event.ydata        x2 = [Coords3x,Coords4x]        y2 = [Coords3y,Coords4y]        ax1 = plt.gca()        #lines = ax1.plot(x1,y1,picker = 5)        lines1 = ax1.plot(x2,y2,picker = 20)        ax1.figure.canvas.draw()        #删除之前的线条,进行更新        l = lines1.pop(0)        l.remove()        del l
                           ##关联鼠标点击事件                           fig.canvas.mpl_connect('button_press_event',OnClick)                           fig.canvas.mpl_connect('motion_notify_event',OnMouseMotion)
完成绘制操作

原创粉丝点击