-
Notifications
You must be signed in to change notification settings - Fork 9
/
CameraProfileBrick.py
136 lines (111 loc) · 4.49 KB
/
CameraProfileBrick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logging
import numpy
import qt
import qtcanvas
from Qub.Widget.QubAction import QubToggleAction
from Qub.Objects.QubDrawingManager import QubPointDrawingMgr,QubAddDrawing
from Qub.Objects.QubDrawingCanvasTools import QubCanvasHLine,QubCanvasVLine
from BlissFramework.BaseComponents import BlissWidget
__category__ = "Camera"
class _graphPoint(qtcanvas.QCanvasPolygon) :
def __init__(self,canvas) :
qtcanvas.QCanvasPolygon.__init__(self,canvas)
def drawShape(self,painter) :
points = self.points()
if self.isVisible():
painter.setPen(self.pen())
painter.drawLineSegments(points,0)
class CameraProfileBrick(BlissWidget) :
def __init__(self,*args) :
BlissWidget.__init__(self,*args)
self.__view = None
self.__drawing = None
self.__line = None
self.__pointSelected = None
self.__graphs = None
self.__refreshTimer = qt.QTimer(self)
qt.QObject.connect(self.__refreshTimer,qt.SIGNAL('timeout()'),self.__refreshGraph)
# Properties
####### SIGNAL #######
self.defineSignal('getView',())
self.defineSignal('getImage',())
self.setFixedSize(0,0)
def run(self) :
key = {}
self.emit(qt.PYSIGNAL("getView"), (key,))
try:
self.__view = key['view']
self.__drawing = key['drawing']
except KeyError:
logging.getLogger().error('%s : You have to connect this brick to the CameraBrick',self.name())
return
self.__toggleButton = QubToggleAction(label='Show profile',name='histogram',place='toolbar',
group='Camera',autoConnect = True)
qt.QObject.connect(self.__toggleButton,qt.PYSIGNAL('StateChanged'),self.__showCBK)
self.__view.addAction([self.__toggleButton])
self.__line,_,_ = QubAddDrawing(self.__drawing,QubPointDrawingMgr,QubCanvasHLine,QubCanvasVLine)
self.__line.setEndDrawCallBack(self.__clickedPoint)
graphV = _graphPoint(self.__drawing.canvas())
graphV.setPen(qt.QPen(qt.Qt.red,2))
graphH = _graphPoint(self.__drawing.canvas())
graphH.setPen(qt.QPen(qt.Qt.green,2))
graphH.setZ(5)
self.__graphs = (graphH,graphV)
def __showCBK(self,state) :
if state:
self.__line.startDrawing()
self.__refreshTimer.start(1000)
else:
self.__refreshTimer.stop()
self.__line.hide()
self.__line.stopDrawing()
def __clickedPoint(self,drawingMgr) :
self.__pointSelected = drawingMgr.point()
for graph in self.__graphs:
graph.show()
self.__refreshGraph()
def __refreshGraph(self) :
key = {}
try:
self.emit(qt.PYSIGNAL("getImage"), (key,))
qimage = key['image']
except KeyError: return
matrix = self.__drawing.matrix()
try:
x,y = self.__pointSelected
except TypeError: return
(graphH,graphV) = self.__graphs
array = numpy.fromstring(qimage.bits().asstring(qimage.width() * qimage.height() * 4),
dtype = numpy.uint8)
array.shape = qimage.height(),qimage.width(),-1
yColor = array[y]
yData = yColor[:,0] * 0.114 + yColor[:,1] * 0.587 + yColor[:,2] * 0.299
maxData = yData.max()
maxHeight = qimage.height() / 3
z = float(maxHeight) / maxData
yData = yData * z
yData = (qimage.height()) - yData
#yData[len(yData)/2]= 220
xData = numpy.arange(qimage.width())
allPoint = numpy.array(zip(xData,yData))
allPoint.shape = -1
aP = qt.QPointArray(len(xData))
aP.putPoints(0,allPoint.tolist())
aP = matrix.map(aP)
graphH._myXProfile = aP
graphH.setPoints(aP)
xColor = array[:,x]
xData = xColor[:,0] * 0.114 + xColor[:,1] * 0.587 + xColor[:,2] * 0.299
maxData = xData.max()
maxHeight = qimage.width() / 3
z = float(maxHeight) / maxData
xData = xData * z
xData = qimage.width() - xData
yData = numpy.arange(qimage.height())
allPoint = numpy.array(zip(xData,yData))
allPoint.shape = -1
aP = qt.QPointArray(len(xData))
aP.putPoints(0,allPoint.tolist())
aP = matrix.map(aP)
graphV._myYProfile = aP
graphV.setPoints(aP)