def compute_threshold(self, image, mask): ''' Compute the threshold. :param image: The image to process :param mask: The pixel mask on the image :returns: A boolean mask showing foreground/background pixels ''' import libtbx params = self.params if params.spotfinder.threshold.dispersion.global_threshold is libtbx.Auto: params.spotfinder.threshold.dispersion.global_threshold \ = int(estimate_global_threshold(image, mask)) logger.info( "Setting global_threshold: %i" % (params.spotfinder.threshold.dispersion.global_threshold)) from dials.algorithms.spot_finding.threshold import DispersionThresholdStrategy self._algorithm = DispersionThresholdStrategy( kernel_size=params.spotfinder.threshold.dispersion.kernel_size, gain=params.spotfinder.threshold.dispersion.gain, mask=params.spotfinder.lookup.mask, n_sigma_b=params.spotfinder.threshold.dispersion.sigma_background, n_sigma_s=params.spotfinder.threshold.dispersion.sigma_strong, min_count=params.spotfinder.threshold.dispersion.min_local, global_threshold=params.spotfinder.threshold.dispersion. global_threshold) return self._algorithm(image, mask)
def main(): args = sys.argv[1:] meta, data = args[0:2] with h5py.File(meta, "r") as f: mask = f["/mask"][()] bitmask = flex.int(mask) == 0 thresholder = DispersionThresholdStrategy(gain=1) total = None with h5py.File(data, "r") as f: d_id = f["/data"].id nz, ny, nx = f["/data"].shape dt = f["/data"].dtype for j in range(nz): image = chunk_read_image(d_id, j) signal_pixels = thresholder(image, mask=mask) if total is None: total = signal_pixels.as_1d().as_int() else: total += signal_pixels.as_1d().as_int() hot_mask = total >= (nz // 2) hot_pixels = hot_mask.iselection() for h in hot_pixels: print(" mask[%d, %d] = 8" % (h % nx, h // nx))
def find_spots(self, min_spot_size=2, max_spot_size=100): """ Find the strong spots on the image """ from dials.algorithms.spot_finding.threshold import DispersionThresholdStrategy from dials.model.data import PixelList from dials.model.data import PixelListLabeller from dials.array_family import flex print("") print("-" * 80) print(" Finding strong spots") print("-" * 80) print("") # Instantiate the threshold function threshold = DispersionThresholdStrategy() # Get the raw data and image mask image = self.experiment.imageset.get_raw_data(0)[0] mask = self.experiment.imageset.get_mask(0)[0] # Threshold the image and create the pixel labeller threshold_mask = threshold(image, mask) pixel_labeller = PixelListLabeller() pixel_labeller.add(PixelList(0, image, threshold_mask)) # Create the shoebox list from the pixel list creator = flex.PixelListShoeboxCreator( pixel_labeller, 0, # Panel number 0, # Z start True, # 2D self.params.spot_finding.min_spot_size, # Min Pixels self.params.spot_finding.max_spot_size, # Max Pixels False, ) # Find hot pixels shoeboxes = creator.result() # Filter the list to remove large and small spots size = creator.spot_size() large = size > self.params.spot_finding.max_spot_size small = size < self.params.spot_finding.min_spot_size bad = large | small shoeboxes = shoeboxes.select(~bad) print("Discarding %d spots with < %d pixels" % (small.count(True), self.params.spot_finding.min_spot_size)) print("Discarding %d spots with > %d pixels" % (large.count(True), self.params.spot_finding.max_spot_size)) # Extract the strong spot information centroid = shoeboxes.centroid_valid() intensity = shoeboxes.summed_intensity() observed = flex.observation(shoeboxes.panels(), centroid, intensity) # Create the reflection list self.reflections = flex.reflection_table(observed, shoeboxes) print("Using %d strong spots" % len(self.reflections))
def hot(greyscale_flex, params): '''Find hot pixels in the image (returns flex bool).''' from dials.algorithms.spot_finding.threshold import \ DispersionThresholdStrategy from dials.array_family import flex thresholder = DispersionThresholdStrategy(gain=params.gain) mask = flex.bool(greyscale_flex.size(), True) mask.reshape(flex.grid(*greyscale_flex.focus())) threshold_mask = thresholder(greyscale_flex, mask=mask) return threshold_mask
def find_spots(image): from dials.algorithms.spot_finding.threshold import DispersionThresholdStrategy from dials.model.data import PixelList from dials.model.data import PixelListLabeller thresholder = DispersionThresholdStrategy(gain=1) mask = image.as_1d() >= 0 # flex.bool(image.size(), True) mask.reshape(flex.grid(*image.focus())) threshold_mask = thresholder(image, mask=mask) plist = PixelList(0, image, threshold_mask) pixel_labeller = PixelListLabeller() pixel_labeller.add(plist) creator = flex.PixelListShoeboxCreator(pixel_labeller, 0, 0, True, 2, 100, False) shoeboxes = creator.result() return shoeboxes
def image_to_shoeboxes(image): """For a given image, find spots 2 - 100 pixels im size, assuming a gain of 1, return the list of spot shoeboxes. Also assumes valid intensities in range 0...N.""" thresholder = DispersionThresholdStrategy(gain=1) mask = image.as_1d() >= 0 mask.reshape(flex.grid(*image.focus())) threshold_mask = thresholder(image, mask=mask) plist = PixelList(0, image, threshold_mask) pixel_labeller = PixelListLabeller() pixel_labeller.add(plist) creator = flex.PixelListShoeboxCreator(pixel_labeller, 0, 0, True, 2, 100, False) shoeboxes = creator.result() return shoeboxes
def find(greyscale_flex, params, mask=None): '''Find stars on input greyscale flex image.''' from dials.algorithms.spot_finding.threshold import \ DispersionThresholdStrategy from dials.model.data import PixelList from dials.model.data import PixelListLabeller from dials.array_family import flex thresholder = DispersionThresholdStrategy(gain=params.gain) if not mask: mask = flex.bool(greyscale_flex.size(), True) mask.reshape(flex.grid(*greyscale_flex.focus())) threshold_mask = thresholder(greyscale_flex, mask=mask) plist = PixelList(0, greyscale_flex, threshold_mask) pixel_labeller = PixelListLabeller() pixel_labeller.add(plist) creator = flex.PixelListShoeboxCreator(pixel_labeller, 0, 0, True, params.min_size, params.max_size, False) shoeboxes = creator.result() # remove nonsense size = creator.spot_size() big = size > params.max_size small = size < params.min_size bad = big | small shoeboxes = shoeboxes.select(~bad) centroid = shoeboxes.centroid_valid() intensity = shoeboxes.summed_intensity() observed = flex.observation(shoeboxes.panels(), centroid, intensity) stars = flex.reflection_table(observed, shoeboxes) return stars