Startup
This commit is contained in:
@@ -4,7 +4,46 @@ from ch.psi.pshell.imaging.Overlays import *
|
||||
import ch.psi.pshell.imaging.Pen as Pen
|
||||
import java.awt.Color as Color
|
||||
import random
|
||||
import ch.psi.pshell.imaging.ImageListener as ImageListener
|
||||
from operator import add, mul, sub, truediv
|
||||
|
||||
def arrmul(a, b):
|
||||
"""Multiply 2 series of the same size.
|
||||
|
||||
Args:
|
||||
|
||||
a(list, tuple, array ...): subscriptable object containing numbers
|
||||
b(list, tuple, array ...): subscriptable object containing numbers
|
||||
|
||||
Returns:
|
||||
List
|
||||
|
||||
"""
|
||||
return map(mul, a, b)
|
||||
|
||||
def center_of_mass(data, x = None):
|
||||
"""Calculate the center of mass of a series, and its rms.
|
||||
|
||||
Args:
|
||||
|
||||
data(list, tuple, array ...): subscriptable object containing numbers
|
||||
x(list, tuple, array ..., optional): x coordinates
|
||||
|
||||
Returns:
|
||||
Tuple (com, rms)
|
||||
|
||||
"""
|
||||
if x is None:
|
||||
x = Arr.indexesDouble(len(data))
|
||||
data_sum = sum(data)
|
||||
if (data_sum==0):
|
||||
return float('nan')
|
||||
xmd = arrmul( x, data)
|
||||
com = sum(xmd) / data_sum
|
||||
xmd2 = arrmul( x, xmd)
|
||||
com2 = sum(xmd2) / data_sum
|
||||
rms = math.sqrt(abs(com2 - com * com))
|
||||
return (com, rms)
|
||||
|
||||
def get_centroid(source):
|
||||
bi = source.getImage()
|
||||
@@ -83,20 +122,38 @@ class ImageStats(DeviceBase):
|
||||
set_device_alias(self.com_y_stdev, name + " com y stdev")
|
||||
self.bg_en = False
|
||||
self.num_images = 1
|
||||
self.initialize()
|
||||
self.initialize()
|
||||
|
||||
class SourceListener (ImageListener):
|
||||
def __init__(self, dev):
|
||||
self.dev=dev
|
||||
def onImage(self, origin, image, data):
|
||||
print "On image"
|
||||
self.dev.doUpdate()
|
||||
def onError(self, origin, ex):
|
||||
self.dev.com_x_samples, self.dev.com_y_samples = [], []
|
||||
self.listener = SourceListener(self)
|
||||
self.source.addListener(self.listener)
|
||||
|
||||
|
||||
def doUpdate(self):
|
||||
print "Update"
|
||||
self.com_x_samples, self.com_y_samples = [], []
|
||||
for i in range(self.num_images):
|
||||
if type(self.source) is not ch.psi.pshell.imaging.FileSource:
|
||||
self.source.waitNext(3000)
|
||||
centroid = get_centroid(self.source)
|
||||
print "cent ", centroid
|
||||
if centroid is not None:
|
||||
self.com_x_samples.append(centroid[0])
|
||||
self.com_y_samples.append(centroid[1])
|
||||
|
||||
#if type(self.source) is not ch.psi.pshell.imaging.FileSource:
|
||||
# self.source.waitNext(5000)
|
||||
#centroid = get_centroid(self.source)
|
||||
#print "cent ", centroid
|
||||
#if centroid is not None:
|
||||
# self.com_x_samples.append(centroid[0])
|
||||
# self.com_y_samples.append(centroid[1])
|
||||
x_profile = self.source.data.integrateVertically(True)
|
||||
y_profile = self.source.data.integrateHorizontally(True)
|
||||
com_x,rms_x = center_of_mass(x_profile)
|
||||
com_y,rms_y = center_of_mass(y_profile)
|
||||
self.com_x_samples.append(com_x)
|
||||
self.com_y_samples.append(com_y)
|
||||
|
||||
def setNumberOfImages(self, value):
|
||||
self.num_images = value
|
||||
|
||||
@@ -106,8 +163,9 @@ class ImageStats(DeviceBase):
|
||||
def captureBackground(self, images):
|
||||
self.doInitialize()
|
||||
|
||||
def doClose(self):
|
||||
pass
|
||||
def doClose(self):
|
||||
print "close"
|
||||
self.source.removeListener(self.listener)
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
@@ -124,14 +182,23 @@ def get_simulated_source(img):
|
||||
show_panel(simulated_source)
|
||||
return simulated_source
|
||||
|
||||
|
||||
if __name__ == "__builtin__":
|
||||
simulated_source = get_simulated_source(image)
|
||||
print get_centroid(simulated_source)
|
||||
#simulated_source = get_simulated_source(image)
|
||||
#print get_centroid(simulated_source)
|
||||
|
||||
add_device(ImageStats("image_stats", simulated_source), True)
|
||||
add_device(ImageStats("image_stats", cam2), True)
|
||||
cam2.waitNext(5000)
|
||||
image_stats.enableBackground(False)
|
||||
for i in range (10):
|
||||
image_stats.update()
|
||||
print image_stats.take(), image_stats.com_x_mean.read(), image_stats.com_y_mean.read()
|
||||
time.sleep(1)
|
||||
#for i in range (10):
|
||||
# image_stats.update()
|
||||
# print image_stats.take(), image_stats.com_x_mean.read(), image_stats.com_y_mean.read()
|
||||
# time.sleep(1)
|
||||
|
||||
image_stats.setNumberOfImages(3)
|
||||
sensors = [image_stats.com_x_mean, image_stats.com_y_mean, image_stats.com_x_stdev, image_stats.com_y_stdev]
|
||||
try:
|
||||
tscan(sensors, 10, 0.1)
|
||||
finally:
|
||||
image_stats.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user