This commit is contained in:
gac-S_Changer
2020-09-08 11:53:11 +02:00
parent 226db0ad02
commit c85d4b4aa9
251 changed files with 19362 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
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.pshell.imaging.Pen as Pen
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 = not is_manual_mode()
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()
+98
View File
@@ -0,0 +1,98 @@
###################################################################################################
# Procedure to detect the cover orientation
###################################################################################################
assert_imaging_enabled()
#Parameters
FRAMES_INTEGRATION = 3
STEP_SIZE = 2
POSITION_NAMES = [ 'A','B','C','D', 'E', 'F']
#POSITION_ANGLES = [ 330, 30, 90, 150, 210, 270 ]
POSITION_ANGLES = [ 0, 60, 120, 180, 240, 300 ]
POSITION_TOLERANCE = 3
MINIMUM_CONFIDENCE = 3
DEBUG = cover_detection_debug
#REFERENCE_IMG = "ref2"
REFERENCE_IMG = "ref1"
BORDER = 7
#Load reference image
ref = load_image(str("{images}/cover/" + REFERENCE_IMG + ".png") , title="Line")
#Pre-process camera image
#ip = load_image("{images}/cover/Cover_000" + str(index) + ".png", title="Img")
ip = integrate_frames(FRAMES_INTEGRATION)
ip = grayscale(ip, True)
smooth(ip)
#bandpass_filter(ip, 30, 1000)
edges(ip)
auto_threshold(ip, method = "MaxEntropy")
#binary_erode(ip, True)
#binary_dilate(ip, True)
ip.getProcessor().erode(1, 255)
cx,cy = int(ip.width/2), int(ip.height/2)
ip = sub_image(ip, cx-ref.width/2, cy-ref.height/2, ref.width, ref.height)
if BORDER>0:
sip = sub_image(ip, BORDER,BORDER, ref.width-2*BORDER, ref.height-2*BORDER)
ip = pad_image(sip, BORDER, BORDER, BORDER, BORDER, fill_color=Color.WHITE)
#Show ROI of pre-processed image
if DEBUG:
image_panel = show_panel(ip.bufferedImage)
#Calculate correlation between image and reference, rotating the reference from 0 to 360
import ch.psi.pshell.imaging.Utils.integrateVertically as integrateVertically
ydata = []
xdata = range (0,360,STEP_SIZE)
for i in xdata:
r = ref.duplicate()
r.getProcessor().setBackgroundValue(0.0)
r.getProcessor().rotate(float(i))
op = op_fft(r, ip, "correlate")
bi = op.getBufferedImage()
p = integrateVertically(bi)
ydata.append(sum(p))
#Calculate angle of the highest correlation, and confidence level
peaks = estimate_peak_indexes(ydata, xdata, (min(ydata) + max(ydata))/2, 25.0)
peaks_x = map(lambda x:xdata[x], peaks)
peaks_y = map(lambda x:ydata[x], peaks)
if len(peaks_x) > 1:
#remoce close peaks between 350 deg and 10 deg
if ((peaks_x[0]<10) and (peaks_x[1]>350)) or ((peaks_x[1]<10) and (peaks_x[0]>350)):
peaks.pop(1)
peaks_x.pop(1)
peaks_y.pop(1)
confidence = None if len(peaks_x)<2 else int(((float(peaks_y[0])/peaks_y[1])-1) * 1000)
angle = (None if len(peaks_x)==0 else peaks_x[0])
#From angle and confidence level estimate hexiposi position
position = None
if angle is not None:
for i in range(len(POSITION_NAMES)):
if abs(POSITION_ANGLES[i] - angle) <= POSITION_TOLERANCE:
position = POSITION_NAMES[i]
#Plot the correlations values agains angle
if DEBUG:
plot(ydata, xdata=xdata)
#Output results
if DEBUG:
print "Peaks", peaks
print "Peak indexes: " + str(peaks_x)
print "Peak values: " + str(peaks_y)
print "Angle: " , angle
print "Position: " , position
print "Confidence: " , confidence
#Set return value
set_return ([position, angle, confidence])
@@ -0,0 +1,40 @@
#Parameters
FRAMES_INTEGRATION = 3
MINIMUM_CONFIDENCE = 10
DEBUG = cover_detection_debug
REFERENCE_IMG = "ref1"
ERODE_ITERATIONS = 2
#Load reference image
SIZE = [128,128]
BORDER = 7
hexiposi.move("A")
#Pre-process camera image
#ip = load_image("{images}/cover/Cover_000" + str(index) + ".png", title="Img")
ip = integrate_frames(FRAMES_INTEGRATION)
ip = grayscale(ip, True)
smooth(ip)
#bandpass_filter(ip, 30, 1000)
edges(ip)
auto_threshold(ip, method = "MaxEntropy")
#binary_dilate(ip, True, 2)
for i in range(ERODE_ITERATIONS):
ip.getProcessor().erode(1, 255)
cx,cy = int(ip.width/2), int(ip.height/2)
ip = sub_image(ip, cx-SIZE[0]/2, cy-SIZE[1]/2, SIZE[0], SIZE[1])
invert(ip)
ip = grayscale(ip, True)
#smooth(ip)
if BORDER > 0:
sip = sub_image(ip, BORDER,BORDER, SIZE[0]-2*BORDER, SIZE[1]-2*BORDER)
ip = pad_image(sip, BORDER, BORDER, BORDER, BORDER)
if DEBUG:
image_panel = show_panel(ip.bufferedImage)
save_image(ip, str("{images}/cover/" + REFERENCE_IMG + ".png") ,"png")
+29
View File
@@ -0,0 +1,29 @@
mask_img = new_image(img.getOutput().getWidth(), img.getOutput().getHeight(), image_type="byte", title = "mask_img", fill_color = Color.BLACK)
mask_radius = 14
mask_points = []
def to_img_coords(absolute_coords):
return [img.getCalibration().convertToImageX(absolute_coords[0]), img.getCalibration().convertToImageY(absolute_coords[1])]
for p in _puck_list:
mask_points.append(to_img_coords(p.led_mini))
mask_points.append(to_img_coords(p.led_uni))
i = mask_img.getBufferedImage()
for p in mask_points:
#i.setRGB(p[0], p[1], 0xFFFFFF)
for x in range (p[0]-mask_radius, p[0]+mask_radius):
for y in range (p[1]-mask_radius, p[1]+mask_radius):
if math.hypot(x-p[0], y-p[1]) <= mask_radius:
i.setRGB(x,y, 0xFFFFFF)
mask_img = load_image(i)
#show_panel( mask_img.getBufferedImage())
set_return(mask_img)
+102
View File
@@ -0,0 +1,102 @@
###################################################################################################
# Example of using ImageJ functionalities through ijutils.
###################################################################################################
import datetime
from ijutils import *
import java.awt.Color as Color
import ch.psi.pshell.imaging.Filter as Filter
from ch.psi.pshell.imaging.Overlays import *
import ch.psi.pshell.imaging.Pen as Pen
integration_count = 10
integration_continuous = False
integration_partial = False
frames = []
roi = get_roi()
color_roi = Color(0, 128, 0)
renderer = show_panel(img)
renderer.clearOverlays()
ov_roi_shape = Ellipse(Pen(color_roi, 0,), java.awt.Point(roi[0], roi[1]), java.awt.Dimension(roi[2], roi[3]))
ov_roi_bound = Rect(Pen(color_roi, 0, Pen.LineStyle.dotted), java.awt.Point(roi[0], roi[1]), java.awt.Dimension(roi[2], roi[3]))
ov_roi_center = Crosshairs(Pen(color_roi, 0), java.awt.Point(roi_center[0],roi_center[1]), java.awt.Dimension(15,15))
renderer.addOverlays([ov_roi_shape, ov_roi_bound,ov_roi_center])
last_ret = (None, None)
def detect_led(ip):
roi = get_roi()
global roi_center, roi_radius, integration_count, integration_continuous, integration_partial, frames
global count , last_ret
aux = sub_image(ip, roi[0], roi[1], roi[2], roi[3])
grayscale(aux)
#gaussian_blur(aux)
if (integration_count>1):
frames.append(aux)
if len(frames) >integration_count:
del frames[0]
if not integration_continuous:
if (len(frames)< integration_count):
if last_ret[1] is not None: invert(last_ret[1])
return last_ret
if (not integration_partial) and len(frames) <integration_count:
return last_ret
aux = integrate(frames)
#aux = get_channel(aux, "blue")
invert(aux)
#subtract_background(aux)
#Tested ok: Huang, Mean, MaxEntropy, Percentile, Triangle, Yen
auto_threshold(aux, method = "Percentile")
#binary_open(aux)
(results,output) = analyse_particles(aux, 250,1000,
fill_holes = True, exclude_edges = False, print_table=False,
output_image = "outlines", minCirc = 0.3
, maxCirc = 1.0)
r=results
points = ""
npoints = 0
for row in range (r.counter):
if in_roi(r.getValue("XM",row), r.getValue("YM",row)):
points = points + " (" + str(int(r.getValue("XM", row))+roi[0]) + ", " + str(int(r.getValue("YM", row))+roi[1]) + ")"
npoints = npoints + 1
print str(npoints) + " - " + points
last_ret = (results,output)
if not integration_continuous:
frames = []
#if npoints!=12:
# save_image(op_image(aux, output,"xor", in_place=False), "{images}/" + str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))+".png", "png")
#return (results,aux)
return (results,output)
ip = None
class MyFilter(Filter):
def process(self, image, data):
global roi_center, roi_radius, ip
ip = load_image(image)
(results,output) = detect_led(ip)
if output is not None:
invert(output)
output = pad_image(output, roi[0], 0,roi[1], 0)
op_image(ip, output, "xor")
return ip.getBufferedImage()
#Setting the filter to a source
img.setFilter(MyFilter())
+106
View File
@@ -0,0 +1,106 @@
###################################################################################################
# Procedure to detect the puck light spots.
###################################################################################################
assert_imaging_enabled()
COVER_PRESENT = True
ROOM_TEMP = is_room_temp()
USE_MASK = True
if get_exec_pars().source == CommandSource.ui:
PLOT = None
RENDERER = None
TEXT = None
if COVER_PRESENT:
cover_position = hexiposi.readback.take()
if (cover_position is None) or (cover_position == "Unknown"):
raise Exception("Unknown cover position")
else:
block_id = cover_position.upper()[0]
else:
block_id = None
print "Block id: ", block_id
number_frames = 5 if ROOM_TEMP else 10
number_backgrounds = 5 if ROOM_TEMP else 5
minimum_size = 78 # r = 5 # 150
maximum_size = 750 # r = 15 #1500
min_circ = 0.2
threshold_method = "MaxEntropy" if ROOM_TEMP else "Default" #Apparently good for LN2: Default, Intermodes, IsoData, Otsu
threshold_method,threshold_range = "Manual", (0, 215)
exclude_edges = True
led_latency = 0.5 #0.1
set_led_state(False)
time.sleep(led_latency)
img.waitNext(2000)
background = average_frames(number_backgrounds)
#background = integrate_frames(number_backgrounds)
set_led_state(True)
time.sleep(led_latency)
img.waitNext(2000)
image = average_frames(number_frames)
#image = integrate_frames(number_frames)
set_led_state(False)
op_image(image, background, "subtract", float_result=True, in_place=True)
image=grayscale(image)
if RENDERER is not None and RENDERER.isShowing():
RENDERER.setImage(None, image.getBufferedImage(), None)
else:
RENDERER = show_panel(image.getBufferedImage())
RENDERER.clearOverlays()
if USE_MASK:
mask_img = run("imgproc/CreateMask")
#mask_img=grayscale(mask_img)
#show_panel( mask_img.getBufferedImage())
op_image(image, mask_img, "and", float_result=False, in_place=True)
RENDERER.setImage(None, image.getBufferedImage(), None)
invert(image)
if threshold_method == "Manual":
threshold(image, threshold_range[0], threshold_range[1])
else:
auto_threshold(image, method = threshold_method) #Tested ok: MaxEntropy, Triangle, Yen
(r,output) = analyse_particles(image, minimum_size,maximum_size,
fill_holes = True, exclude_edges = exclude_edges, print_table=False,
output_image = "outlines", minCirc = min_circ
, maxCirc = 1.0)
points = []
for row in range (r.counter):
if in_roi(r.getValue("XM",row), r.getValue("YM",row)):
x, y = int(r.getValue("XM", row)), int(r.getValue("YM", row))
cx, cy = img.getCalibration().convertToAbsoluteX(x), img.getCalibration().convertToAbsoluteY(y)
points.append([cx,cy])
if RENDERER is not None:
RENDERER.addOverlay(Crosshairs(Pen(java.awt.Color.MAGENTA), java.awt.Point(x,y), java.awt.Dimension(15,15)))
clear_detection(block_id)
detect_pucks(points, block_id)
if PLOT is not None:
plot_base_plate(points, p=PLOT)
ret = get_puck_detection_dict(block_id)
if TEXT is not None:
TEXT.setText(str(ret))
set_return(ret)
+113
View File
@@ -0,0 +1,113 @@
###################################################################################################
# Image processing utilities
###################################################################################################
from ijutils import *
from ch.psi.pshell.imaging.Overlays import *
import ch.psi.pshell.imaging.Pen as Pen
import java.awt.Rectangle as Rectangle
def get_img_cover_pos():
[position, angle, confidence] = run("imgproc/CoverDetection")
return position
def assert_img_in_cover_pos(pos = None):
if pos==None:
pos = hexiposi.take()
elif type(pos) is int:
pos = chr( ord('A') + (pos-1))
elif is_string(pos):
pos = pos.upper()
img_segment = get_img_cover_pos()
if img_segment != pos:
raise Exception ("Image detection of cover does not match position: " + str(img_segment))
def in_roi(x,y):
global roi_center, roi_radius, roi_border
return math.hypot(x-roi_center[0], y-roi_center[1]) < (roi_radius-roi_border)
def integrate(ips):
roi = get_roi()
aux = None
for i in range(len(ips)):
if i==0:
aux = new_image(roi[2], roi[3], image_type="float", title = "sum", fill_color = None)
op_image(aux, ips[i], "add", float_result=True, in_place=True)
return aux
def average (ips):
aux = integrate(ips)
op_const(aux, "divide", len(ips), in_place=True)
return aux
def grab_frames(samples):
frames = []
for i in range(samples):
aux = get_image()
frames.append(aux)
return frames
def average_frames(samples = 1):
return average(grab_frames(samples))
def integrate_frames(samples = 1):
return integrate(grab_frames(samples))
roi_center = (600, 600) #(800, 600)
roi_radius = 600
roi_border = 30
def get_roi():
#roi_center = (img.output.width/2, img.output.height/2)
#roi_radius = min(roi_center[0], roi_center[1])
#return (roi_center[0] - roi_radius, roi_center[1] - roi_radius, 2* roi_radius, 2*roi_radius)
global roi_center, roi_radius
roi_center = (img.output.width/2, img.output.height/2)
roi_radius = min(roi_center[0], roi_center[1])
return (0,0,img.output.width, img.output.height)
def get_image():
roi = get_roi()
#ip = load_image(img.output)
#ret = ip if (roi is None) else sub_image(ip, roi[0], roi[1], roi[2], roi[3])
#grayscale(ret, do_scaling=True)
ret = load_image(Utils.grayscale(img.output, Rectangle(roi[0], roi[1], roi[2], roi[3]) if (roi is not None) else None))
return ret
#def detect_pucks(ip):
# """
# """
# aux = grayscale(ip, in_place=False)
# threshold(aux,0,50)
# binary_fill_holes(aux)
# return analyse_particles(aux, 10000,50000,
# fill_holes = False, exclude_edges = True,print_table=True,
# output_image = "outlines", minCirc = 0.4, maxCirc = 1.0)
#
#def detect_samples(ip):
# """
# """
# aux = grayscale(ip, in_place=False)
# invert(aux)
# subtract_background(aux)
# auto_threshold(aux)
# binary_open(aux)
# return analyse_particles(aux, 250,1000,
# fill_holes = False, exclude_edges = True,print_table=True,
r,g,b = [0]*256,[0]*256,[0]*256
b[0]=0xFF
b[1]=0xFF ; g[1] = 0x80; r[1] = 0x80
outline_lut1 = (r,g,b)
r,g,b = [0]*256,[0]*256,[0]*256
g[0]=0x80;r[0]=0x80;
g[1]=0xFF ; r[1] = 0x80; b[1] = 0x80
outline_lut2 = (r,g,b)