114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
|
|
###################################################################################################
|
|
# 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)
|