类似于 MATLAB 中的 Python 交互式选择工具

标签 python qt matlab matplotlib pyqt

我正在尝试从 MATLAB 切换到 python,但现在我遇到了一些我自己无法解决的问题。我在用 Qt 设计器设计的 pyqt 中做了一个 GUI(分析一些神经元),所有的可视化都是在 Qt 的 matplotlib 小部件中完成的(它包含在 pythonxy 中)但现在我需要一些工具,比如在 MATLAB 中进行交互式选择(不仅在图像但也在绘图上)与集成在 Qt GUI 中的 matplotlib 一起工作:

  • 内联
  • 实现
  • 椭圆
  • 随心所欲
  • imrect(在 pyqt GUI 中不起作用 imrect for python );
  • ginput(我在 matplotlib 库的 matplotlib\blocking_input.py 文件中评论了命令 self.fig.show() 后,我可以直接在 myMatplotlibWidget.figure.ginput() 上调用 ginput)。

我找到了这个 http://matplotlib.org/users/event_handling.html请不要告诉我必须自己用这个 python 模块实现上述工具 xD

我找到了这个 http://www.pyqtgraph.org/但它没有与 matplotlib 集成,最终渲染效果不如 matplotlib。

pyqt有没有好的交互选择工具?在谷歌上,我找不到任何有用的东西,但我不敢相信没有好的 python 交互工具......如果是这样,我会切换回 MATLAB。

感谢您的帮助

最佳答案

OK,我已经自己实现了imline for matplotlib in the Qt GUI...现在,imrect等实现起来很简单。如果有人需要 imrect 等,我会更新代码。下面是我的 imline 代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import time
import matplotlib as mpl
import matplotlib.pyplot as plt

import numpy as np
import scipy.optimize as opt


class Imline(QObject):
    '''
    Plot interactive line
    '''

    def __init__(self, plt, image = None, scale = 1, *args, **kwargs):
        '''
        Initialize imline
        '''
        super(Imline, self).__init__(None)

        # set plot        
        self.__plt = plt
        self.scale = scale        

        # initialize start and end points        
        self.startX = None
        self.startY = None
        self.endX = None
        self.endY = None  

        # initialize line2d        
        self.__line2d = None
        self.mask = None

        # store information to generate mask
        if(image is not None):        
            height, width = image.shape

        else:
            height = None
            width = None            

        self.__width = width
        self.__height = height

        # set signals and slots        
        self.__c1 = self.__plt.figure.canvas.mpl_connect('button_press_event', self.__mousePressEvent)
        self.__c2 = self.__plt.figure.canvas.mpl_connect('motion_notify_event', self.__mouseMoveEvent)
        self.__c3 = self.__plt.figure.canvas.mpl_connect('button_release_event', self.__mouseReleaseEvent)       

        self.imlineEventFinished = SIGNAL('imlineEventFinished')        


    def __mousePressEvent(self, event):
        '''
        Starting point
        '''

        # get xy data        
        xdata = event.xdata
        ydata = event.ydata

        # check if mouse is outside the figure        
        if((xdata is None) | (ydata is None) | (self.startX is not None) | (self.startY is not None) | (self.endX is not None) | (self.endY is not None)):
            return       

        # start point        
        self.startX = xdata
        self.startY = ydata


    def __mouseMoveEvent(self, event):
        '''
        Draw interactive line
        '''

        # get xy data        
        xdata = event.xdata
        ydata = event.ydata

        # check if mouse is outside the figure        
        if((xdata is None) | (ydata is None) | (self.startX is None) | (self.startY is None) | (self.endX is not None) | (self.endY is not None)):
            return      

        # remove line        
        if(self.__line2d is not None):
            self.__line2d[0].remove()

        # set x, t
        x = [self.startX, xdata]
        y = [self.startY, ydata]        

        # plot line
        self.__plt.axes.hold(True)
        xlim = self.__plt.axes.get_xlim()
        ylim = self.__plt.axes.get_ylim()
        self.__line2d = self.__plt.axes.plot(x, y, color = [1, 0, 0])
        self.__plt.axes.set_xlim(xlim)
        self.__plt.axes.set_ylim(ylim)

        # update plot        
        self.__plt.draw()
        self.__plt.show()


    def __mouseReleaseEvent(self, event):
        '''
        End point
        '''     

        # get xy data        
        xdata = event.xdata
        ydata = event.ydata

        # check if mouse is outside the figure        
        if((xdata is None) | (ydata is None) | (self.endX is not None) | (self.endY is not None)):
            return             

        # remove line        
        if(self.__line2d is not None):
            self.__line2d[0].remove()

        self.endX = xdata
        self.endY = ydata   

        P = np.polyfit([self.startX, self.endX], [self.startY, self.endY],1 )
        self.__m = P[0]
        self.__q = P[1]

        # update plot        
        self.__plt.draw()
        self.__plt.show()

        # disconnect the vents        
        self.__plt.figure.canvas.mpl_disconnect(self.__c1)
        self.__plt.figure.canvas.mpl_disconnect(self.__c2)
        self.__plt.figure.canvas.mpl_disconnect(self.__c3)

        # emit SIGNAL        
        self.emit(SIGNAL('imlineEventFinished'))


    def createMask(self):
        '''
        Create mask from painted line
        '''

        # check height width        
        if((self.__height is None) | (self.__width is None)):
            return None

        # initialize mask        
        mask = np.zeros((self.__height, self.__width))        

        # get m q        
        m = self.__m
        q = self.__q        

        print m, q

        # get points        
        startX = np.int(self.startX)   
        startY = np.int(self.startY) 
        endX = np.int(self.endX) 
        endY = np.int(self.endY)

        # ensure startX < endX
        tempStartX = startX
        if(startX > endX):
            startX = endX
            endX = tempStartX

        # ensure startY < endY
        tempStartY = startY
        if(startY > endY):
            startY = endY
            endY = tempStartY

        # save points
        self.startX = startX
        self.endX = endX
        self.startY = startY
        self.endY = endY

        # intialize data        
        xData = np.arange(startX, endX)
        yData = np.arange(startY, endY)

        # scan on x        
        for x in xData:
            row = round(m*x + q)
            if(row < startY):
                row = startY
            if(row > endY):
                row = endY
            mask[row, x] = 1

        # scan on y
        for y in yData:
            col = round((y - q) / m)
            if(col < startX):
                col = startX
            if(col > endX):
                col = endX
            mask[y, col] = 1

        # get boolean mask        
        mask = mask == 1        

        # return boolean mask
        return mask

关于类似于 MATLAB 中的 Python 交互式选择工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058621/

相关文章:

matlab - 设置 writematrix 的回车类型

algorithm - 在 MATLAB 中查找具有公共(public)重叠区域的多个圆

python - 如何使函数返回第一个字符串中出现的第二个字符串中的字符的索引列表?

ruby - QtRuby GridLayout- 未定义的方法 'addWidget'

c++ - Qt检查文件签名有效性

MATLAB 数据解析优化

python - 将 Selenium IDE 脚本导入为 unittest.TestCase 并动态修改

python - 如何使用 Python 字典?

python (django) hashlib vs Nodejs 加密

c++ - 使用临时数据表修复内存泄漏(堆与堆栈)