155 lines
4.9 KiB
Python
155 lines
4.9 KiB
Python
###################################################################################################
|
|
# Example of using ImageJ functionalities through ijutils.
|
|
###################################################################################################
|
|
|
|
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
|
|
|
|
|
|
"""
|
|
def detect_led(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)
|
|
"""
|
|
|
|
roi_center = (2322, 1025)
|
|
roi_radius = 700
|
|
integration_count = 10
|
|
integration_continuous = False
|
|
integration_partial = False
|
|
frames = []
|
|
|
|
|
|
|
|
|
|
"""
|
|
def in_roi(x,y):
|
|
global roi_center, roi_radius
|
|
return math.hypot(x-roi_center[0], y-roi_center[1]) < roi_radius
|
|
|
|
def detect_led(ip):
|
|
aux = grayscale(ip, in_place=False)
|
|
invert(aux)
|
|
#subtract_background(aux)
|
|
auto_threshold(aux)
|
|
binary_open(aux)
|
|
(results,output) = analyse_particles(aux, 800,4000,
|
|
fill_holes = True, exclude_edges = True,print_table=False,
|
|
output_image = "outlines", minCirc = 0.4
|
|
, maxCirc = 1.0)
|
|
r=results
|
|
print "\n"
|
|
print r.getColumnHeadings()
|
|
for row in range (r.counter):
|
|
if in_roi(r.getValue("XM",row), r.getValue("YM",row)):
|
|
print r.getRowAsString(row)
|
|
else:
|
|
#print >> sys.stderr, 'Invalid location:' + str(r.getValue("XM", row)) + ", " + str( r.getValue("YM", row))
|
|
print >> sys.stderr, r.getRowAsString(row)
|
|
return (results,output)
|
|
|
|
class MyFilter(Filter):
|
|
def process(self, image, data):
|
|
ip = load_image(image)
|
|
(results,output) = detect_led(ip)
|
|
invert(output)
|
|
op_image(ip, output, "xor")
|
|
return ip.getBufferedImage()
|
|
|
|
"""
|
|
|
|
|
|
|
|
def in_roi(x,y):
|
|
return math.hypot(x-roi_radius, y-roi_radius) < roi_radius
|
|
|
|
|
|
def integrate (ips):
|
|
for i in range(len(ips)):
|
|
if i==0:
|
|
aux = new_image(2* roi_radius, 2*roi_radius, image_type="float", title = "sum", fill_color = None)
|
|
op_image(aux, ips[i], "add", float_result=True, in_place=True)
|
|
op_const(aux, "divide", len(ips), in_place=True)
|
|
return aux
|
|
|
|
last_ret = (None, None)
|
|
|
|
def detect_led(ip):
|
|
global roi_center, roi_radius, integration_count, integration_continuous, integration_partial, frames
|
|
global count , last_ret
|
|
aux = sub_image(ip, roi_center[0] - roi_radius, roi_center[1] - roi_radius, 2* roi_radius, 2*roi_radius)
|
|
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)
|
|
auto_threshold(aux)
|
|
#binary_open(aux)
|
|
(results,output) = analyse_particles(aux, 400,4000,
|
|
fill_holes = True, exclude_edges = True,print_table=False,
|
|
output_image = "outlines", minCirc = 0.4
|
|
, maxCirc = 1.0)
|
|
r=results
|
|
|
|
"""
|
|
print "\n"
|
|
print r.getColumnHeadings()
|
|
for row in range (r.counter):
|
|
if in_roi(r.getValue("XM",row), r.getValue("YM",row)):
|
|
print r.getRowAsString(row)
|
|
else:
|
|
#print >> sys.stderr, 'Invalid location:' + str(r.getValue("XM", row)) + ", " + str( r.getValue("YM", row))
|
|
print >> sys.stderr, r.getRowAsString(row)
|
|
"""
|
|
points = ""
|
|
for row in range (r.counter):
|
|
points = points + " (" + str(int(r.getValue("XM", row))) + ", " + str(int(r.getValue("YM", row))) + ")"
|
|
print str(r.counter) + " - " + points
|
|
|
|
last_ret = (results,output)
|
|
if not integration_continuous:
|
|
frames = []
|
|
return (results,aux)
|
|
#return (results,output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyFilter(Filter):
|
|
def process(self, image, data):
|
|
global roi_center, roi_radius
|
|
ip = load_image(image)
|
|
(results,output) = detect_led(ip)
|
|
if output is not None:
|
|
invert(output)
|
|
output = pad_image(output, roi_center[0] - roi_radius, 0, roi_center[1] - roi_radius, 0)
|
|
op_image(ip, output, "xor")
|
|
return ip.getBufferedImage()
|
|
|
|
#Setting the filter to a source
|
|
img.setFilter(MyFilter())
|
|
|