def denoise(data, num=None, snr=None): """ Denoises nd array with the format n x p x b Parameters: ----------- data : nd array 3-d numpy array with b = band num : int number of bands used snr : int threshold Returns ------- denoised array with same shape as data """ signal = spectral.calc_stats(data) noise = spectral.noise_from_diffs(data) mnfr = spectral.mnf(signal, noise) if num: denoised, trans = mnfr.denoise(data, num=num) print(50*'_') print(trans.shape) elif snr: denoised = mnfr.denoise(data, snr=snr) print("--------------") print(mnfr.num_with_snr(snr=snr)) else: raise ValueError('"snr" or "num" must be given!') return denoised, trans
def test_mnf_all_equals_data(self): '''Test that MNF transform with all components equals original data.''' data = self.data signal = spy.calc_stats(data) noise = spy.noise_from_diffs(data[117:137, 85:122, :]) mnfr = spy.mnf(signal, noise) denoised = mnfr.denoise(data, num=data.shape[-1]) assert_allclose(denoised, data)
def test_mnf_all_equals_data(self): '''Test that MNF transform with all components equals original data.''' data = self.data signal = spy.calc_stats(data) noise = spy.noise_from_diffs(data[117: 137, 85: 122, :]) mnfr = spy.mnf(signal, noise) denoised = mnfr.denoise(data, num=data.shape[-1]) assert(np.allclose(denoised, data))
outstack = r"/home/jb/Downloads/stack_denoised5.tif" with rasterio.open(instack) as intif: stack = intif.read() land = stack[7, :, :] > 500 np.count_nonzero(land) out_meta = intif.meta.copy() (stack[:, land]) = 0 t_stack = np.transpose(stack, (1, 2, 0)) help(spectral.calc_stats) ss = stack.shape[0] print(t_stack.shape) # help(spectral.calc_stats) #view =imshow(t_stack,(8,3,2)) signal = spectral.calc_stats(t_stack) noise = spectral.noise_from_diffs(t_stack) mnfr = spectral.mnf(signal, noise) t_denoised = mnfr.denoise(t_stack, num=5) t_reduced = mnfr.reduce(t_stack, num=5) t_reduced.shape t_denoised.shape tt_denoised = np.transpose(t_denoised, (2, 0, 1)) tt_reduced = np.transpose(t_reduced, (2, 0, 1)) tt_stack = np.transpose(t_stack, (2, 0, 1)) tt_reduced.shape out_meta['count'] = 13 tt_denoised.shape tt_denoised.shape np.max(tt_denoised[2:, ]) np.min(tt_denoised[1:, ]) view = spectral.imshow(t_denoised, (1, 3, 2))