def __init__(self, *args, **kwargs): super(LfpDevignetter, self).__init__(*args, **kwargs) # threshold from white image intensity distribution (key to find balance between edges turning black or white) default_thresh = np.mean(self._wht_img / self._wht_img.max()) - np.std( self._wht_img / self._wht_img.max()) self._th = kwargs['th'] if 'th' in kwargs else default_thresh # noise level for decision making whether division by raw image or fit values self._noise_lev = kwargs['noise_lev'] if 'noise_lev' in kwargs else None self._noise_th = 0.1 self._patch_mode = False self._lfp_div = np.zeros(self._lfp_img.shape) # white balance if len(self._wht_img.shape) == 3: # balance RGB channels in white image #self._wht_img = misc.eq_channels(self._wht_img) self._wht_img = misc.rgb2gray(self._wht_img)[..., np.newaxis] # check for same dimensionality self._wht_img = self._wht_img if len(self._wht_img.shape) == len( self._lfp_img.shape) else misc.rgb2gray(self._wht_img)
def export_vp_stack(self, type='png', downscale=None): # print status self.sta.status_msg('Write viewpoint image stack', self.cfg.params[self.cfg.opt_prnt]) self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt]) # downscale image downscale = True if downscale is None else downscale views_stacked_img = misc.img_resize(self.views_stacked_img.copy(), 1 / self._M) \ if downscale else self.views_stacked_img.copy() # normalization p_lo = np.percentile(misc.rgb2gray(self.central_view), 0.05) p_hi = np.percentile(misc.rgb2gray(self.central_view), 99.995) views_stacked_img = misc.Normalizer(views_stacked_img, min=p_lo, max=p_hi).uint8_norm() # export all viewpoints in single image views_stacked_path = os.path.join( self.cfg.exp_path, 'views_stacked_img_' + str(self._M) + 'px') misc.save_img_file(views_stacked_img, file_path=views_stacked_path, file_type=type) self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt]) return True
def blur_metric(img_tile): ''' img_tile : cropped image ''' img = misc.rgb2gray(img_tile) if len(img_tile.shape) == 3 else img_tile y, x = img.shape magnitude = np.abs(fftpack.fft2(img)) magnitudeCrop = magnitude[:int(np.ceil(y/2)), :int(np.ceil(x/2))] #figure, imshow(magnitude,[0 1000]), colormap gray # magnitude F2 = fftpack.fftshift(magnitude) psd2D = np.abs(F2) ** 2 #plt.figure(1) #plt.imshow(psd2D/np.percentile(psd2D, 95)) #plt.show() # total energy TE = sum(sum(magnitudeCrop**2)) # high frequency energy freq_bounds = (int(np.ceil(y/500)), int(np.ceil(x/500))) HE = TE - sum(sum(magnitudeCrop[:freq_bounds[0], :freq_bounds[1]]**2)) # energy ratio (Sharpness) S = HE/TE return S
def michelson_contrast(img_tile): ''' https://colorusage.arc.nasa.gov/luminance_cont.php ''' #lum_tile = misc.yuv_conv(img_tile)[..., 0] lum_tile = misc.rgb2gray(img_tile) c_m = (lum_tile.max() - lum_tile.min()) / (lum_tile.max() + lum_tile.min()) return c_m
def auto_hist_align(img, ref_img, opt=None): if opt: p_lo, p_hi = (0.005, 99.9) #(0.001, 99.999) min_perc = np.percentile(misc.rgb2gray(ref_img), p_lo) max_perc = np.percentile(ref_img, p_hi) else: p_lo, p_hi = (0.5, 99.9) min_perc = np.percentile(ref_img, p_lo) max_perc = np.percentile(ref_img, p_hi) img = misc.Normalizer(img, min=min_perc, max=max_perc).type_norm() return img
def _estimate_noise_level(self): ''' estimate white image noise level ''' # print status self.sta.status_msg('Estimate white image noise level', self.cfg.params[self.cfg.opt_prnt]) self.sta.progress(None, self.cfg.params[self.cfg.opt_prnt]) M = np.mean(self.cfg.calibs[self.cfg.ptc_mean]) lp_kernel = misc.create_gauss_kernel(l=M) bw_img = misc.rgb2gray(self._wht_img) if len( self._wht_img.shape) == 3 else self._wht_img flt_img = convolve2d(bw_img, lp_kernel, 'same') self.sta.progress(100, self.cfg.params[self.cfg.opt_prnt]) return np.std(bw_img - flt_img)
def estimate_gamma(self, img: np.ndarray = None) -> float: img = self._img if img is None else np.asarray(img, dtype='float64') # try extract luminance try: from plenopticam import misc lum = misc.rgb2gray(img) except ImportError: lum = img # normalize lum /= lum.max() #self._gam = 1/np.log(img.mean())/np.log((img.max()-img.min())/2) #self._gam = -.3/np.log10(np.mean(img/img.max())) self._gam = 1 / np.log(np.mean(lum / lum.max())) / np.log(.5) return self._gam
def __init__(self, img, centroids, cfg=None): # input variables self._img = uint8_norm(rgb2gray(img.copy())) self._centroids = np.asarray(centroids) self.cfg = cfg if cfg is not None else Config()
def main(self): if self._wht_img is None: self.sta.status_msg(msg='White image file not present', opt=self.cfg.params[self.cfg.opt_prnt]) self.sta.error = True # convert Bayer to RGB representation if len(self._wht_img.shape) == 2 and 'bay' in self.cfg.lfpimg: # perform color filter array management and obtain rgb image cfa_obj = CfaProcessor(bay_img=self._wht_img, cfg=self.cfg, sta=self.sta) cfa_obj.bay2rgb() self._wht_img = cfa_obj.rgb_img del cfa_obj # ensure white image is monochromatic self._wht_img = rgb2gray(self._wht_img) if len( self._wht_img.shape) == 3 else self._wht_img # estimate micro image diameter obj = PitchEstimator(self._wht_img, self.cfg, self.sta) obj.main() self._M = obj.M if not self._M else self._M del obj # compute all centroids of micro images obj = CentroidExtractor(self._wht_img, self.cfg, self.sta, self._M, method='area') obj.main() centroids = obj.centroids del obj # write micro image center image to hard drive if debug option is set if not self.sta.interrupt: CentroidDrawer( self._wht_img, centroids, self.cfg, self.sta).write_centroids_img(fn='wht_img+mics_unsorted.png') # reorder MICs and assign indices based on the detected MLA pattern obj = CentroidSorter(centroids, self.cfg, self.sta) obj.main() mic_list, pattern, pitch = obj.mic_list, obj.pattern, obj.pitch del obj # save calibration metadata self.sta.status_msg('Save calibration data', opt=self.cfg.params[self.cfg.opt_prnt]) self.sta.progress(None, opt=self.cfg.params[self.cfg.opt_prnt]) try: self.cfg.save_cal_data(mic_list=mic_list, pat_type=pattern, ptc_mean=pitch) self.sta.progress(100, opt=self.cfg.params[self.cfg.opt_prnt]) except PermissionError: self.sta.status_msg('Could not save calibration data', opt=self.cfg.params[self.cfg.opt_prnt]) # write image to hard drive (only if debug option is set) CentroidDrawer( self._wht_img, mic_list, self.cfg, self.sta).write_centroids_img(fn='wht_img+mics_sorted.png') return True
def __init__(self, img, centroids, cfg=None): # input variables self._img = Normalizer(rgb2gray(img.copy())).uint8_norm() self._centroids = np.asarray(centroids) self.cfg = cfg if cfg is not None else PlenopticamConfig()