Commit c644ab2b authored by Martin Drechsler's avatar Martin Drechsler

Still the roi app is not properly incorporated. I will try to make everything...

Still the roi app is not properly incorporated. I will try to make everything nice, written as pyqt classes, I think that way this will work.
parent e2f6b158
...@@ -9,6 +9,8 @@ Created on Thu Jul 5 12:17:30 2018 ...@@ -9,6 +9,8 @@ Created on Thu Jul 5 12:17:30 2018
from PyQt5.QtCore import QSettings from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QAction, QFileDialog from PyQt5.QtWidgets import QAction, QFileDialog
from threading import Thread from threading import Thread
from webcamROI import roiWindow
#%% #%%
def save(aos): def save(aos):
...@@ -88,19 +90,21 @@ def putWebcams(checked): ...@@ -88,19 +90,21 @@ def putWebcams(checked):
pass pass
def openRoi(): def openRoi():
t_roi = Thread(target=startRoiThread, args=()) print('opening roi window')
t_roi.start() #t_roi = Thread(target=startRoiThread, args=())
#t_roi.start()
startRoiThread()
def startRoiThread(): def startRoiThread():
exec(open('C:\\Users\\Usuario\\Documents\\control_app\\webcamROI.py').read()) roiW = roiWindow()
roiW.w.show()
def incorporate_toolbar(win, analog_control_signals): def incorporate_toolbar(win, analog_control_signals):
menubar = win.menuBar() menubar = win.menuBar()
saveAct = QAction('Save', win) saveAct = QAction('Save', win)
loadAct = QAction('Load', win) loadAct = QAction('Load', win)
saveAsAct = QAction('Save as' , win) saveAsAct = QAction('Save as' , win)
...@@ -130,4 +134,4 @@ def incorporate_toolbar(win, analog_control_signals): ...@@ -130,4 +134,4 @@ def incorporate_toolbar(win, analog_control_signals):
saveAsAct.triggered.connect(lambda: save_as(analog_control_signals, win)) saveAsAct.triggered.connect(lambda: save_as(analog_control_signals, win))
openFromAct.triggered.connect(lambda: open_from(analog_control_signals, win)) openFromAct.triggered.connect(lambda: open_from(analog_control_signals, win))
viewWebcams.toggled.connect(lambda: putWebcams(viewWebcams.isChecked()) ) viewWebcams.toggled.connect(lambda: putWebcams(viewWebcams.isChecked()) )
plotRoi.toggled.connect(openRoi) plotRoi.triggered.connect(openRoi)
\ No newline at end of file \ No newline at end of file
...@@ -2,110 +2,109 @@ ...@@ -2,110 +2,109 @@
""" """
Created on Fri Aug 10 14:21:33 2018 Created on Fri Aug 10 14:21:33 2018
@author: Usuario @author: Martindrech
""" """
from pyqtgraph.Qt import QtCore, QtGui from pyqtgraph.Qt import QtCore
import numpy as np import numpy as np
import pyqtgraph as pg import pyqtgraph as pg
import cv2 import cv2
from PyQt5.QtCore import QTimer from PyQt5.QtCore import QTimer
cap = cv2.VideoCapture(1) import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
n = int(1e2)
data = np.zeros(n)
i = 0
def capture(): class roiWindow(QMainWindow):
ret, frame = cap.read()
#frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return gray_image
def updateImage():
im1.setImage(capture(), cmap = 'Gray')
updateRoi(lastRoi)
## create GUI def __init__(self):
app = QtGui.QApplication([]) super().__init__()
w = pg.GraphicsWindow(size=(800,800), border=True)
v = w.addViewBox(colspan=2)
v.invertY(True) ## Images usually have their Y-axis pointing downward self.cap = cv2.VideoCapture(1)
v.setAspectLocked(True)
im1 = pg.ImageItem(capture())
v.addItem(im1)
self.n = int(1e2)
self.data = np.zeros(self.n)
self.i = 0
self.rois = []
#self.rois.append(pg.EllipseROI([110, 10], [30, 20], pen=(3,9)))
self.rois.append(pg.TestROI([0, 0], [20, 20], pen=(0,9)))
self.initUI()
def initUI(self):
## create GUI
self.w = pg.GraphicsWindow(size=(800,800), border=True)
self.v = self.w.addViewBox(colspan=2)
self.v.invertY(True) ## Images usually have their Y-axis pointing downward
self.v.setAspectLocked(True)
self.im1 = pg.ImageItem(self.capture())
self.v.addItem(self.im1)
imgTimer = QTimer()
imgTimer.timeout.connect(updateImage)
imgTimer.start(10)
im2 = pg.ImageItem() self.imgTimer = QTimer()
v2 = w.addViewBox(1,0) self.imgTimer.timeout.connect(self.updateImage)
v2.addItem(im2) self.imgTimer.start(10)
self.im2 = pg.ImageItem()
self.v2 = self.w.addViewBox(1,0)
self.v2.addItem(self.im2)
p = w.addPlot(row = 3, col = 0, title="Updating plot")
p.setRange(QtCore.QRectF(0, -10, 5000, 20))
p.setAutoPan(y=True)
p.setRange(xRange = (0, n), yRange = (0, 255))
curve = p.plot(pen='y') self.p = self.w.addPlot(row = 3, col = 0, title="Updating plot")
self.p.setRange(QtCore.QRectF(0, -10, 5000, 20))
self.p.setAutoPan(y=True)
self.p.setRange(xRange = (0, self.n), yRange = (0, 255))
self.curve = self.p.plot(pen='y')
lastRoi = None
def updateRoi(roi): self.lastRoi = None
global im1, im2, lastRoi, data, i, curve
if roi is None:
return
lastRoi = roi
roiSlice = roi.getArrayRegion(im1.image, img=im1)
im2.setImage(roiSlice)
newData = np.mean(roiSlice)
data[i] = newData
i = np.mod(i + 1, len(data))
curve.setData(data)
app.processEvents() ## force complete redraw for every plot
def updateRoiPlot(data):
global curve
curve.setData(data)
app.processEvents() ## force complete redraw for every plot
rois = [] ## Add each ROI to the scene and link its data to a plot curve with the same color
#rois.append(pg.EllipseROI([110, 10], [30, 20], pen=(3,9))) for r in self.rois:
rois.append(pg.TestROI([0, 0], [20, 20], pen=(0,9))) self.v.addItem(r)
r.sigRegionChanged.connect(self.updateRoi)
## Add each ROI to the scene and link its data to a plot curve with the same color
for r in rois:
v.addItem(r)
#c = pi1.plot(pen=r.pen)
#r.curve = c
r.sigRegionChanged.connect(updateRoi)
def updateImage():
global im1, arr, lastRoi
r = abs(np.random.normal(loc=0, scale=(arr.max()-arr.min())*0.1, size=arr.shape))
im1.updateImage(arr + r)
updateRoi(lastRoi)
#updateRoiPlot(data)
if cv2.waitKey(1) & 0xFF == ord('q'): self.w.show()
cap.release()
cv2.destroyAllWindows()
def capture(self):
w.show() ret, frame = self.cap.read()
#frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return gray_image
def updateImage(self):
self.im1.setImage(self.capture(), cmap = 'Gray')
self.updateRoi(self.lastRoi)
def updateRoi(self, roi):
if roi is None:
return
self.lastRoi = roi
roiSlice = roi.getArrayRegion(self.im1.image, img=self.im1)
self.im2.setImage(roiSlice)
newData = np.mean(roiSlice)
self.data[self.i] = newData
self.i = np.mod(self.i + 1, len(self.data))
self.curve.setData(self.data)
## Start Qt event loop unless running in interactive mode or using pyside. def updateRoiPlot(self, data):
self.curve.setData(data)
#app.processEvents() ## force complete redraw for every plot
if __name__ == '__main__': if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): app = QApplication(sys.argv)
QtGui.QApplication.instance().exec_() ex = roiWindow()
sys.exit(app.exec_())
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment