def _denoise_band(X, wavelet, levels, alpha): from var_est import variance if alpha is None: alpha = 2 decomp = pywt.wavedec2(X, wavelet, level=levels) for i, all_coeff in enumerate(decomp[1:]): minvar = np.empty(all_coeff[0].shape, dtype=float) minvar.fill(np.inf) # Handle horizontal, vertical and diagonal coefficients for coeff in all_coeff: for win_size in (3, 5, 7, 9): var = variance(coeff, win_size) mask = (var < minvar) minvar[mask] = var[mask] # Wiener estimator coeff *= (minvar / (minvar + alpha)) rec = pywt.waverec2(decomp, wavelet) rows, cols = X.shape if X.shape != rec.shape: rows_mod = rows % 2 cols_mod = cols % 2 return rec[rows_mod:, cols_mod:] else: return rec
def blend_images(base, texture, wavelet, level, mode='sp1', base_gain=None, texture_gain=None): """Blend loaded images at `level` of granularity using `wavelet`""" base_data = image2array(base) texture_data = image2array(texture) output_data = [] # process color bands for base_band, texture_band in zip(base_data, texture_data): # multilevel dwt base_band_coeffs = pywt.wavedec2(base_band, wavelet, mode, level) texture_band_coeffs = pywt.wavedec2(texture_band, wavelet, mode, level) # average coefficients of base image output_band_coeffs = [base_band_coeffs[0]] # cA del base_band_coeffs[0], texture_band_coeffs[0] # blend details coefficints for n, (base_band_details, texture_band_details) in enumerate(zip(base_band_coeffs, texture_band_coeffs)): blended_details = [] for (base_detail, texture_detail) in zip(base_band_details, texture_band_details): if base_gain is not None: base_detail *= base_gain if texture_gain is not None: texture_detail *= texture_gain # select coeffs with greater energy blended = numpy.where(abs(base_detail) > abs(texture_detail), base_detail, texture_detail) blended_details.append(blended) base_band_coeffs[n] = texture_band_coeffs[n] = None output_band_coeffs.append(blended_details) # multilevel idwt new_band = pywt.waverec2(output_band_coeffs, wavelet, mode) output_data.append(new_band) del new_band, base_band_coeffs, texture_band_coeffs del base_data, texture_data output_data = numpy.array(output_data) return array2image(output_data, base.mode)