from shutil import copyfile class ImageIntensity(DeviceBase, Readable): def __init__(self, image): DeviceBase.__init__(self, image.name + " intensity") self.setParent(image) def read(self): 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 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): 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_XDIM] self.broi = [0,0, self.pixel.PIX_XDIM-1, self.pixel.PIX_YDIM] self.intensity = ImageIntensity(self) self.matrix = ImageMatrix(self) self.corrected_intensity = ImageCorrectedIntensity(self) self.last_filename = None self.last_image = None def get_int(self, threshold1= None, threshold2= None, threshold3= None, threshold4= None, filename = None): if threshold1 is None: threshold1 = self.pixel.PIX_THRESH1 if threshold2 is None: threshold2 = self.pixel.PIX_THRESH2 if threshold3 is None: threshold3 = self.pixel.PIX_THRESH3 if threshold4 is None: threshold4 = self.pixel.PIX_THRESH4 if filename is None: filename = self.pixel.get_image_filename() print "Calculating intensity for: " + filename + " - threshold: " + str((threshold1, threshold2, threshold3, threshold4)) tmp_file = self.pixel.image_root_folder + "tmp/spec_image_info.dat" copyfile(filename, tmp_file) ret = img_get_int(tmp_file, threshold1, threshold2, threshold3, threshold4, \ self.pixel.IMAGE_HEADER_LENGTH, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH, \ self.roi[0], self.roi[1], self.roi[2], self.roi[3], self.broi[0], self.broi[1], self.broi[2], self.broi[3]) self.setCache(ret, None) return ret def get_image(self, filename = None): if filename is None: filename = self.pixel.get_image_filename() if filename == self.last_filename: return self.last_image print "Reading image : " + filename ret = img_read(filename, self.pixel.IMAGE_HEADER_LENGTH, self.pixel.PIX_XDIM, self.pixel.PIX_YDIM,self.pixel.PIX_COLOR_DEPTH) ret = Convert.reshape(ret, self.pixel.PIX_YDIM, self.pixel.PIX_XDIM) self.last_filename = filename self.last_image = ret return ret def read(self): return self.get_int() 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) image.matrix.polling = 10 add_device(RegisterMatrixSource("detector_image", image.matrix), True)