def calibrate_brightness_from_image_fn(fns): """ Calibrate pixel->brightness (size of beam) from a set of images fns: `str` Set of images to determine size of beam from return: instance of Calibration class with conversion methods """ values = [] for fn in fns: print() print("Image:", fn) img, h = load_img(fn) brightness = float(h["Brightness"]) binsize = float(h["ImageBinSize"]) img, scale = autoscale(img) holes = find_holes(img, plot=False, fname=None, verbose=False, max_eccentricity=0.8) size = max([hole.equivalent_diameter for hole in holes]) * binsize / scale print(f"Brightness: {brightness:.0f}, equivalent diameter: {size:.1f}px") values.append((brightness, size)) values = np.array(values) c = CalibBrightness.from_data(*values.T) c.plot() return c
def calibrate_brightness_live(ctrl, step=1000, save_images=False, **kwargs): """ Calibrate pixel->brightness coordinates live on the microscope ctrl: instance of `TEMController` contains tem + cam interface start: `float` start value for calibration (0.0 - 1.0) end: `float` end value for calibration (0.0 - 1.0) exposure: `float` exposure time binsize: `int` return: instance of CalibBrightness class with conversion methods """ raise NotImplementedError("calibrate_brightness_live function needs fixing...") exposure = kwargs.get("exposure", ctrl.cam.default_exposure) binsize = kwargs.get("binsize", ctrl.cam.default_binsize) values = [] start = ctrl.brightness.value for i in range(10): target = start + i*step ctrl.brightness.value = int(target) outfile = f"calib_brightness_{i:04d}" if save_images else None comment = f"Calib image {i}: brightness={target}" img, h = ctrl.getImage(exposure=exposure, out=outfile, comment=comment, header_keys="Brightness") img, scale = autoscale(img) brightness = float(h["Brightness"]) holes = find_holes(img, plot=False, verbose=False, max_eccentricity=0.8) if len(holes) == 0: print(" >> No holes found, continuing...") continue size = max([hole.equivalent_diameter for hole in holes]) * binsize / scale print("Brightness: {:.f}, equivalent diameter: {:.1f}".format(brightness, size)) values.append((brightness, size)) values = np.array(values) c = CalibBrightness.from_data(*values.T) # Calling c.plot with videostream crashes program if not hasattr(ctrl.cam, "VideoLoop"): c.plot() return c
def calibrate_beamshift_from_image_fn(center_fn, other_fn): """ Calibrate pixel->beamshift coordinates from a set of images center_fn: `str` Reference image with the beam at the center of the image other_fn: `tuple` of `str` Set of images to cross correlate to the first reference image return: instance of Calibration class with conversion methods """ print() print("Center:", center_fn) img_cent, h_cent = load_img(center_fn) beamshift_cent = np.array(h_cent["BeamShift"]) img_cent, scale = autoscale(img_cent, maxdim=512) binsize = h_cent["ImageBinSize"] holes = find_holes(img_cent, plot=False, verbose=False, max_eccentricity=0.8) pixel_cent = np.array(holes[0].centroid) * binsize / scale print("Beamshift: x={} | y={}".format(*beamshift_cent)) print("Pixel: x={:.2f} | y={:.2f}".format(*pixel_cent)) shifts = [] beampos = [] for fn in other_fn: img, h = load_img(fn) img = imgscale(img, scale) beamshift = np.array((h["BeamShift"])) print() print("Image:", fn) print("Beamshift: x={} | y={}".format(*beamshift)) shift = cross_correlate(img_cent, img, upsample_factor=10, verbose=False) beampos.append(beamshift) shifts.append(shift) # correct for binsize, store as binsize=1 shifts = np.array(shifts) * binsize / scale beampos = np.array(beampos) - beamshift_cent c = CalibBeamShift.from_data(shifts, beampos, reference_shift=beamshift_cent, reference_pixel=pixel_cent, header=h_cent) c.plot() return c