Skip to content

Commit 3f4db9a

Browse files
committed
qtvcp -camview: - add ability to fix aspect ratio distrortion
user has a camera that has distorted X vrs Y making it hard to align round things. Now on can adjust X and Y scale separately.
1 parent 2bfffa6 commit 3f4db9a

1 file changed

Lines changed: 72 additions & 11 deletions

File tree

lib/python/qtvcp/widgets/camview_widget.py

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import numpy as np
5353

5454

55+
5556
class CamView(QtWidgets.QWidget, _HalWidgetBase):
5657
def __init__(self, parent=None):
5758
super(CamView, self).__init__(parent)
@@ -64,6 +65,8 @@ def __init__(self, parent=None):
6465
self.rotation = 0
6566
self.rotationIncrement = .5
6667
self.scale = 1
68+
self.scaleX = 1.0
69+
self.scaleY = 1.0
6770
self.gap = 5
6871
self._noRotate = False
6972
self.setWindowTitle('Cam View')
@@ -81,15 +84,21 @@ def __init__(self, parent=None):
8184
self.pix = None
8285
self.stopped = False
8386
self.degree = str("\N{DEGREE SIGN}")
84-
if INFO.PROGRAM_PREFIX is not None:
85-
self.user_path = os.path.expanduser(INFO.PROGRAM_PREFIX)
86-
else:
87+
88+
# trap so can run script directly to test
89+
try:
90+
if INFO.PROGRAM_PREFIX is not None:
91+
self.user_path = os.path.expanduser(INFO.PROGRAM_PREFIX)
92+
except:
8793
self.user_path = (os.path.join(os.path.expanduser('~'), 'linuxcnc/nc_files'))
8894

8995
#self.blobInit()
9096

9197
def _hal_init(self):
92-
self.pin_ = self.HAL_GCOMP_.newpin('cam-rotation',hal.HAL_FLOAT, hal.HAL_OUT)
98+
try:
99+
self.pin_ = self.HAL_GCOMP_.newpin('cam-rotation',hal.HAL_FLOAT, hal.HAL_OUT)
100+
except:
101+
pass
93102
if LIB_GOOD:
94103
STATUS.connect('periodic', self.nextFrameSlot)
95104

@@ -139,6 +148,16 @@ def mouseDoubleClickEvent(self, event):
139148
elif event.button() & QtCore.Qt.MiddleButton:
140149
self.diameter = 20
141150

151+
def zoom_in(self):
152+
if self.scale >= 5:
153+
return
154+
self.scale += 0.1
155+
156+
def zoom_out(self, event):
157+
if self.scale <= 1:
158+
return
159+
self.scale -= 0.1
160+
142161
def limitChecks(self):
143162
w = self.size().width()
144163
if self.diameter < 2: self.diameter = 2
@@ -194,19 +213,18 @@ def makeImage(self, image, qFormat=QImage.Format_RGB888):
194213
def makeCVImage(self, frame):
195214
CV.imshow('CV Image',frame)
196215

197-
def rescaleFrame(self, frame, scale =1.5):
198-
width = int(frame.shape[1] * scale)
199-
height = int(frame.shape[0] * scale)
200-
dims = (width,height)
201-
return CV.resize(frame, dims, interpolation=CV.INTER_CUBIC)
216+
def rescaleFrame(self, frame, scale = 1, scale_x =1.0, scale_y=1.0):
217+
x = scale_x * scale
218+
y = scale_y * scale
219+
return CV.resize(frame, None, fx = x, fy = y, interpolation=CV.INTER_CUBIC)
202220

203221
def zoom(self, frame, scale):
204222
# get original size of image
205223
(oh, ow) = frame.shape[:2]
206224
#############################
207225
# scale image
208226
#############################
209-
frame = self.rescaleFrame(frame, scale)
227+
frame = self.rescaleFrame(frame, scale,self.scaleX,self.scaleY)
210228
##########################
211229
# crop to the original size of the frame
212230
# measure from center, so we zoom on center
@@ -499,11 +517,54 @@ def list_ports(self):
499517
dev_port +=1
500518
return available_ports,working_ports,non_working_ports
501519

520+
class CamAngle(CamView):
521+
def __init__(self, parent=None):
522+
super(CamAngle, self).__init__(parent)
523+
524+
def mouseDoubleClickEvent(self, event):
525+
if event.button() & QtCore.Qt.LeftButton:
526+
# zoom
527+
self.scale = 1
528+
elif event.button() & QtCore.Qt.MiddleButton:
529+
self.diameter = 40
530+
531+
def wheelEvent(self, event):
532+
mouse_state = QtWidgets.qApp.mouseButtons()
533+
size = self.size()
534+
w = size.width()
535+
if event.angleDelta().y() < 0:
536+
if mouse_state == QtCore.Qt.NoButton:
537+
self.diameter -= 2
538+
if mouse_state == QtCore.Qt.LeftButton:
539+
self.scale -= .1
540+
else:
541+
if mouse_state == QtCore.Qt.NoButton:
542+
self.diameter += 2
543+
if mouse_state == QtCore.Qt.LeftButton:
544+
self.scale += .1
545+
if self.diameter < 2: self.diameter = 2
546+
if self.diameter > w: self.diameter = w
547+
if self.scale < 1: self.scale = 1
548+
if self.scale > 5: self.scale = 5
549+
550+
def drawText(self, event, qp):
551+
size = self.size()
552+
w = size.width()
553+
h = size.height()
554+
qp.setPen(self.text_color)
555+
qp.setFont(self.font)
556+
if self.pix:
557+
angle = 0.0 if self.rotation == 0 else 360 - self.rotation
558+
qp.drawText(self.rect(), QtCore.Qt.AlignTop, '{:0.3f}{}'.format(angle, self.degree))
559+
else:
560+
qp.drawText(self.rect(), QtCore.Qt.AlignCenter, self.text)
561+
562+
502563
if __name__ == '__main__':
503564

504565
import sys
505566
app = QtWidgets.QApplication(sys.argv)
506-
capture = CamView()
567+
capture = CamAngle()
507568
capture.show()
508569

509570
def jump():

0 commit comments

Comments
 (0)