This commit is contained in:
gac-S_Changer
2018-05-22 16:07:56 +02:00
parent 5114bfaf3d
commit 05f982c86b
26 changed files with 595 additions and 221 deletions
+36 -2
View File
@@ -16,16 +16,48 @@ img.config.rotationCrop=True
"""
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)
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)
@@ -45,7 +77,8 @@ def rotate(x,y, degrees):
y = oy * math.cos(rotation) + ox * math.sin(rotation) + rh / 2;
return x,y
p.addOverlay(ov_text)
set_led_state(True)
try:
ov_text.update("Click on the center of C4 (19) position...")
p.refresh()
@@ -89,13 +122,14 @@ try:
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
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()
finally:
set_led_state(False)
p.removeOverlay(ov_text)
img.refresh()
+79
View File
@@ -0,0 +1,79 @@
###################################################################################################
# Procedure to detect the cover orientation
###################################################################################################
#Parameters
FRAMES_INTEGRATION = 3
STEP_SIZE = 2
POSITION_NAMES = [ 'A','B','C','D', 'E', 'F']
POSITION_ANGLES = [330, 30, 90, 150, 210, 270]
POSITION_TOLERANCE = 10
MINIMUM_CONFIDENCE = 10
DEBUG = True
PLOT = True
REFERENCE_IMG = "ref2"
#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")
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)
#Show ROI of pre-processed image
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(ip, r, "correlate")
bi = op.getBufferedImage()
p = integrateVertically(bi)
ydata.append(sum(p))
#image_panel = show_panel(op.bufferedImage)
#time.sleep(0.001)
#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)
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 PLOT:
p = plot(ydata, xdata=xdata)[0]
#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 "Confifdence: " , confidence
#Set return value
set_return ([position, angle, confidence])
+61 -45
View File
@@ -2,77 +2,93 @@
# Procedure to detect the puck light spots.
###################################################################################################
COVER_PRESENT = True
ROOM_TEMP = False
room_temp = False
number_frames = 10
number_backgrounds = 10
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 = "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_range(room_temp)
img.backgroundEnabled=False
set_led_state(False)
time.sleep(0.1)
img.waitNext(100)
time.sleep(led_latency)
img.waitNext(2000)
background = average_frames(number_backgrounds)
#img.backgroundImage=background.bufferedImage
#img.captureBackground(1,0)
#show_panel(img.backgroundImage)
#img.backgroundEnabled = True
set_led_state(True)
time.sleep(0.1)
img.waitNext(100)
time.sleep(led_latency)
img.waitNext(2000)
image = average_frames(number_frames)
set_led_state(False)
op_image(image, background, "subtract", float_result=True, in_place=True)
renderer = show_panel(image.getBufferedImage())
renderer.clearOverlays()
if RENDERER is None:
RENDERER = show_panel(image.getBufferedImage())
else:
RENDERER.setImage(None, image.getBufferedImage(), None)
RENDERER.clearOverlays()
invert(image)
auto_threshold(image, method = threshold_method) #Tested ok: MaxEntropy, Triangle, Yen
#renderer = show_panel(image.getBufferedImage())
#binary_open(aux)
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 = ""
npoints = 0
x=[]
y=[]
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))+roi[0], int(r.getValue("YM", row))+roi[1]
x.append(int(r.getValue("XM", row)))
y.append(int(r.getValue("YM", row)))
points = points + " (" + str(x[-1]) + ", " + str(y[-1]) + ")"
npoints = npoints + 1
renderer.addOverlay(Crosshairs(Pen(java.awt.Color.MAGENTA), java.awt.Point(x[-1],y[-1]), java.awt.Dimension(15,15)))
print str(npoints) + " - " + points
#print r
#print x
#print y
offset = int(math.sqrt(1000)/2)
cv = (min(x)-offset, min(y)-offset, max(x)+offset, max(y)+offset)
renderer.addOverlay(Rect(Pen(java.awt.Color.MAGENTA), java.awt.Point(cv[0], cv[1]), java.awt.Dimension(cv[2]-cv[0],cv[3]-cv[1])))
#show_panel(output.getBufferedImage())
#img.backgroundEnabled=False
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])
RENDERER.addOverlay(Crosshairs(Pen(java.awt.Color.MAGENTA), java.awt.Point(x,y), java.awt.Dimension(15,15)))
#RENDERER.addOverlays(ovs)
clear_detection(block_id)
detect_pucks(points, block_id)
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)
+23 -7
View File
@@ -7,9 +7,17 @@
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/CoverDetection2")
return position
def in_roi(x,y):
return math.hypot(x-roi_radius, y-roi_radius) < roi_radius
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):
@@ -40,18 +48,26 @@ def integrate_frames(samples = 1):
return integrate(grab_frames(samples))
roi_center = (800, 600)
roi_center = (600, 600) #(800, 600)
roi_radius = 600
roi_border = 30
def get_roi():
return (roi_center[0] - roi_radius, roi_center[1] - roi_radius, 2* roi_radius, 2*roi_radius)
#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.image)
ret = sub_image(ip, roi[0], roi[1], roi[2], roi[3])
grayscale(ret, do_scaling=True)
#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):