def patch_devignetting(self, ly, lx, margin=1): """ patch-wise devignetting via white image patch from least-squares regression """ # find MIC by indices mic = self.get_coords_by_idx(ly=ly, lx=lx) # slice images wht_win = self._extract_win(self._wht_img, mic, margin) lfp_win = self._extract_win(self._lfp_img, mic, margin) # fit micro image if self.noise_lev > self.noise_th: _, weight_win = self.fit_patch(wht_win) else: weight_win = wht_win / wht_win.max() # apply vignetting correction div_win = np.divide(lfp_win, weight_win, out=np.zeros_like(weight_win), where=weight_win != 0) olap = self._C + margin self._lfp_img[rint(mic[0]) - olap:rint(mic[0]) + olap + 1, rint(mic[1]) - olap:rint(mic[1]) + olap + 1] = div_win return True
def resample_rec(self): # initialize variables required for micro image resampling process self._lfp_out = np.zeros([self._LENS_Y_MAX * self._M, self._LENS_X_MAX * self._M, self._DIMS[2]]) # iterate over each MIC for ly in range(self._LENS_Y_MAX): for lx in range(self._LENS_X_MAX): # find MIC by indices mic = self.get_coords_by_idx(ly=ly, lx=lx) # interpolate each micro image with its MIC as the center with consistent micro image size window = self._lfp_img[rint(mic[0]) - self._C - 1:rint(mic[0]) + self._C + 2, rint(mic[1]) - self._C - 1:rint(mic[1]) + self._C + 2] self._lfp_out[ly * self._M:(ly + 1) * self._M, lx * self._M:(lx + 1) * self._M] = \ self._patch_align(window, mic, method=self._METHOD)[1:-1, 1:-1] # check interrupt status if self.sta.interrupt: return False # print progress status for on console self.sta.progress((ly + 1) / self._LENS_Y_MAX * 100, self.cfg.params[self.cfg.opt_prnt]) return True
def _patch_align(self, window, mic): # initialize patch patch = np.zeros(window.shape) # verify patch shapes as wrong shapes cause crashes if window.shape[0] == self._M + 2 and window.shape[1] == self._M + 2: # iterate through color channels for p in range(self._DIMS[2]): fun = self._interpol_method(range(window.shape[1]), range(window.shape[0]), window[:, :, p]) patch[:, :, p] = fun( np.arange(window.shape[1]) + mic[1] - rint(mic[1]), np.arange(window.shape[0]) + mic[0] - rint(mic[0])) else: self.sta.status_msg( 'Warning: chosen micro image size exceeds light-field borders') return np.zeros((self._M + 2, ) * 2 + (window.shape[2], )) # flip patch to compensate for micro lens rotation patch = np.flip(patch, axis=(0, 1)) if self._flip else patch return patch
def _patch_align(self, window, mic): # initialize patch patch = np.zeros(window.shape) # wrong window shapes cause crashes which is why we if window.shape[0] == self._M + 2 and window.shape[1] == self._M + 2: # iterate through color channels for p in range(self._DIMS[2]): fun = self._interpol_method(range(window.shape[1]), range(window.shape[0]), window[:, :, p]) patch[:, :, p] = fun( np.arange(window.shape[1]) + mic[1] - rint(mic[1]), np.arange(window.shape[0]) + mic[0] - rint(mic[0])) else: self.sta.status_msg( 'Warning: chosen micro image size exceeds light-field borders') return np.zeros((self._M + 2, ) * 2 + (window.shape[2], )) # treatment of interpolated values being below or above original extrema #patch[patch < window.min()] = window.min() #patch[patch > window.max()] = window.max() return patch
def resample_hex(self): # initialize variables required for micro image resampling process patch_stack = np.zeros( [self._LENS_X_MAX, self._M, self._M, self._DIMS[2]]) hex_stretch = int(np.round(2 * self._LENS_X_MAX / np.sqrt(3))) interpol_stack = np.zeros( [hex_stretch, self._M, self._M, self._DIMS[2]]) self._lfp_out = np.zeros( [self._LENS_Y_MAX * self._M, hex_stretch * self._M, self._DIMS[2]]) # check if lower neighbor of upper left MIC is shifted to left or right hex_odd = self._get_hex_direction(self._CENTROIDS) # iterate over each MIC for ly in range(self._LENS_Y_MAX): for lx in range(self._LENS_X_MAX): # find MIC by indices mic = self._get_coords_by_idx(ly=ly, lx=lx) # interpolate each micro image with its MIC as the center and consistent micro image size window = self._lfp_img[rint(mic[0]) - self._C - 1:rint(mic[0]) + self._C + 2, rint(mic[1]) - self._C - 1:rint(mic[1]) + self._C + 2] patch_stack[lx, :, :] = self._patch_align( window, mic, method=self._METHOD)[1:-1, 1:-1] # do interpolation of two adjacent micro images if np.mod(ly + hex_odd, 2) and lx > 0: patch_stack[lx - 1, :, :, :] = (patch_stack[lx - 1, :, :, :] + patch_stack[lx, :, :, :]) / 2. # image stretch interpolation in x-direction to compensate for hex-alignment for y in range(self._M): for x in range(self._M): for p in range(self._DIMS[2]): # stack of micro images elongated in x-direction interpol_vals = np.arange( hex_stretch) / hex_stretch * self._LENS_X_MAX interpol_stack[:, y, x, p] = np.interp(interpol_vals, range(self._LENS_X_MAX), patch_stack[:, y, x, p]) self._lfp_out[ly*self._M:ly*self._M+self._M, :] = \ np.concatenate(interpol_stack, axis=1).reshape((self._M, hex_stretch * self._M, self._DIMS[2])) # check interrupt status if self.sta.interrupt: return False # print progress status self.sta.progress((ly + 1) / self._LENS_Y_MAX * 100, self.cfg.params[self.cfg.opt_prnt])
def _patch_align(window, mic, method='linear'): # initialize patch patch = np.zeros(window.shape) for p in range(window.shape[2]): # careful: interp2d() takes x first and y second fun = interp2d(range(window.shape[1]), range(window.shape[0]), window[:, :, p], kind=method, copy=False) #fun = RectBivariateSpline(range(window.shape[1]), range(window.shape[0]), window[:, :, p]) patch[:, :, p] = fun(np.arange(window.shape[1])+mic[1]-rint(mic[1]), np.arange(window.shape[0])+mic[0]-rint(mic[0])) # treatment of interpolated values being below or above original extrema patch[patch < window.min()] = window.min() patch[patch > window.max()] = window.max() return patch
def patch_devignetting(self, mic): # slice images wht_win = self._wht_img[rint(mic[0]) - self._C - 1:rint(mic[0]) + self._C + 2, rint(mic[1]) - self._C - 1: rint(mic[1]) + self._C + 2] lfp_win = self._lfp_img[rint(mic[0]) - self._C - 1:rint(mic[0]) + self._C + 2, rint(mic[1]) - self._C - 1: rint(mic[1]) + self._C + 2] _, weight_win = self.fit_patch(wht_win) # apply vignetting correction lfp_win /= weight_win return lfp_win
def _extract_win(self, img, mic, margin=0): win = img[rint(mic[0]) - self._C - margin:rint(mic[0]) + self._C + margin + 1, rint(mic[1]) - self._C - margin:rint(mic[1]) + self._C + margin + 1] return win