5

MATLABからPythonに切り替えようとしていますが、自分では解決できない問題がいくつかあります。Qtデザイナーで設計されたpyqtでGUIを実行し(一部のニューロンを分析するため)、すべての視覚化はQtのmatplotlibウィジェット(pythonxyに含まれています)で行われますが、インタラクティブな選択のためにMATLABのようないくつかのツールが必要です( Qt GUIに統合されたmatplotlibで動作する画像だけでなくプロット上でも):

  • インライン
  • インポリ
  • imellipse
  • インフリーハンド
  • imrect( Pythonのpyqt GUI imrectでは機能しません);
  • 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に戻ります。

助けてくれてありがとう

4

3 に答える 3

4

OK、Qt GUIに統合されたmatplotlibのimlineを自分で実装しました...今では、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
于 2013-02-28T11:17:34.833 に答える
0

インタラクティブツールの場合は、ipythonノートブックまたは他のipythonアプリケーションを確認することをお勧めします。

ipython qtコンソール

http://ipython.org/ipython-doc/dev/interactive/qtconsole.html

ipythonノートブック

http://ipython.org/notebook.html

于 2013-02-25T01:15:57.510 に答える
0

matplotlibドキュメントには、PyQt5で機能する簡単な実装があります(便宜上、そこから例全体をコピーします)

from matplotlib import pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs = list(line.get_xdata())
        self.ys = list(line.get_ydata())
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        print('click', event)
        if event.inaxes!=self.line.axes: return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])  # empty line
linebuilder = LineBuilder(line)

plt.show()
于 2018-07-08T13:52:57.647 に答える