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
from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QAction, QFileDialog
from threading import Thread
from webcamROI import roiWindow
#%%
def save(aos):
......@@ -88,19 +90,21 @@ def putWebcams(checked):
pass
def openRoi():
t_roi = Thread(target=startRoiThread, args=())
t_roi.start()
print('opening roi window')
#t_roi = Thread(target=startRoiThread, args=())
#t_roi.start()
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):
menubar = win.menuBar()
saveAct = QAction('Save', win)
loadAct = QAction('Load', win)
saveAsAct = QAction('Save as' , win)
......@@ -130,4 +134,4 @@ def incorporate_toolbar(win, analog_control_signals):
saveAsAct.triggered.connect(lambda: save_as(analog_control_signals, win))
openFromAct.triggered.connect(lambda: open_from(analog_control_signals, win))
viewWebcams.toggled.connect(lambda: putWebcams(viewWebcams.isChecked()) )
plotRoi.toggled.connect(openRoi)
\ No newline at end of file
plotRoi.triggered.connect(openRoi)
\ No newline at end of file
......@@ -2,110 +2,109 @@
"""
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 pyqtgraph as pg
import cv2
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():
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)
class roiWindow(QMainWindow):
## create GUI
app = QtGui.QApplication([])
w = pg.GraphicsWindow(size=(800,800), border=True)
v = w.addViewBox(colspan=2)
v.invertY(True) ## Images usually have their Y-axis pointing downward
v.setAspectLocked(True)
im1 = pg.ImageItem(capture())
v.addItem(im1)
def __init__(self):
super().__init__()
self.cap = cv2.VideoCapture(1)
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()
v2 = w.addViewBox(1,0)
v2.addItem(im2)
self.imgTimer = QTimer()
self.imgTimer.timeout.connect(self.updateImage)
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):
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
self.lastRoi = None
def updateRoiPlot(data):
global curve
curve.setData(data)
app.processEvents() ## force complete redraw for every plot
rois = []
#rois.append(pg.EllipseROI([110, 10], [30, 20], pen=(3,9)))
rois.append(pg.TestROI([0, 0], [20, 20], pen=(0,9)))
## 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)
## Add each ROI to the scene and link its data to a plot curve with the same color
for r in self.rois:
self.v.addItem(r)
r.sigRegionChanged.connect(self.updateRoi)
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
w.show()
self.w.show()
def capture(self):
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__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
app = QApplication(sys.argv)
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