from shutil import copyfile import threading class ImageIntensity(DeviceBase, Readable): def __init__(self, image): DeviceBase.__init__(self, image.name + " intensity") self.setParent(image) def read(self): p=self.getParent() ret = self.getParent().read() if ret is not None: (I_sum, area_I, thresh1_count, thresh2_count, thresh3_count, thresh4_count, I_sum_bgr, area_bgr) = ret ret = I_sum #if area_bgr > 0: # ret = ret - ((I_sum_bgr / area_bgr) * area_I) self.setCache(ret, None) return ret class ImageCorrectedIntensity(DeviceBase, Readable): def __init__(self, image): DeviceBase.__init__(self, image.name + " corrected intensity") self.setParent(image) def read(self): ret = self.getParent().intensity.take() if ret is not None: trans = transm.value count_time = get_count_time() if (trans == 0.0) or (count_time==0): ret= float("nan") else: ret = ret / count_time / transm.value self.setCache(ret, None) return ret class ImageMatrix(ReadonlyRegisterBase, ReadonlyRegisterMatrix, Readable.IntegerType): def __init__(self, image): DeviceBase.__init__(self, "Matrix") self.setParent(image) def doRead(self): #print "----- DO READ --------------" #traceback.print_stack() return self.getParent().get_image() def getWidth(self): return self.getParent().pixel.PIX_XDIM def getHeight(self): return self.getParent().pixel.PIX_YDIM class Image(DeviceBase, Readable): def __init__(self, name, pixel): DeviceBase.__init__(self, name) self.pixel = pixel self.roi = [0,0, self.pixel.PIX_XDIM-1, self.pixel.PIX_YDIM-1] self.broi = [0,0, self.pixel.PIX_XDIM-1, self.pixel.PIX_YDIM-1] self.intensity = ImageIntensity(self) self.matrix = ImageMatrix(self) self.corrected_intensity = ImageCorrectedIntensity(self) self.last_filename = None self.last_image = None self.image_lock = threading.Lock() self.I_sum, self.area_I, self.thresh1_count, self.thresh2_count, self.thresh3_count, self.thresh4_count, self.I_sum_bgr, self.area_bgr = None,None,None,None,None,None,None,None self.init_threshold() def get_int(self, filename = None): if filename is None: filename = self.pixel.get_image_filename() file_size = 4 * self.pixel.PIX_XDIM * self.pixel.PIX_YDIM print "Waiting image file: " , filename, " size=", file_size wait_for_file_size(filename, file_size, 20.0) print "Calculating intensity for: " + filename + " - threshold: " + str((self.threshold1, self.threshold2, self.threshold3, self.threshold4)) roi = get_roi() bkroi = get_bg_roi() tmp_file = self.pixel.image_root_folder + "tmp/spec_image_info.dat" copyfile(filename, tmp_file) try: ret = img_get_int(tmp_file, self.threshold1, self.threshold2, self.threshold3, self.threshold4, \ self.pixel.image_header_length, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH, \ roi[0], roi[1], roi[2], roi[3], bkroi[0], bkroi[1], bkroi[2], bkroi[3], filter_median = True, filter_nsigma = 30) self.I_sum, self.area_I, self.thresh1_count, self.thresh2_count, self.thresh3_count, self.thresh4_count, self.I_sum_bgr, self.area_bgr = ret except: log("Error calculating intensity: " + str(filename) + " - " + str(sys.exc_info()[1]), False) self.I_sum, self.area_I, self.thresh1_count, self.thresh2_count, self.thresh3_count, self.thresh4_count, self.I_sum_bgr, self.area_bgr = None,None,None,None,None,None,None,None ret = None self.setCache(ret, None) return ret def get_image(self, filename = None): if filename is None: filename = self.pixel.get_image_filename() else: if not '/' in filename: filename = self.pixel.get_full_path() + filename filename = str(filename) self.image_lock.acquire(True) try: if filename == self.last_filename: return self.last_image print ("Reading image : " + str(filename)) try: ret = img_read(filename, self.pixel.image_header_length, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH) except: log("Error reading data file: " + str(filename) + " - " + str(sys.exc_info()[1]), False) print ("Error reading data file: " + str(filename) + " - " + str(sys.exc_info()[1])) self.last_filename = None self.last_image = None return None ret = Convert.reshape(ret, self.pixel.PIX_YDIM, self.pixel.PIX_XDIM) self.last_filename = filename self.last_image = ret return ret finally: self.image_lock.release() def read(self): return self.get_int() def set_threshold(self, threshold1= None, threshold2= None, threshold3= None, threshold4= None): if threshold1 is not None: self.threshold1 = threshold1 if threshold2 is not None: self.threshold2 = threshold2 if threshold3 is not None: self.threshold3 = threshold3 if threshold4 is not None: self.threshold4 = threshold4 def init_threshold(self): self.threshold1, self.threshold2, self.threshold3, self.threshold4 = self.pixel.threshold1, self.pixel.threshold2, self.pixel.threshold3, self.pixel.threshold4 class ReadableImage(ReadableMatrix): def read(self): ret = [] for i in range (self.getHeight()): ret.append([random.random()] * self.getWidth()) return to_array(ret, 'd') def getWidth(self): return 80 def getHeight(self): return 40 add_device( Image("image", pixel), True) """ def _update_detector_image(): time.sleep(1.0) #TODO: Must wait thje file to be ready instead image.matrix.update() if "_pixelListener" in globals(): pixel.removeListener(_pixelListener) class PixelListener (DeviceListener): def onValueChanged(self, device, value, former): fork(_update_detector_image) _pixelListener = PixelListener() pixel.addListener(_pixelListener) add_device(RegisterMatrixSource("detector_image", image.matrix), True) detector_image.monitored = True image.matrix.update() """ add_device(RegisterMatrixSource("detector_image", image.matrix), True) detector_image.monitored = True