168 lines
5.1 KiB
Python
168 lines
5.1 KiB
Python
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
|
|
import ch.psi.pshell.device.Camera.DataType as DataType
|
|
|
|
eiger.setDataType(DataType.Float32)
|
|
eiger.getDataArray().monitored=True
|
|
###############################################################################
|
|
# 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 roi.integrate(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)
|
|
|
|
def create_roi_devices(roi_list, add = True):
|
|
rois = []
|
|
for r in roi_list:
|
|
roi = RoiIntensity(r, image, roi_list[r][0], roi_list[r][1], roi_list[r][2], roi_list[r][3])
|
|
if add:
|
|
add_device(roi, True)
|
|
rois.append(roi)
|
|
return rois
|
|
|
|
###############################################################################
|
|
# Frame integration
|
|
###############################################################################
|
|
"""
|
|
def get_image(source, roi=None, wait_next=False):
|
|
if wait_next:
|
|
source.waitNext(-1)
|
|
elif wait_next:
|
|
if str(source.camera)=="Single":
|
|
eiger.start()
|
|
eiger.waitCacheChange(20000)
|
|
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)
|
|
"""
|
|
def grab_frame(source, roi=None, wait_next=False):
|
|
if wait_next:
|
|
if str(eiger.grabMode)=="Single":
|
|
eiger.start()
|
|
#eigar.waitCacheChange(20000)
|
|
source.waitNext(20000)
|
|
#ret = load_image(Utils.grayscale(source.output, Rectangle(roi[0], roi[1], roi[2], roi[3]) if (roi is not None) else None))
|
|
time.sleep(1.0)
|
|
data=source.data
|
|
if roi is not None:
|
|
data = data.getRoi(Rectangle(roi[0], roi[1], roi[2], roi[3]))
|
|
#ret = load_image(img)
|
|
return data
|
|
|
|
def grab_frames(source, samples, roi=None, wait_next=False, sleep=0):
|
|
frames = []
|
|
for i in range(samples):
|
|
if (i>0) and (sleep>0):
|
|
time.sleep(sleep)
|
|
aux = grab_frame(source, roi, wait_next)
|
|
frames.append(aux)
|
|
return frames
|
|
|
|
def integrate_frames(frames):
|
|
if frames is None or (len(frames)==0):
|
|
return None
|
|
ret = frames[0].copy()
|
|
for data in frames[1:]:
|
|
ret.sum(data)
|
|
return ret
|
|
|
|
def get_ip_array(ip):
|
|
"""
|
|
Returns data array of ImagePlus
|
|
"""
|
|
if type(ip.getProcessor()) == FloatProcessor:
|
|
ret = ip.getProcessor().getFloatArray()
|
|
else:
|
|
ret = ip.getProcessor().getIntArray()
|
|
ret = Convert.reshape(ret, [ip.getHeight(), ip.getWidth()])
|
|
return ret
|
|
|
|
def average_frames(frames):
|
|
ret = integrate_frames(frames)
|
|
if ret is not None:
|
|
ret.div(len(frames))
|
|
return ret
|
|
|
|
def save_as_tiff(data, filename, check=False):
|
|
if type(data) == Data:
|
|
ip = load_array(data.matrix)
|
|
else:
|
|
ip = data
|
|
data = get_ip_array(ip)
|
|
#ip.show()
|
|
#plot(data)
|
|
if not os.path.exists(os.path.dirname(filename)):
|
|
os.makedirs(os.path.dirname(filename))
|
|
save_image(ip, filename,"tiff")
|
|
|
|
if check:
|
|
import java.util.Arrays as Arrays
|
|
ip=open_image(filename)
|
|
read = get_ip_array(ip)
|
|
if not Arrays.deepEquals(read, data):
|
|
raise Exception("Error reading array")
|
|
#plot(read)
|
|
|
|
def trigger_eiger(wait=False):
|
|
eiger.start()
|
|
if wait:
|
|
eiger.waitNewImage(20000)
|
|
|
|
|
|
|
|
if False:
|
|
integrate_roi(image, 10, 5, 20, 10)
|
|
|
|
add_device(RoiIntensity("Region1", image, 10, 5, 20, 10), True)
|
|
add_device(RoiIntensity("Region2", image, 10, 5, 40, 20), True)
|
|
|
|
|
|
import ch.psi.pshell.data.ProviderCSV as ProviderCSV
|
|
ProviderCSV.setDefaultItemSeparator(" ")
|
|
tscan((Region1, Region2), 10, 0.1, layout="table", provider = "csv")
|
|
|
|
ret = grab_frames(image, 10, sleep=0.1)
|
|
av = integrate_frames(ret)
|
|
save_as_tiff(av,"{images}/data.tif", True)
|
|
|
|
|
|
print "Success"
|
|
|
|
|
|
|
|
|
|
|