def background_subtracted_images(self): """Image data to use for analysis All iamges as 3D numpy array, shape Nimage x W x H""" from numimage import numimage from background_image import background_subtracted from os.path import exists from numpy import array,where info("Mapping images...") filenames = self.image_filenames images = [] for i in range(0,len(filenames)): filename = filenames[i] background_subtracted_filename = filename.replace("/alignment/", "/alignment/background_subtracted/%r/" % self.ROI_fraction) if not exists(background_subtracted_filename): image = self.ROI(numimage(filename)) image = background_subtracted(image) numimage(image+10).save(background_subtracted_filename) images += [numimage(background_subtracted_filename)] info("Loading images...") images = array(images) info("Loading images done.") # offset 10 = 0 counts images = where(images!=0,images-10.0,0.0) return images
def setup(): """Calculate beamstop mask""" global bc_roi, Beamstopid_roi, mask mask_image = numimage(mask_filename) mask = mask_image[x_i:x_f, y_i:y_f] mask = mask_image[x_i + 1.:x_f + 1., y_i:y_f] # beamstop - maximum ring , circle or ellipse? x_indices, y_indices = indices((w, h)) # beamstop assuming ellipse - beamstop roi ellip_x, ellip_y = 2 * 4.0, 2 * 4.0 # pixels # beamstopid - flat-area inside beamstop, assuming ellipse ellip_xid, ellip_yid = ellip_x * 0.75, ellip_y * 0.75 distid_criteria = sqrt(ellip_yid**2 * (x_indices - xbsc)**2 + ellip_xid**2 * (y_indices - ybsc)**2) Beamstopid = distid_criteria < (ellip_xid * ellip_yid) Beamstopid_roi = Beamstopid[x_i:x_f, y_i:y_f] # beamcenter roi # tweak beam center roi to avoid spurious scattering beam_rx, beam_ry = 4.0, 4.0 # 5.0, 5.0 # pixels # just a little shift by 0.5 pixel to avoid beam spillage #Beamcenter_criteria =\ # sqrt(beam_ry**2*(x_indices-(xinit+0.5))**2+beam_rx**2*(y_indices-(yinit-1.0))**2) Beamcenter_criteria =\ sqrt(beam_ry**2*(x_indices-(xinit))**2+beam_rx**2*(y_indices-(yinit))**2) Beamcenter = Beamcenter_criteria < (beam_rx * beam_ry) bc_roi = Beamcenter[x_i:x_f, y_i:y_f]
def image(self): from numimage import numimage from numpy import uint16 filename = self.image_filename if filename: image = numimage(filename) else: image = self.default_image return image
def spot_mask(self): from numpy import zeros,sum,uint32 from peak_integration import spot_mask from numimage import numimage images = self.images sum_image = sum(images.astype(uint32),axis=0) info("Peak mask of summed image...") mask = spot_mask(sum_image) mask = numimage(mask) mask.filename = self.spot_mask_filename return mask
def image(self): from numimage import numimage filename = normpath(self.settings.image_filename) if exists(filename): image = numimage(filename) else: image = self.default_image # Needed for "BeamProfile" to detect image updates. # If a memory-mapped image would be chached by "BeamProfile", the # comparison with the new image would show no difference, since the # cached image dynamically updates. image = image.copy() return image
def image_ROIs(self): """Image data to use for analysis All iamges as 3D numpy array, shape Nimage x W x H""" from numimage import numimage from numpy import array info("Mapping images...") images = [numimage(f) for f in self.image_filenames] images = [self.ROI(image) for image in images] info("Loading images...") images = array(images) info("Loading images done.") return images
def FOM_image(self): """Scan result presented as image""" from numimage import numimage from numpy import rint logfile = self.logfile X,Y,FOM = logfile.X,logfile.Y,logfile.FOM I = rint(self.I(X)).astype(int) J = rint(self.J(Y)).astype(int) image = numimage((self.NX,self.NY)) image[I,J] = FOM image.pixelsize = self.dx image.filename = self.FOM_image_filename return image
def diffraction_spots_of_image(self,image_file): from peak_integration import spot_mask from numimage import numimage from scipy.ndimage.measurements import label from os.path import basename I = numimage(image_file) ROIX,ROIY,WIDTH = self.ROIX,self.ROIY,self.WIDTH I = I[ROIX:ROIX+WIDTH,ROIY:ROIY+WIDTH] # part of image mask = spot_mask(I+20) ##total_spots = mask.sum() labelled_mask,total_spots = label(mask) if total_spots > 0: debug("%s: %s spots" % (basename(image_file),total_spots)) return total_spots
def acquire_image(self): from numimage import numimage from numpy import uint16 from os.path import basename from thread import start_new_thread if self.acquiring_series: filename = "%s%0*d%s" % \ (self.filename_base,self.number_field_width, self.frame_number,self.filename_suffix) I = numimage((self.image_size,self.image_size),dtype=uint16, pixelsize=self.pixelsize) ##info("rayonix: Saving image %r %r" % (basename(filename),I.shape)) start_new_thread(I.save,(filename,)) self.last_filename = filename self.frame_number += 1 if self.frame_number > self.last_frame_number: self.acquiring_series = False
def background_image(image): """An image containing the part of the intensity of each pixel of the imput image that cannot be accouned for by Bragg reflections. Where the background cannot be determined the pixel value is nan. """ from numimage import numimage from scipy.ndimage.filters import gaussian_filter from numpy import log, sqrt, sum, nan, average, zeros from time import clock # Parameters filter_width = 4.5 # FWHM in pixels, e.g. 18 spot_threshold = 2.0 # for spot detection, multiples of sigma filter_sigma = filter_width / (2 * sqrt(2 * log(2))) image = image.astype(float) noise_image = noise(image) unusable_pixel_mask = beamstop_mask(image) | inactive_area(image) mask = zeros(image.shape, bool) info("Background image...") for i in range(0, 10): filtered_image = gaussian_filter(image * (1 - mask), sigma=filter_sigma, mode='constant') weights = gaussian_filter(1.0 - mask, sigma=filter_sigma, mode='constant') background_image = filtered_image / weights Nmasked0 = sum(mask & ~unusable_pixel_mask) mask = (image - background_image > spot_threshold*noise_image) | \ unusable_pixel_mask Nmasked = sum(mask & ~unusable_pixel_mask) ##info("masked %.3g%%" % (average(mask & ~unusable_pixel_mask)*100)) if float(Nmasked - Nmasked0) / Nmasked < 0.001: break info("Background image done.") background_image[unusable_pixel_mask] = nan return numimage(background_image)
def show_image(image_file): from time import time from pylab import figure,imshow,title,show,cm from numimage import numimage from peak_integration import spot_mask,peak_integration_mask from numpy import minimum from scipy.ndimage.measurements import label I0 = numimage(image_file) ROIX,ROIY,WIDTH = sample_frozen.ROIX,sample_frozen.ROIY,sample_frozen.WIDTH I = I0[ROIX:ROIX+WIDTH,ROIY:ROIY+WIDTH] # Time the 'peak_integration_mask' function. t0 = time() mask = spot_mask(I+20) info('spot_mask = %.3f [s]' %(time()-t0)) ##N_spots = mask.sum() labelled_mask,N_spots = label(mask) # Display the image. chart = figure(figsize=(8,8)) title("%s: %d spots" % (image_file,N_spots)) imshow(minimum(I,1000).T,cmap=cm.jet,origin='upper',interpolation='nearest') if N_spots != 0: # Spot integration t0 = time() SB_mask = peak_integration_mask(I) t1 = time() print "Time to find Spots and generate S_mask (s):",t1-t0 # Perform the spot integration. print "Integrated intensity: ",sum(I*SB_mask) # Display 'mask' chart = figure(figsize=(8,8)) title('SB_mask') imshow(SB_mask.T,cmap=cm.jet,origin='upper',interpolation='nearest') show()
def __init__(self, parent, title="Beam Profile", object=None, refresh_period=1.0, size=(300, 300), *args, **kwargs): """title: string object: has attributes "image","x_ROI_center",... """ wx.Window.__init__(self, parent, size=size, *args, **kwargs) self.title = title self.object = object self.refresh_period = refresh_period self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_SIZE, self.OnResize) # Refresh from numpy import nan, uint16 from numimage import numimage self.values = dict([(n, nan) for n in self.attributes]) self.values["image"] = numimage((0, 0), dtype=uint16, pixelsize=0.080) self.old_values = {} from threading import Thread self.refresh_thread = Thread(target=self.refresh_background, name=self.name + ".refresh") self.refreshing = False from wx.lib.newevent import NewEvent self.EVT_THREAD = NewEvent()[1] self.Bind(self.EVT_THREAD, self.OnUpdate) self.thread = Thread(target=self.keep_updated, name=self.name) self.thread.start()
def save_image(image, filename): from numimage import numimage ##debug("Saving image %r..." % basename(filename)) numimage(image).save(filename, "MCCD")
def default_image(self): from numimage import numimage from numpy import uint16 image = numimage((3840, 3840), pixelsize=0.0886, dtype=uint16) + 10 return image