-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDisplayImage.py
468 lines (410 loc) · 16.7 KB
/
DisplayImage.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage, QPen, QColor, QPainter
from PyQt5.QtCore import Qt, QPointF, QSize, QRectF
import param
# Comments are whether we are in image or screen coordinates.
class DisplayImage(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
gui = parent
x = gui.parentWidget()
while x is not None:
gui = x
x = gui.parentWidget()
self.gui = gui
self.hint = QSize(self.gui.viewwidth, self.gui.viewheight)
self.retry = False
self.pcnt = 0
size = param.getSize()
self.image = QImage(size, QImage.Format_RGB32)
self.image.fill(0)
self.rectZoom = param.Rect(0, 0, param.x, param.y) # image
self.arectZoom = param.Rect(0, 0, param.x, param.y) # image
self.rectRoi = param.Rect(0, 0, param.x, param.y) # image
self.paintevents = 0
self.xoff = QPointF(20, 0)
self.yoff = QPointF(0, 20)
self.setZoom()
self.cursorPos = param.Point(0, 0) # image
self.setMouseTracking(True)
self.rectImage = QRectF(0, 0, size.width(), size.height()) # screen
self.sWindowTitle = "Camera: None"
# set marker data
self.lMarker = [
param.Point(-100, -100), # image
param.Point(param.x + 100, -100),
param.Point(param.x + 100, param.y + 100),
param.Point(-100, param.y + 100),
]
self.lPenColor = [
(0 / 255.0, 128 / 255.0, 255 / 255.0), # matplotlib colors!!
(255 / 255.0, 0 / 255.0, 0 / 255.0),
(0 / 255.0, 204 / 255.0, 204 / 255.0),
(204 / 255.0, 0 / 255.0, 204 / 255.0),
]
self.lPenMarker = [
QPen(QColor(0, 128, 255), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(255, 0, 0), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(0, 204, 204), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(204, 0, 204), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
]
self.lPenProj = [
QPen(QColor(0, 128, 255), 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(255, 0, 0), 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(0, 204, 204), 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin),
QPen(QColor(204, 0, 204), 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin),
]
self.penMarkerBack = QPen(Qt.black, 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
self.penProjBack = QPen(Qt.black, 1, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin)
self.penRoi = QPen(Qt.green, 1, Qt.DashLine, Qt.RoundCap, Qt.RoundJoin)
self.penRoiBack = QPen(Qt.black, 1, Qt.DashLine, Qt.RoundCap, Qt.RoundJoin)
self.roiInvX = False
self.roiInvY = False
def contextMenuEvent(self, ev):
ui = self.gui.ui
# Fix up the menu!
if ui.showconf.isChecked():
ui.actionShow_Configuration.setText("Hide Configuration")
else:
ui.actionShow_Configuration.setText("Show Configuration")
if ui.showproj.isChecked():
ui.actionShow_Projection.setText("Hide Projection")
else:
ui.actionShow_Projection.setText("Show Projection")
# Start the menu!
action = ui.menuPopup.exec_(ev.globalPos())
# Take the action!
if action == ui.actionReset_ROI:
self.gui.onRoiReset()
elif action == ui.actionZoom_to_ROI:
self.zoomToRoi()
elif action == ui.actionShow_Configuration:
ui.showconf.setChecked(not ui.showconf.isChecked())
self.gui.doShowConf()
elif action == ui.actionShow_Projection:
ui.showproj.setChecked(not ui.showproj.isChecked())
self.gui.doShowProj()
elif action == ui.actionSave_to_File:
self.gui.onfileSave()
def resizeEvent(self, ev):
self.setZoom()
def doResize(self, s=None):
if s is None:
s = self.size()
self.hint = s
self.updateGeometry()
def sizeHint(self):
return self.hint
def setRectZoom(self, x, y, w, h):
self.rectZoom = param.Rect(x, y, w, h)
self.setZoom()
def setImageSize(self, reset=True):
size = param.getSize()
self.image = QImage(size, QImage.Format_RGB32)
self.image.fill(0)
if reset:
self.rectZoom = param.Rect(0, 0, param.x, param.y)
self.rectRoi = param.Rect(0, 0, param.x, param.y)
self.setZoom()
self.gui.updateRoiText()
self.gui.updateMiscInfo()
if reset:
self.lMarker = [
param.Point(-100, -100), # image
param.Point(param.x + 100, -100),
param.Point(param.x + 100, param.y + 100),
param.Point(-100, param.y + 100),
]
def pWidth(self):
if param.orientation & 2:
return self.height()
else:
return self.width()
def pHeight(self):
if param.orientation & 2:
return self.width()
else:
return self.height()
def paintEvent(self, event):
if self.gui.dispUpdates == 0:
return
painter = QPainter(self)
# painter.setRenderHint(QPainter.Antialiasing)
self.paintevents += 1
fZoomedWidth = param.zoom * self.arectZoom.oriented().width()
fZoomedHeight = param.zoom * self.arectZoom.oriented().height()
self.rectImage = QRectF(
(self.width() - fZoomedWidth) / 2,
(self.height() - fZoomedHeight) / 2,
fZoomedWidth,
fZoomedHeight,
)
# Draw arectZoom portion of image into rectImage
painter.drawImage(self.rectImage, self.image, self.arectZoom.oriented())
painter.setOpacity(1)
lbProjChecked = [
self.gui.ui.checkBoxM1Lineout.isChecked(),
self.gui.ui.checkBoxM2Lineout.isChecked(),
self.gui.ui.checkBoxM3Lineout.isChecked(),
self.gui.ui.checkBoxM4Lineout.isChecked(),
]
for (iMarker, ptMarker) in enumerate(self.lMarker):
markerImage = (
ptMarker.oriented() - self.arectZoom.oriented().topLeft()
) * param.zoom + self.rectImage.topLeft() # screen
painter.setPen(self.penMarkerBack)
painter.drawLine(
markerImage - self.xoff + QPointF(1, 1),
markerImage + self.xoff + QPointF(1, 1),
)
painter.drawLine(
markerImage - self.yoff + QPointF(1, 1),
markerImage + self.yoff + QPointF(1, 1),
)
painter.setPen(self.lPenMarker[iMarker])
painter.drawLine(markerImage - self.xoff, markerImage + self.xoff)
painter.drawLine(markerImage - self.yoff, markerImage + self.yoff)
if lbProjChecked[iMarker]:
painter.setPen(self.penProjBack)
painter.drawLine(
markerImage.x() + 1, 1, markerImage.x() + 1, self.pHeight()
)
painter.drawLine(
1, markerImage.y() + 1, self.pWidth(), markerImage.y() + 1
)
painter.setPen(self.lPenProj[iMarker])
painter.drawLine(
markerImage.x(), 0, markerImage.x(), self.pHeight() - 1
)
painter.drawLine(0, markerImage.y(), self.pWidth() - 1, markerImage.y())
roiTopLeft = (
self.rectRoi.oriented().topLeft() - self.arectZoom.oriented().topLeft()
) * param.zoom + self.rectImage.topLeft() # image
roiSize = self.rectRoi.oriented().size() * param.zoom
painter.setPen(self.penRoiBack)
painter.drawRect(QRectF(roiTopLeft + QPointF(1, 1), roiSize))
painter.setPen(self.penRoi)
painter.drawRect(QRectF(roiTopLeft, roiSize))
def mousePressEvent(self, event):
# OK, what's going on here?
# arectZoom is coordinates in the image.
# rectImage is coordinates on the screen.
# posx is coordinates on the screen as well.
posx = event.x()
posy = event.y()
# rectImage is inside (xpos, ypos). Convert to oriented image coordinates.
imgx = (posx - self.rectImage.x()) * (
self.arectZoom.oriented().width() / self.rectImage.width()
) + self.arectZoom.oriented().x()
imgy = (posy - self.rectImage.y()) * (
self.arectZoom.oriented().height() / self.rectImage.height()
) + self.arectZoom.oriented().y()
self.cursorPos = param.Point(imgx, imgy, rel=True)
if self.gui.iSpecialMouseMode == 0:
self.lastMousePos = event.pos()
return
if self.gui.iSpecialMouseMode == 5:
self.roiInvX = False
self.roiInvY = False
self.rectRoi.setRel(imgx, imgy, 2, 2)
self.gui.updateRoiText()
self.gui.updateProj()
if self.gui.cfg is None:
self.gui.dumpConfig()
elif self.gui.iSpecialMouseMode >= 1 and self.gui.iSpecialMouseMode <= 4:
self.lMarker[self.gui.iSpecialMouseMode - 1].setRel(imgx, imgy)
self.lMarker[self.gui.iSpecialMouseMode - 1].pr(
"Set marker %d to (%d, %d): " % (self.gui.iSpecialMouseMode, imgx, imgy)
)
self.gui.updateMarkerText(
True,
True,
1 << (self.gui.iSpecialMouseMode - 1),
1 << (self.gui.iSpecialMouseMode - 1),
)
self.gui.updateProj()
if self.gui.cfg is None:
self.gui.dumpConfig()
self.update()
def mouseMoveEvent(self, event):
posx = event.x()
posy = event.y()
imgx = (posx - self.rectImage.x()) * (
self.arectZoom.oriented().width() / self.rectImage.width()
) + self.arectZoom.oriented().x()
imgy = (posy - self.rectImage.y()) * (
self.arectZoom.oriented().height() / self.rectImage.height()
) + self.arectZoom.oriented().y()
self.cursorPos = param.Point(imgx, imgy, rel=True)
if self.gui.iSpecialMouseMode == 0:
if not (event.buttons() & Qt.LeftButton):
return
return self.moveImage(event)
if self.gui.iSpecialMouseMode <= 5 and not (event.buttons() & Qt.LeftButton):
return
if self.gui.iSpecialMouseMode == 5:
if self.roiInvX:
if imgx > self.rectRoi.oriented().right():
self.roiInvX = False
else:
if imgx < self.rectRoi.oriented().left():
self.roiInvX = True
if self.roiInvX:
self.rectRoi.setLeft(imgx)
else:
self.rectRoi.setRight(imgx)
if self.roiInvY:
if imgy > self.rectRoi.oriented().bottom():
self.roiInvY = False
else:
if imgy < self.rectRoi.oriented().top():
self.roiInvY = True
if self.roiInvY:
self.rectRoi.setTop(imgy)
else:
self.rectRoi.setBottom(imgy)
self.gui.updateRoiText()
self.gui.updateProj()
if self.gui.cfg is None:
self.gui.dumpConfig()
elif self.gui.iSpecialMouseMode >= 1 and self.gui.iSpecialMouseMode <= 4:
self.lMarker[self.gui.iSpecialMouseMode - 1].setRel(imgx, imgy)
self.gui.updateMarkerText(
True,
True,
1 << (self.gui.iSpecialMouseMode - 1),
1 << (self.gui.iSpecialMouseMode - 1),
)
self.gui.updateProj()
if self.gui.cfg is None:
self.gui.dumpConfig()
self.update()
def mouseReleaseEvent(self, event):
if self.gui.iSpecialMouseMode != 0:
return self.mouseMoveEvent(event)
return self.moveImage(event)
def wheelEvent(self, event):
if event.angleDelta().y() < 0:
fFactor = 1.5
else:
fFactor = 1 / 1.5
zoomSize = self.rectZoom.oriented().size() * fFactor
posx = event.x()
posy = event.y()
shiftRatioX = (posx - self.rectImage.x()) / self.rectImage.width()
shiftRatioY = (posy - self.rectImage.y()) / self.rectImage.height()
imgx = (
shiftRatioX * self.arectZoom.oriented().width()
+ self.arectZoom.oriented().x()
)
imgy = (
shiftRatioY * self.arectZoom.oriented().height()
+ self.arectZoom.oriented().y()
)
pointNewTopLeft = QPointF(
imgx - zoomSize.width() * shiftRatioX,
imgy - zoomSize.height() * shiftRatioY,
)
self.rectZoom = param.Rect(
pointNewTopLeft.x(),
pointNewTopLeft.y(),
zoomSize.height(),
zoomSize.width(),
rel=True,
)
self.setZoom()
self.gui.updateall()
if self.gui.cfg is None:
self.gui.dumpConfig()
def moveImage(self, event):
dx = (event.x() - self.lastMousePos.x()) * (
self.arectZoom.oriented().width() / self.rectImage.width()
)
dy = (event.y() - self.lastMousePos.y()) * (
self.arectZoom.oriented().height() / self.rectImage.height()
)
self.lastMousePos = event.pos()
self.rectZoom = param.Rect(
self.rectZoom.oriented().x() - dx,
self.rectZoom.oriented().y() - dy,
self.rectZoom.oriented().width(),
self.rectZoom.oriented().height(),
rel=True,
)
self.setZoom()
self.gui.updateall()
if self.gui.cfg is None:
self.gui.dumpConfig()
def zoomByFactor(self, fFactor):
zoomSize = self.arectZoom.oriented().size() / fFactor
zoomCenterShift = QPointF(zoomSize.width(), zoomSize.height()) * 0.5
tl = self.arectZoom.oriented().center() - zoomCenterShift
self.rectZoom = param.Rect(
tl.x(), tl.y(), zoomSize.width(), zoomSize.height(), rel=True
)
self.setZoom()
self.gui.updateall()
if self.gui.cfg is None:
self.gui.dumpConfig()
def zoomToRoi(self):
self.rectZoom = param.Rect(
self.rectRoi.x, self.rectRoi.y, self.rectRoi.w, self.rectRoi.h
)
self.setZoom()
self.gui.updateall()
if self.gui.cfg is None:
self.gui.dumpConfig()
# This must be called after setting rectZoom so that arectZoom is correctly set!
#
# rectZoom is our *desired* zoom rectangle.
# This routine "fixes" it to make it actually fit in the available space.
def setZoom(self):
if self.rectZoom.oriented().width() <= 0:
self.rectZoom.setWidth(1)
if self.rectZoom.oriented().height() <= 0:
self.rectZoom.setHeight(1)
self.arectZoom = param.Rect(
self.rectZoom.x, self.rectZoom.y, self.rectZoom.w, self.rectZoom.h
)
h = self.height()
w = self.width()
fWidthRatio = w / self.arectZoom.oriented().width()
fHeightRatio = h / self.arectZoom.oriented().height()
if abs((fWidthRatio - fHeightRatio) / fWidthRatio) < 0.01:
param.zoom = w / self.arectZoom.oriented().width()
return
if fWidthRatio > fHeightRatio:
fNewZoomWidth = (
self.arectZoom.oriented().width() * fWidthRatio / fHeightRatio
)
fNewZoomX = (
self.arectZoom.oriented().x()
+ (self.arectZoom.oriented().width() - fNewZoomWidth) * 0.5
)
self.arectZoom.setLeft(fNewZoomX)
self.arectZoom.setWidth(fNewZoomWidth)
else:
fNewZoomHeight = (
self.arectZoom.oriented().height() * fHeightRatio / fWidthRatio
)
fNewZoomY = (
self.arectZoom.oriented().y()
+ (self.arectZoom.oriented().height() - fNewZoomHeight) * 0.5
)
self.arectZoom.setTop(fNewZoomY)
self.arectZoom.setHeight(fNewZoomHeight)
param.zoom = w / self.arectZoom.oriented().width()
def zoomReset(self):
self.zoomByFactor(self.rectZoom.oriented().width() / self.width())
def roiReset(self):
self.rectRoi = param.Rect(0, 0, param.width(), param.height())
self.gui.updateRoiText()
self.update()
if self.gui.cfg is None:
self.gui.dumpConfig()
def roiSet(self, x, y, w, h, rel=False):
self.rectRoi = param.Rect(x, y, w, h, rel=rel)
self.gui.updateRoiText()
self.update()
if self.gui.cfg is None:
self.gui.dumpConfig()