This commit is contained in:
sfop
2017-06-02 08:52:46 +02:00
parent feec6adefe
commit 0f87bec392
21 changed files with 843 additions and 884 deletions
+43 -83
View File
@@ -1,6 +1,7 @@
from startup import *
from ijutils import *
from mathutils import *
import ch.psi.pshell.imaging.Filter as Filter
from ch.psi.pshell.imaging.Overlays import *
import ch.psi.pshell.imaging.Pen as Pen
import java.awt.Color as Color
@@ -12,6 +13,7 @@ from ch.psi.pshell.imaging.Utils import sub
REMOVE_BACKGROUND = True
PLOT_PROFILE = True
"""
def get_centroid(source):
bi = source.getImage()
if bi is None:
@@ -34,44 +36,8 @@ def get_centroid(source):
ov = Crosshairs(Pen(Color.ORANGE), java.awt.Point(int(centroid[0]),int(centroid[1])), java.awt.Dimension(15,15))
op.addOverlay(ov)
return centroid
def arroff(a, value = "mean"):
"""Subtract offset to all elemets in series.
Args:
a(list, tuple, array ...): subscriptable object containing numbers
type(int or str, optional): value to subtract from the array, or "mean" or "min".
Returns:
List
"""
if value=="mean":
value = mean(a)
elif value=="min":
value = min(a)
return [x-value for x in a]
def arrsubback(data, a, b):
"""Subtract offset to all elemets in series.
Args:
a(list, tuple, array ...): subscriptable object containing numbers
type(int or str, optional): value to subtract from the array, or "mean" or "min".
Returns:
List
"""
return [y-(a*y+b) for y in a]
import ch.psi.pshell.imaging.Filter as Filter
"""
class SimulatedSource(Filter):
def process(self, img, data):
@@ -94,7 +60,13 @@ class SimulatedSource(Filter):
class ImageStats(DeviceBase):
def __init__(self, name, source):
DeviceBase.__init__(self, name)
self.source = source
if isinstance(source, basestring):
self.source = get_context().getClassByName("SfCamera")(source, source)
self.private_source = True
self.source.initialize()
else:
self.private_source = False
self.source = source
self.com_x_samples, self.com_y_samples = [], []
self.rms_x_samples, self.rms_y_samples = [], []
self.background = None
@@ -122,7 +94,7 @@ class ImageStats(DeviceBase):
set_device_alias(self.com_y_mean, name + " com y mean")
set_device_alias(self.com_x_stdev, name + " com x stdev")
set_device_alias(self.com_y_stdev, name + " com y stdev")
self.bg_en = False
#self.bg_en = False
self.num_images = 5
class BackgroundSubtractor(Filter):
@@ -135,6 +107,7 @@ class ImageStats(DeviceBase):
self.backgroundFilter = BackgroundSubtractor(self)
self.initialize()
self.start()
#class SourceListener (ImageListener):
# def __init__(self, dev):
@@ -154,17 +127,8 @@ class ImageStats(DeviceBase):
self.rms_x_samples, self.rms_y_samples = [], []
for i in range(self.num_images):
if type(self.source) is not ch.psi.pshell.imaging.FileSource:
#print "A"
self.source.waitNext(5000)
#print "B"
#time.sleep(0.2)
#print "Ok"
#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)
x_profile_x = self.source.data.getRowSelectionX(True)
@@ -172,7 +136,7 @@ class ImageStats(DeviceBase):
y_profile_x = self.source.data.getColSelectionX(True)
#Remove background
if (self.bg_en == False) and REMOVE_BACKGROUND:
if (self.source.backgroundEnabled == False) and REMOVE_BACKGROUND:
(a, b, amp, com, sigma) = fit_gaussian_linear(x_profile, x_profile_x)
x_profile = [x_profile[i]-(a*x_profile_x[i]+b) for i in range(len(x_profile))]
(a, b, amp, com, sigma) = fit_gaussian_linear(y_profile, y_profile_x)
@@ -187,44 +151,37 @@ class ImageStats(DeviceBase):
p = get_plots("Profile")
if (p is None) or (len(p)==0):
p = plot([x_profile, y_profile], ["x_profile", "yprofile"], [x_profile_x,y_profile_x], title ="Profile")
else:
if p is not None:
p[0].removeMarker(None)
p[1].removeMarker(None)
p[0].getSeries(0).setData(x_profile_x, x_profile)
p[1].getSeries(0).setData(y_profile_x, y_profile)
p[0].addMarker(com_x, None, "COM = " + str(com_x), Color.GREEN)
p[1].addMarker(com_y, None, "COM = " + str(com_y), Color.GREEN)
def setNumberOfImages(self, value):
self.num_images = value
def enableBackground(self, value):
self.bg_en = value
self.source.filter = self.backgroundFilter if self.bg_en else None
self.source.backgroundEnabled = value
def captureBackground(self, images):
try:
self.source.filter = None
imgs = []
for i in range(images):
self.source.waitNext(5000)
imgs.append(cam3.output)
#TODO:
self.background = cam3.output
finally:
self.enableBackground(self.bg_en)
def grabBackground(self, images):
self.source.captureBackground(images,0)
def doClose(self):
print "close"
self.source.filter = None
#self.source.removeListener(self.listener)
if self.private_source:
self.source.close()
else:
self.source.backgroundEnabled = False
def start(self):
pass
if self.private_source:
self.source.polling = 200
self.source.waitNext(2000)
def stop(self):
pass
if self.private_source:
self.source.polling = 0
def get_simulated_source(img):
@@ -235,16 +192,14 @@ 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)
add_device(ImageStats("image_stats", "SLG-LCAM-C041"), True)
add_device(image_stats.source, True)
#cam3.waitNext(2000)
add_device(ImageStats("image_stats", cam3), True)
cam3.waitNext(2000)
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()
@@ -253,9 +208,14 @@ if __name__ == "__builtin__":
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]
image_stats.captureBackground(5)
#image_stats.enableBackground(True)
image_stats.grabBackground(1)
def before_sample():
image_stats.update()
tscan(sensors, 10, 0.1, before_read = before_sample)
image_stats.enableBackground(False)
try:
tscan(sensors, 10, 0.1, before_read = before_sample)
finally:
image_stats.enableBackground(False)
image_stats.stop()
"""