def __init__(self, workQueue, processedQueue, pLock): self.qt_app = QApplication(sys.argv) QWidget.__init__(self, None) # create the main ui self.pQueue = processedQueue self.wQueue = workQueue self.ui = Ui_ImageProcessor() self.ui.setupUi(self) self.imageScene = QGraphicsScene() self.original = None self.processed = None self.frameScaled = None self.imageFile = None self.pLock = pLock self.patchsize = 128 # fill combobox self.ui.boxFunction.addItem("GrayScale") self.ui.boxFunction.addItem("SobelFilter") self.ui.boxFunction.addItem("BinarizeFilter") self.ui.boxFunction.addItem("PrewittFilter") self.ui.boxFunction.addItem("RobertsFilter") self.ui.boxFunction.addItem("GaussianFilter") # connect buttons self.ui.buttonLoadImage.clicked.connect(self.loadImagePressed) self.ui.buttonResetImage.clicked.connect(self.resetImagePressed) self.ui.buttonStartProcess.clicked.connect(self.startProcess) self.ui.buttonStopProcess.clicked.connect(self.stopProcess) # start timer self.timer = QTimer() self.timer.timeout.connect(self.collectPatch) self.timer.start(10) self.sTimer = QTimer() self.sTimer.timeout.connect(self.scheduler) self.perms = None self.sceneObject = None
class ImGui(QMainWindow): def __init__(self, workQueue, processedQueue, pLock): self.qt_app = QApplication(sys.argv) QWidget.__init__(self, None) # create the main ui self.pQueue = processedQueue self.wQueue = workQueue self.ui = Ui_ImageProcessor() self.ui.setupUi(self) self.imageScene = QGraphicsScene() self.original = None self.processed = None self.frameScaled = None self.imageFile = None self.pLock = pLock self.patchsize = 128 # fill combobox self.ui.boxFunction.addItem("GrayScale") self.ui.boxFunction.addItem("SobelFilter") self.ui.boxFunction.addItem("Prewitt") self.ui.boxFunction.addItem("Binarize") # connect buttons self.ui.buttonLoadImage.clicked.connect(self.loadImagePressed) self.ui.buttonResetImage.clicked.connect(self.resetImagePressed) self.ui.buttonStartProcess.clicked.connect(self.startProcess) self.ui.buttonStopProcess.clicked.connect(self.stopProcess) # start timer self.timer = QTimer() self.timer.timeout.connect(self.collectPatch) self.timer.start(10) self.sTimer = QTimer() self.sTimer.timeout.connect(self.scheduler) self.perms = None self.sceneObject = None def loadImagePressed(self): # load the image from a file into a QImage object imageFile = QFileDialog.getOpenFileName(self, "Open file", ".", "Images (*.png *.xpm " "*.jpg)") if not imageFile: return with open(imageFile, "r") as f: try: self.imageFile = imageFile self.original = QImage(imageFile) # say that an image is loaded print "Image loaded: " + imageFile except: print "Problem with image file: " + self.imageFile self.imageFile = None finally: self.processed = self.original.convertToFormat(QImage.Format_RGB16) if self.imageFile: # find the horizontal and vertical patch numgers self.tmpPatchNum = ( self.processed.size().width() / self.patchsize, self.processed.size().height() / self.patchsize, ) self.numPatches = self.tmpPatchNum[0] * self.tmpPatchNum[1] self.updateImage() def resetImagePressed(self): # return to the original image if not self.imageFile: return self.processed = self.original self.updateImage() def updateImage(self): # update the visual of the image with the new processed image if self.processed: multiplierh = float(self.processed.size().height()) / float(self.ui.imageView.size().height()) multiplierw = float(self.processed.size().width()) / float(self.ui.imageView.size().width()) if multiplierh > multiplierw: self.frameScaled = self.processed.scaledToHeight(self.ui.imageView.size().height() - 5) else: self.frameScaled = self.processed.scaledToWidth(self.ui.imageView.size().width() - 5) if self.sceneObject: self.imageScene.removeItem(self.sceneObject) self.sceneObject = self.imageScene.addPixmap(QPixmap.fromImage(self.frameScaled)) self.imageScene.update() self.ui.imageView.setScene(self.imageScene) def serializePatch(self, x, y, offset=0): # serializes the patch and prepares the message data tempVector = [0] * (self.patchsize ** 2) rect = (x * self.patchsize, y * self.patchsize) rng = range(0, self.patchsize) for j in rng: Y = j + rect[1] for i in rng: X = i + rect[0] # the message contains 8-bit grayscale (0-255) data tempVector[j * self.patchsize + i] = rgb2gray(self.processed.pixel(X, Y)) # we should also send the reference rectangle information # where to put the patch when we receive the processed return rect, tempVector def deserializePatch(self, refPix, data): # convert the message data into the matrix and put directy on the # image using the reference pixels (refPix) counter = 0 for color in data: x = counter % self.patchsize y = counter // self.patchsize self.processed.setPixel(refPix[0] + x, refPix[1] + y, gray2rgb(color)) counter += 1 def scheduler(self): # puts the serialized patches into the work queue if self.processed: function = self.ui.boxFunction.currentText() if len(self.perms) > 0: p = self.perms.pop() x = p % self.tmpPatchNum[0] y = p // self.tmpPatchNum[0] rect, tempVector = self.serializePatch(x, y) self.wQueue.put(((function, rect), tempVector)) else: self.sTimer.stop() def collectPatch(self): # collects the processed patches from the process queue if self.pQueue.qsize() > 0: for i in range(0, self.pQueue.qsize()): # self.pLock.acquire() message = self.pQueue.get() # self.pLock.release() self.deserializePatch(message[0], message[1]) self.updateImage() def startProcess(self): # randomly organizes the patches self.perms = list(np.random.permutation(self.numPatches)) # or simply orders the patches # self.perms = range(0,self.numPatches) # self.perms.reverse() # start scheduler's timer self.sTimer.start() def stopProcess(self): # stops the timer and thus the processing self.sTimer.stop() def run(self): self.show() self.qt_app.exec_()
class imGui(QMainWindow): def __init__(self, workQueue, processedQueue, pLock): self.qt_app = QApplication(sys.argv) QWidget.__init__(self, None) # create the main ui self.pQueue = processedQueue self.wQueue = workQueue self.ui = Ui_ImageProcessor() self.ui.setupUi(self) self.imageScene = QGraphicsScene() self.original = None self.processed = None self.frameScaled = None self.imageFile = None self.pLock = pLock self.patchsize = 128 # fill combobox self.ui.boxFunction.addItem("GrayScale") self.ui.boxFunction.addItem("SobelFilter") # connect buttons self.ui.buttonLoadImage.clicked.connect(self.loadImagePressed) self.ui.buttonResetImage.clicked.connect(self.resetImagePressed) self.ui.buttonStartProcess.clicked.connect(self.startProcess) self.ui.buttonStopProcess.clicked.connect(self.stopProcess) # start timer self.timer = QTimer() self.timer.timeout.connect(self.collectPatch) self.timer.start(10) self.sTimer = QTimer() self.sTimer.timeout.connect(self.scheduler) self.perms = None self.sceneObject = None def loadImagePressed(self): # load the image from a file into a QImage object imageFile = QFileDialog.getOpenFileName( self, 'Open file', '.', 'Images (*.png *.xpm ' '*.jpg)') if not imageFile: return with open(imageFile, 'r') as f: try: self.imageFile = imageFile self.original = QImage(imageFile) # say that an image is loaded print "Image loaded: " + imageFile except: print "Problem with image file: " + self.imageFile self.imageFile = None finally: self.processed = self.original.convertToFormat( QImage.Format_RGB16) if self.imageFile: # find the horizontal and vertical patch numgers self.tmpPatchNum = (self.processed.size().width() / self.patchsize, self.processed.size().height() / self.patchsize) self.numPatches = self.tmpPatchNum[0] * \ self.tmpPatchNum[1] self.updateImage() def resetImagePressed(self): # return to the original image if not self.imageFile: return self.processed = self.original self.updateImage() def updateImage(self): # update the visual of the image with the new processed image if self.processed: multiplierh = float(self.processed.size().height()) / float( self.ui.imageView.size().height()) multiplierw = float(self.processed.size().width()) / float( self.ui.imageView.size().width()) if multiplierh > multiplierw: self.frameScaled = self.processed.scaledToHeight( self.ui.imageView.size().height() - 5) else: self.frameScaled = self.processed.scaledToWidth( self.ui.imageView.size().width() - 5) if self.sceneObject: self.imageScene.removeItem(self.sceneObject) self.sceneObject = self.imageScene.addPixmap( QPixmap.fromImage(self.frameScaled)) self.imageScene.update() self.ui.imageView.setScene(self.imageScene) def serializePatch(self, x, y, offset=0): # serializes the patch and prepares the message data tempVector = [0] * (self.patchsize**2) rect = (x * self.patchsize, y * self.patchsize) rng = range(0, self.patchsize) for j in rng: Y = j + rect[1] for i in rng: X = i + rect[0] # the message contains 8-bit grayscale (0-255) data tempVector[j*self.patchsize + i] = \ rgb2gray(self.processed.pixel(X,Y)) # we should also send the reference rectangle information # where to put the patch when we receive the processed return rect, tempVector def deserializePatch(self, refPix, data): # convert the message data into the matrix and put directy on the # image using the reference pixels (refPix) counter = 0 for color in data: x = counter % self.patchsize y = counter // self.patchsize self.processed.setPixel(refPix[0] + x, refPix[1] + y, gray2rgb(color)) counter += 1 def scheduler(self): # puts the serialized patches into the work queue if self.processed: function = self.ui.boxFunction.currentText() if len(self.perms) > 0: p = self.perms.pop() x = p % self.tmpPatchNum[0] y = p // self.tmpPatchNum[0] rect, tempVector = self.serializePatch(x, y) self.wQueue.put(((function, rect), tempVector)) else: self.sTimer.stop() def collectPatch(self): # collects the processed patches from the process queue if self.pQueue.qsize() > 0: for i in range(0, self.pQueue.qsize()): # self.pLock.acquire() message = self.pQueue.get() # self.pLock.release() self.deserializePatch(message[0], message[1]) self.updateImage() def startProcess(self): # randomly organizes the patches self.perms = list(np.random.permutation(self.numPatches)) # or simply orders the patches # self.perms = range(0,self.numPatches) # self.perms.reverse() # start scheduler's timer self.sTimer.start() def stopProcess(self): # stops the timer and thus the processing self.sTimer.stop() def run(self): self.show() self.qt_app.exec_()