def _get_motion_kernel(psf_sd, line_xy): if len(psf_sd) == 3: sd1, w, sd2 = psf_sd else: sd1, w, sd2 = psf_sd[0], 0, 0 psf_hw = math.ceil(max(sd1 * 3, sd2 * 2)) psf_fw = 1 + 2 * psf_hw psf = ImageProc.gkern2d(psf_fw, sd1) + (0 if w == 0 else w * ImageProc.gkern2d(psf_fw, sd2)) line_xy = np.array(line_xy) line = np.zeros( np.ceil(np.abs(np.flip(line_xy))).astype(np.int) + psf_fw) cnt = np.flip(line.shape) / 2 start = tuple(np.round(cnt - line_xy / 2).astype(np.int)) end = tuple(np.round(cnt + line_xy / 2).astype(np.int)) cv2.line(line, start, end, color=1.0, thickness=1, lineType=cv2.LINE_AA) mb_psf = cv2.filter2D(line, cv2.CV_64F, psf) mb_psf /= np.sum(mb_psf) # normalize to one return mb_psf
def _motion_kernel_psf_saturation(self, du, psf_sd, get_px_du_sat=False): read_sd = None if len(psf_sd) in (2, 4): psf_sd, read_sd = psf_sd[:-1], psf_sd[-1] line_xy = self.frame.motion_in_px(self.ixy) mb_psf = self._get_motion_kernel(psf_sd, line_xy) px_du_sat = np.clip(mb_psf * du, 0, self.frame.max_signal) if read_sd: noise = trunc_gaussian_shift(px_du_sat, read_sd * self.frame.max_signal, self.frame.max_signal) if 1: px_du_sat = np.clip(px_du_sat - noise, 0, self.frame.max_signal) du_sat = np.sum(px_du_sat) else: noise = np.random.normal(0, read_sd * saturation_val, px_du_sat.shape) noise = cv2.filter2D(noise, cv2.CV_64F, ImageProc.gkern2d(5, 1.0)) px_du_sat = np.clip(px_du_sat + noise, 0, saturation_val) else: du_sat = np.sum(px_du_sat) return (du_sat, ) + ((px_du_sat, ) if get_px_du_sat else tuple())
def generate_field_fft(shape, sd=(0.33, 0.33, 0.34), len_sc=(0.5, 0.5 / 4, 0.5 / 16)): from visnav.algo.image import ImageProc sds = sd if getattr(sd, '__len__', False) else [sd] len_scs = len_sc if getattr(len_sc, '__len__', False) else [len_sc] assert len(shape) == 2, 'only 2d shapes are valid' assert len(sds) == len(len_scs), 'len(sd) differs from len(len_sc)' n = np.prod(shape) kernel = np.sum( np.stack([1 / len_sc * sd * n * ImageProc.gkern2d(shape, 1 / len_sc) for sd, len_sc in zip(sds, len_scs)], axis=2), axis=2) f_img = np.random.normal(0, 1, shape) + np.complex(0, 1) * np.random.normal(0, 1, shape) f_img = np.real(np.fft.ifft2(np.fft.fftshift(kernel * f_img))) return f_img