Blend loaded images at `level` of granularity using `wavelet`
(base, texture, wavelet, level, mode='smooth', base_gain=None,
texture_gain=None)
| 89 | |
| 90 | |
| 91 | def blend_images(base, texture, wavelet, level, mode='smooth', base_gain=None, |
| 92 | texture_gain=None): |
| 93 | """Blend loaded images at `level` of granularity using `wavelet`""" |
| 94 | |
| 95 | base_data = image2array(base) |
| 96 | texture_data = image2array(texture) |
| 97 | output_data = [] |
| 98 | |
| 99 | # process color bands |
| 100 | for base_band, texture_band in zip(base_data, texture_data): |
| 101 | # multilevel dwt |
| 102 | base_band_coeffs = pywt.wavedec2(base_band, wavelet, mode, level) |
| 103 | texture_band_coeffs = pywt.wavedec2(texture_band, wavelet, mode, level) |
| 104 | |
| 105 | # average coefficients of base image |
| 106 | output_band_coeffs = [base_band_coeffs[0]] # cA |
| 107 | del base_band_coeffs[0], texture_band_coeffs[0] |
| 108 | |
| 109 | # blend details coefficients |
| 110 | for n, (base_band_details, texture_band_details) in enumerate( |
| 111 | zip(base_band_coeffs, texture_band_coeffs)): |
| 112 | blended_details = [] |
| 113 | for (base_detail, texture_detail) in zip(base_band_details, |
| 114 | texture_band_details): |
| 115 | if base_gain is not None: |
| 116 | base_detail *= base_gain |
| 117 | if texture_gain is not None: |
| 118 | texture_detail *= texture_gain |
| 119 | |
| 120 | # select coeffs with greater energy |
| 121 | blended = numpy.where(abs(base_detail) > abs(texture_detail), |
| 122 | base_detail, texture_detail) |
| 123 | blended_details.append(blended) |
| 124 | |
| 125 | base_band_coeffs[n] = texture_band_coeffs[n] = None |
| 126 | output_band_coeffs.append(blended_details) |
| 127 | |
| 128 | # multilevel idwt |
| 129 | new_band = pywt.waverec2(output_band_coeffs, wavelet, mode) |
| 130 | output_data.append(new_band) |
| 131 | del new_band, base_band_coeffs, texture_band_coeffs |
| 132 | |
| 133 | del base_data, texture_data |
| 134 | output_data = numpy.array(output_data) |
| 135 | |
| 136 | return array2image(output_data, base.mode) |
| 137 | |
| 138 | |
| 139 | def main(): |
no test coverage detected