Skip to content

Commit

Permalink
opencv no longer dirty window
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Genty committed May 4, 2016
1 parent acede7b commit b72de4d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 125 deletions.
145 changes: 38 additions & 107 deletions mac/pyrgb/display/cv2display.py → mac/pyrgb/display/cv2layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,77 @@
from .. import QtGui, QtCore
from ..rgb_cv2.cv2selector import CV2Selector

class CV2Display(QtGui.QWidget):
class CV2Layout(QtGui.QWidget):

def __init__(self):
super(CV2Display,self).__init__()
super(CV2Layout,self).__init__()
self.cv2_inputs = QtGui.QGridLayout()

self.name = "CV2Layout"

self.enabled = False
self.cv2 = None
self.imi = None
self.overwrite = False
self.transform = True

# this should save users from having to load
# opencv if they don't want to...


def enable(self):

if self.enabled == True:
return

import cv2
self.cv2 = cv2
self.name = "CV2Display"
self.win = pg.GraphicsWindow()
self.plt = self.win.addPlot()
self.layout = QtGui.QGridLayout()
self.layout.addWidget( self.win, 0, 0, 1, 10 )
self.setLayout(self.layout)

### Input Widgets
### Layouts
self.lay_inputs = QtGui.QGridLayout()
self.layout.addLayout( self.lay_inputs, 1, 0 )

self.option = QtGui.QLabel("<b>Function</b>")
self.lay_inputs.addWidget(self.option,0,0)

self.selector = CV2Selector()

# fill out the combo box selector
self.comboBoxSelector = QtGui.QComboBox()
for selection in self.selector.selections: #its a dict
self.comboBoxSelector.addItem(selection)

self.lay_inputs.addWidget(self.comboBoxSelector,0,1)


self.setSelection()

self.tf = QtGui.QCheckBox("Transform")
self.tf.setChecked(True)
self.lay_inputs.addWidget( self.tf, 1, 0 )

self.tf.stateChanged.connect(self.setTransform)
self.setTransform()

self.ow = QtGui.QCheckBox("Overwrite")
self.ow.setChecked(False)
self.lay_inputs.addWidget( self.ow, 1, 1 )
self.ow.stateChanged.connect(self.setOverwrite)
self.setOverwrite()

# self.option = QtGui.QLabel("<b>Options</b>")
# self.lay_inputs.addWidget(self.option,0,1)
self.ow.stateChanged.connect(self.setOverwrite)

self.setOverwrite()
self.changed = False

self.menu = {}
self.setMenu()


self.loaded = QtGui.QPushButton("Reload!")
self.lay_inputs.addWidget(self.loaded,2,0)
self.loaded = QtGui.QPushButton("Reload!")
self.loaded.clicked.connect(self.reLoad)

self.enabled = True

def grid(self,enable):

if self.enabled == False: #don't load cv2 unless necessary
import cv2
self.cv2 = cv2

if enable == True:
self.enabled = True
self.cv2_inputs.addWidget(self.option,0,0)
self.cv2_inputs.addWidget(self.comboBoxSelector,1,0)
self.cv2_inputs.addWidget(self.tf, 0, 1 )
self.cv2_inputs.addWidget(self.ow, 1, 1 )
self.cv2_inputs.addWidget(self.loaded,2,0)

return self.cv2_inputs

for i in reversed(range(self.cv2_inputs.count())):
self.cv2_inputs.itemAt(i).widget().setParent(None)

#does shit or not to slice
def setTransform(self):
if self.tf.isChecked():
self.transform = True
else:
self.transform = False


#apply over and over?
def setOverwrite(self):
if self.ow.isChecked():
self.overwrite = True
Expand All @@ -104,26 +93,25 @@ def reLoad(self):

def setMenu(self):

c = 2
if len(self.menu) != 0:
for item in self.menu:
#explicitly get rid of these fuckers
self.lay_inputs.removeWidget(self.menu[item][0])
self.lay_inputs.removeWidget(self.menu[item][1])
self.cv2_inputs.removeWidget(self.menu[item][0])
self.cv2_inputs.removeWidget(self.menu[item][1])
self.menu[item][0].setParent(None)
self.menu[item][1].setParent(None)


# now they are gone right...
self.menu = {}

c = 2
for op,ty in self.selector.selection.options.iteritems():
l = QtGui.QLabel(op)
t = self.selector.selection.types[op]
o = self.set_type(ty,t)
o = self.selector.selection.set_type(ty,t)

self.lay_inputs.addWidget(l,1,c)
self.lay_inputs.addWidget(o,2,c)
self.cv2_inputs.addWidget(l,0,c)
self.cv2_inputs.addWidget(o,1,c)

self.menu[op] = (l,o,t)

Expand All @@ -136,76 +124,19 @@ def setSelection(self):
self.selected = str(self.comboBoxSelector.currentText())

def paint(self,sl): #sl == slice of pimg
if self.enabled == False:
return

self.plt.clear()
self.imi = pg.ImageItem()
self.plt.addItem(self.imi)

# lets tell selector about our options
if self.changed == True:
for item in self.menu:
it = self.menu[item]
it = (str(it[1].text()),it[2])
self.selector.selection.options[item] = self.convert(it)
self.selector.selection.options[item] = self.selector.selection.convert(it)

self.changed = False

# get the selection, apply transformation
# for now just one at a time
sl = self.selector.apply(sl)

self.imi.setImage(sl)
return sl

def set_type(self,option,ty):
qte = QtGui.QLineEdit()
qte.setFixedHeight(5*10)
qte.setFixedWidth(75)

text = ""

if ty is tuple:
text = "("
for o in option:
text += str(o) + ","

text = text[:-1]
text += ")"
qte.setText(text)
return qte

if ty is int:
qte.setText(str(option))
return qte

if ty is float:
qte.setText(str(option))
return qte

raise Exception("Please implement type: {} in set_type cv2display".format(type(option)))

def convert(self,tu):

if tu[1] is tuple:
t = tu[0][1:-1].split(",")
return tuple([int(i) for i in t])

if tu[1] is int:
return int(tu[0])

if tu[1] is float:
return float(tu[0])

if tu[1] is list:

t = tu[0].split(",")

#shave of front and back
t = t[1:-1]

#make it
o = [float(i) for i in t]

return o
31 changes: 14 additions & 17 deletions mac/pyrgb/display/rgbdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

from ..lib.hoverrect import HoverRect as HR


from cv2layout import CV2Layout
from caffelayout import CaffeLayout
from ..rgb_caffe.testwrapper import TestWrapper

from cv2display import CV2Display

class RGBDisplay(QtGui.QWidget) :

def __init__(self,argv):
Expand Down Expand Up @@ -194,8 +192,8 @@ def __init__(self,argv):
self.pimg = None
self.modimg = None

self.rgbcaffe.clicked.connect( self.expandWindow )
self.rgbcv2.clicked.connect( self.openCVEditor )
self.rgbcaffe.clicked.connect( self.openCaffe )
self.rgbcv2.clicked.connect ( self.openCVEditor)

### Caffe Widgets
#wrapper for FORWARD function
Expand All @@ -205,12 +203,10 @@ def __init__(self,argv):

### OpenCV Widgets
#wrapper for the opencv specific window
self.cv2_display = CV2Display()
self.cv2_layout = CV2Layout()
self.cv2_enabled = False



def expandWindow(self):
def openCaffe(self):
if re.search("Disable",self.rgbcaffe.text()) is None:
self.rgbcaffe.setText("Disable RGBCaffe")
self.resize( 1200, 900 )
Expand All @@ -223,15 +219,16 @@ def expandWindow(self):
def openCVEditor(self):
if re.search("Disable",self.rgbcv2.text()) is None:
self.rgbcv2.setText("Disable OpenCV")
self.cv2_display.enable()
self.cv2_display.show()
self.resize( 1200, 900 )
self.layout.addLayout(self.cv2_layout.grid(True), 3, 0 )
self.plt.addItem(self.swindow)
self.cv2_enabled = True
else:
self.cv2_enabled = False
self.rgbcv2.setText("Enable OpenCV")
self.cv2_display.hide()
self.layout.removeItem(self.cv2_layout.grid(False))
self.resize( 1200, 700 )
self.plt.removeItem(self.swindow)
self.cv2_enabled = False

def setRunInfo(self,run,subrun,event):
self.runinfo.setText("<b>Run:</b> {} <b>Subrun:</b> {} <b>Event:</b> {}".format(run,subrun,event))
Expand Down Expand Up @@ -482,18 +479,18 @@ def regionChanged(self):
sl = self.swindow.getArraySlice(self.pimg,self.imi)[0]

# need mask if user doesn't want to overwrite
if self.cv2_display.overwrite == False:
if self.cv2_layout.overwrite == False:
idx = np.where( self.modimage == 1 )
pcopy = self.pimg.copy()

self.pimg[ sl ] = self.cv2_display.paint( self.pimg[ sl ] ) ##11,11,3
self.pimg[ sl ] = self.cv2_layout.paint( self.pimg[ sl ] ) ##11,11,3

# use mask to updated only pixels not already updated
if self.cv2_display.overwrite == False:
if self.cv2_layout.overwrite == False:
self.pimg[ idx ] = pcopy[ idx ]
self.modimage[ sl ] = 1

if self.cv2_display.transform == False:
if self.cv2_layout.transform == False:
return

self.imi.setImage(self.pimg)
Expand Down
23 changes: 23 additions & 0 deletions mac/pyrgb/rgb_cv2/cv2copypaste.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from cv2selection import CV2Selection
import cv2

class CV2CopyPaste(CV2Selection):
def __init__(self):
super(CV2CopyPaste,self).__init__()

self.name = "CV2CopyPaste"
# how to impletement this?
# default options
self.copy = None

def __description__(self):
return "No description provided!"

def __implement__(self,image):
if self.paste == False:
self.copy = image.copy()
return image

#can paste multiple times
return self.copy

Loading

0 comments on commit b72de4d

Please sign in to comment.