91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
import ij
|
|
from ij.io import Opener
|
|
from ijutils import *
|
|
from ch.psi.pshell.imaging.Overlays import *
|
|
from ch.psi.pshell.imaging.Utils import *
|
|
import ch.psi.pshell.imaging.Pen as Pen
|
|
import java.awt.Rectangle as Rectangle
|
|
import ch.psi.pshell.imaging.Data as Data
|
|
|
|
|
|
###############################################################################
|
|
# ROI Integration
|
|
###############################################################################
|
|
|
|
def integrate_roi(source, x,y, w, h):
|
|
if source.data is None:
|
|
source.update()
|
|
roi = source.data.getRoi(Rectangle(x,y, w, h))
|
|
return sum(roi.integrateHorizontally(False))
|
|
|
|
|
|
|
|
class RoiIntensity(ReadonlyRegisterBase):
|
|
def __init__(self, name, source, x,y, w, h):
|
|
ReadonlyRegisterBase.__init__(self, name)
|
|
self.source=source
|
|
self.roi = x,y, w, h
|
|
|
|
def doRead(self):
|
|
x,y, w, h = self.roi
|
|
return integrate_roi(self.source, x,y, w, h)
|
|
|
|
###############################################################################
|
|
# Frame integration
|
|
###############################################################################
|
|
|
|
def integrate_ips(ips, as_float=True):
|
|
aux = None
|
|
for i in range(len(ips)):
|
|
if i==0:
|
|
img_type = "float" if as_float else "short"
|
|
aux = new_image(ips[i].width, ips[i].height, image_type=img_type, title = "sum", fill_color = None)
|
|
op_image(aux, ips[i], "add", float_result=as_float, in_place=True)
|
|
return aux
|
|
|
|
def average_ips (ips, roi=None, as_float=True):
|
|
aux = integrate_ips(ips, as_float)
|
|
op_const(aux, "divide", float(len(ips)), in_place=True)
|
|
return aux
|
|
|
|
def get_image(source, roi=None, wait_next=False):
|
|
if wait_next:
|
|
source.waitNext(-1)
|
|
ret = load_image(Utils.grayscale(source.output, Rectangle(roi[0], roi[1], roi[2], roi[3]) if (roi is not None) else None))
|
|
return ret
|
|
|
|
def grab_frames(source, samples, roi=None, wait_next=False, sleep=0):
|
|
frames = []
|
|
for i in range(samples):
|
|
if sleep>0:
|
|
time.sleep(sleep)
|
|
aux = get_image(source, roi, wait_next)
|
|
frames.append(aux)
|
|
return frames
|
|
|
|
def average_frames(source, samples=1, roi=None, wait_next=False, sleep=0, as_float=True):
|
|
return average_ips(grab_frames(source, samples, roi, wait_next, sleep), as_float)
|
|
|
|
def integrate_frames(source, samples=1, roi=None, wait_next=False, sleep=0, as_float=True):
|
|
return integrate_ips(grab_frames(source, samples, roi, wait_next, sleep), as_float)
|
|
|
|
|
|
###############################################################################
|
|
# Image manipulation
|
|
###############################################################################
|
|
|
|
def get_ip_array(ip):
|
|
if type(ip.getProcessor()) == ij.process.FloatProcessor:
|
|
return ip.getProcessor().getFloatArray()
|
|
else:
|
|
return ip.getProcessor().getIntArray()
|
|
|
|
def open_image(file_name):
|
|
file_name = get_context().setup.expandPath(file_name)
|
|
#return Opener().openTiff(file_name, 1)
|
|
return Opener().openImage(file_name)
|
|
|
|
#Save TIFF from
|
|
#save_image(av, "{images}/float.tif","tiff")
|
|
|