2017-03-11 20 views
0

特定のフォルダを常に見ているプログラムを開発し、そのフォルダに画像をコピーすると別のクラスから関数を呼び出して処理を開始します。これらの画像が巨大であるという問題は、その画像処理機能が開始されるたびに、プロセスが終了するまでuiがフリーズします。私はQThreadが解決策であるかもしれないことを学んだが、私がクラスで多くの機能を持っているときにどのように適用するのかわからない。画像処理におけるスレッド処理方法

class Main(QtGui.QMainWindow): 
    def __init__(): 

    functions = Functions() 
    ... 
    self.something.signal.connect(self.functions.doStuff) 

class Functions(QtCore.QObject): 
    def __init__(): 

    imProc = ImageProcessing() 

    def doStuff(): 
     initialImage = loadimage(...) 
     processedImage = imProc.process1(initialImage) 

class ImageProcessing(QtCore.QObject) 
    def __init__(): 

    def process1(original_image): 
     do maximum likelihood on the image 
     return segmented image 

    def process2(original_image): 

     do another image processing task 
     return segmented image 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    main = Main() 
    main.show() 
    main.exec_() 

問題は、プロセス1()のメインウィンドウを実行しているがフリーズするとき...あなたは、UIとは別にバックグラウンドでプロセス1を()を実行する方法をアドバイスしてください可能性がありますか?

ありがとうございます!

+0

スレッド内で実行したいメソッドの内容を知らなくても、スレッドセーフな方法でこれを返すことはできません。 –

+0

@three_pineapples私は、別のスレッドで最尤セグメンテーションメソッドを含むfunction1()を実行して、分割されたイメージを返したいと思います。あなたはどう思いますか?可能ですか? – Kristan

+0

@three_pineapples main1をフリーズさせないようにprocess1(画像)関数をどのように実行するのか分かりません。私はUI上に何も表示する必要はないので、基本的に私は何らかの価値を伝える必要はありません(または間違っていますか?)。この関数は、元のイメージがコピーされているフォルダに保存されたイメージを返します。最尤アルゴリズムの詳細を記述する必要はありますか?または、どんな情報が必要ですか?ありがとう – Kristan

答えて

0

これでわかりました。私はそれを解決する方法の一般的な説明を与える:

まずここに私の画像処理クラスがあるQMainWindow

class ImageProcess(QtCore.QThread): 
    def __init__(self): 
     QtCore.QThread.__init__(self) 

    def __del__(self): 
     self.exiting = True 
     self.wait() 

    def run(self): 
     #machine learning algorithm, which results a processed image... 
     self.emit(QtCore.SIGNAL('image'), processedImage) 
     return 

ザ・私は、その後

class Functions(QtCore.QObject): 
    def __init__(): 

     self.improc = ImageProcess() 
     self.connect(self.improc, SIGNAL("image"), self.processImage) 

     self.segmented_image = [] 

     self.improc.setPath(self.image) 
     self.improc.start() 

    def processImage(self,seg_img):  
     self.segmented_image = seg_img 

...私の関数のクラスを持っていると...ですいつものようにセットアップしてください...

Qthread、信号スロットアプリケーションでsrtuggelingしているsomenoneに役立つことを願って...