Files
tell/script/imgproc/CameraCalibration.py
gac-S_Changer bdf5049f96 Creation
2018-12-03 12:17:40 +01:00

155 lines
4.6 KiB
Python

import ch.psi.pshell.device.Camera as Camera
import ch.psi.pshell.imaging.RendererMode as RendererMode
import ch.psi.pshell.imaging.Calibration as Calibration
from ch.psi.pshell.imaging.Overlays import *
import ch.psi.utils.swing.SwingUtils as SwingUtils
import javax.swing.SwingUtilities as SwingUtilities
from swingutils.threads.swing import callSwing
#SIMULATION = ch.psi.pshell.imaging.FileSource
"""
img.camera.setColorMode(Camera.ColorMode.Mono)
img.camera.setDataType(Camera.DataType.UInt8)
img.camera.setGrabMode(Camera.GrabMode.Continuous)
img.camera.setTriggerMode(Camera.TriggerMode.Fixed_Rate)
img.camera.setExposure(50.00)
img.camera.setAcquirePeriod(200.00)
img.camera.setGain(0.0)
img.config.rotationCrop=True
"""
MOVE_HEXIPOSI = True
ROTATION_OFFSET = 180.0
if MOVE_HEXIPOSI:
release_safety() #enable_motion()
sensor_width,sensor_height = img.camera.getSensorSize()
img.camera.setROI(0, 0,sensor_width, sensor_height)
img.config.rotation=0
img.config.roiX,img.config.roiY, img.config.roiWidth,img.config.roiHeight =0,0,-1,-1
img.config.setCalibration(None)
img.camera.stop()
img.camera.start()
p = show_panel(img)
dlg = SwingUtilities.getWindowAncestor(p)
dlg.setSize(800,800)
frm=SwingUtils.getFrame(p)
dlg.setLocationRelativeTo(frm)
p.setMode(RendererMode.Fit)
ov_text = Text(Pen(java.awt.Color.GREEN.darker()), "", java.awt.Font("Verdana", java.awt.Font.PLAIN, 24), java.awt.Point(20,20))
ov_text.setFixed(True)
p.addOverlay(ov_text)
try:
#Find image center and Prosilica ROI
ov_text.update("Click on the center of the Dewar...")
p.refresh()
dc = p.waitClick(60000)
print dc
width, height = min(dc.x, sensor_width-dc.x)*2, min(dc.y, sensor_height-dc.y)*2
width, height = width - width%16, height - height%16
width, height = min(width,1000), min(height,1000)
print width, height
roi_x = int(dc.x- width/2)
roi_y = int(dc.y- height/2)
roi_w = int(width)
roi_h = int(height)
set_setting("roi_x", roi_x)
set_setting("roi_y", roi_y)
set_setting("roi_w", roi_w)
set_setting("roi_h", roi_h)
img.camera.setROI(roi_x, roi_y, width, height)
except:
img.camera.setROI(int(get_setting("roi_x")), int(get_setting("roi_y")), int(get_setting("roi_w")), int(get_setting("roi_h")))
finally:
img.camera.stop()
img.camera.start()
#Configure source
CC4 = (-129.9, -150)
CD5 = (129.9, -150)
CA5 = (-129.9, 150)
CF4 = (129.9, 150)
DX = 259.8
DY = 300.0
ROI_X = 470.0
ROI_Y = 470.0
def rotate(x,y, degrees):
rotation = math.radians(degrees)
rw, rh = img.getImage().getWidth(), img.getImage().getHeight()
ox, oy = x - (rw / 2), y - (rh / 2)
x = ox * math.cos(rotation) - oy * math.sin(rotation) + rw / 2;
y = oy * math.cos(rotation) + ox * math.sin(rotation) + rh / 2;
return x,y
set_led_state(True)
try:
if MOVE_HEXIPOSI: set_hexiposi("C")
ov_text.update("Click on the center of C4 (19) position...")
p.refresh()
pc4 = p.waitClick(60000)
print pc4
if MOVE_HEXIPOSI: set_hexiposi("D")
ov_text.update("Click on the center of D5 (13) position...")
p.refresh()
pd5 = p.waitClick(60000)
print pd5
if MOVE_HEXIPOSI: set_hexiposi("F")
ov_text.update("Click on the center of F4 (04) position...")
p.refresh()
pf4 = p.waitClick(60000)
print pf4
if MOVE_HEXIPOSI: set_hexiposi("A")
ov_text.update("Click on the center of A5 (28) position...")
p.refresh()
pa5 = p.waitClick(60000)
print pa5
vc1x, vc1y, vc2x, vc2y = (pc4.x + pd5.x )/2.0, (pc4.y + pd5.y )/2.0, (pa5.x + pf4.x )/2.0, (pa5.y + pf4.y )/2.0
hc1x, hc1y, hc2x, hc2y = (pc4.x + pa5.x )/2.0, (pc4.y + pa5.y )/2.0, (pd5.x + pf4.x )/2.0, (pd5.y + pf4.y )/2.0
cx, cy = (vc1x + vc2x)/2, (hc1y + hc2y)/2
a1 = math.degrees(math.atan((cx-vc1x)/(vc1y-cy)))
a2 = math.degrees(math.atan((cx-vc2x)/(vc2y-cy)))
a = (a1+a2)/2
dy = math.hypot(vc2y - vc1y, vc2x - vc1x)
dx = math.hypot(hc2x - hc1x, hc2y - hc1y)
print dy, dx, cx, cy
sx, sy = DX/dx, DY/dy
#Rotating center of puck
rcx, rcy = rotate(cx, cy, -a)
roi_w, roi_h = int(ROI_X / sx), int(ROI_Y / sy)
roi_x, roi_y = int(rcx-roi_w/2), int(rcy-roi_h/2)
print a, sx, sy, roi_w, roi_h
img.config.rotation=-a + ROTATION_OFFSET
img.config.roiX,img.config.roiY, img.config.roiWidth,img.config.roiHeight = roi_x, roi_y, roi_w, roi_h
img.config.setCalibration(Calibration(sx, sy, -roi_w/2, -roi_h/2))
img.config.save()
set_return ("Success calibrating the camera")
finally:
set_led_state(False)
p.removeOverlay(ov_text)
img.refresh()