Files
mxsc/script/imgproc/LedDetectionProc.py
gac-S_Changer 70a11a6e39
2018-06-14 17:33:20 +02:00

96 lines
2.7 KiB
Python

###################################################################################################
# Procedure to detect the puck light spots.
###################################################################################################
COVER_PRESENT = True
ROOM_TEMP = is_room_temp()
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 10
minimum_size = 150
maximum_size = 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)
if RENDERER is not None:
RENDERER.setImage(None, image.getBufferedImage(), None)
else:
RENDERER = show_panel(image.getBufferedImage())
RENDERER.clearOverlays()
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)