def sub_mean(img, mean_img, scale=1.0): """ Subtract the pixelwise mean from each frame. """ return set_neg_to_zero(img-scale*mean_img)
def sub_min(img, min_img, scale=1.0): """ Subtract a pixelwise minimum from each frame. """ return set_neg_to_zero(img-scale*min_img)
def sub_median(img, median_img, scale=1.0): """ Subtract the movie's median with respect to time from the frame. """ return set_neg_to_zero(img-scale*median_img)
def sub_gauss_filt_mean(img, mean_img, k=2.0, scale=1.0): """ Subtract a Gaussian-filtered mean image from the present image. """ return set_neg_to_zero(img - scale * \ ndi.gaussian_filter(mean_img, k))
def centroid(psf_img, sub_bg=False, camera_offset=0.0, camera_gain=1.0): """ Localize with center-of-mass. args ---- psf_img : 2D ndarray sub_bg : bool, subtract BG before taking centroid camera_offset : float, offset of camera camera_gain : float, gain of camera returns ------- dict : { 'y0': y centroid in pixels, 'x0': x centroid in pixels 'I': integrated intensity above background; 'amp': the intensity of the brightest pixel; 'bg': the estimated background level; 'snr': the estimated signal-to-noise ratio based on the amplitude } """ # Subtract BG and divide out gain psf_img = utils.rescale_img(psf_img, camera_offset=camera_offset, camera_gain=camera_gain) # Estimate background bg = utils.ring_mean(psf_img) # Subtract background psf_sub_bg = utils.set_neg_to_zero(psf_img - bg) # Integrate intensity above background I = psf_sub_bg.sum() # Take the brightest pixel above background as # the amplitude amp = utils.amp_from_I(I, sigma=1.0) # Find spot centers if sub_bg: y0, x0 = ndi.center_of_mass(psf_sub_bg) else: y0, x0 = ndi.center_of_mass(psf_img) # Estimate signal-to-noise snr = utils.estimate_snr(psf_img, amp) return dict((('bg', bg), ('I', I), ('amp', amp), ('y0', y0), ('x0', x0), ('snr', snr)))
def dou_filter(img, k0=3, k1=9, t=None, return_filt=True): """ Find spots by difference of uniforming- filtering. args ---- img : 2D ndarray k0 : float, spot kernel width k1 : float, BG kernel width t : float, threshold (if None, defaults to Otsu) return_filt : bool. In addition to the spot positions, also return the filtered and binary images returns ------- If *return_filt*: ( 2D ndarray, filtered image; 2D ndarray, binary image; 2D ndarray of shape (n_spots, 2), coordinates of spots ) else: 2D ndarray of shape (n_spots, 2), coordinates of spots """ img = img.astype('float64') # Filter img_filt = utils.set_neg_to_zero( ndi.uniform_filter(img, k0) - \ ndi.uniform_filter(img, k1) ) # Threshold if t is None: t = threshold_otsu(img_filt) img_bin = img_filt > t # Find spots positions = utils.label_binary_spots(img_bin, img_int=img_filt) if return_filt: return img_filt, img_bin, positions else: return positions
def ls_log_gaussian(psf_img, sigma=1.0, camera_bg=0.0, camera_gain=1.0): """ Find the least-squares estimate for the parameters of a pointwise 2D Gaussian PSF model, assuming noise is log-normally distributed. While this noise model is fairly unrealistic, it admits a linear LS estimator for the parabolic log intensities of a pointwise Gaussian. As a result, it is very fast. However, it has the issue that it is biased- the log normality assumption tends to make it biased toward the center of the fitting window. Use at your own risk. args ---- psf_img : 2D ndarray sigma : float, the width of the Gaussian model returns ------- dict : { 'y0': estimated y center, 'x0': estimated x center, 'I': estimated PSF intensity, 'bg': estimated background intensity, 'amp': estimated peak PSF amplitude, 'snr': estimated SNR } """ # Subtract BG and divide out gain psf_img = utils.rescale_img(psf_img, camera_offset=camera_offset, camera_gain=camera_gain) # Common factors V = sigma**2 V2 = V * 2 # Estimate background by taking the mean # of the outer ring of pixels bg = utils.ring_mean(psf_img) # Subtract background psf_img_sub = utils.set_neg_to_zero(psf_img - bg) # Avoid taking log of zero pixels nonzero = psf_img_sub > 0.0 # Pixel indices Y, X = np.indices(psf_img.shape) Y = Y[nonzero] X = X[nonzero] # Log PSF above background log_psf = np.log(psf_img_sub[nonzero]) # Response vector in LS problem R = log_psf + np.log(V2 * np.pi) + (Y**2 + X**2) / V2 # Independent matrix in LS problem M = np.asarray([np.ones(nonzero.sum()), V * Y, V * X]).T # Compute the LS parameter estimate ls_pars = utils.pinv(M) @ R # Use the LS parameters to the find the # spot center y0 = ls_pars[1] x0 = ls_pars[2] I = np.exp(ls_pars[0] + (y0**2 + x0**2) / V2) # Estimate peak Gaussian amplitude amp = utils.amp_from_I(I, sigma=sigma) # Estimate SNR snr = utils.estimate_snr(psf_img, amp) return dict((('y0', y0), ('x0', x0), ('I', I), ('bg', bg), ('amp', amp), ('snr', snr)))