-
Notifications
You must be signed in to change notification settings - Fork 5
/
musica.py
256 lines (213 loc) · 6.76 KB
/
musica.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (C) 2021 Lafith Mattara
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# contact: lafithmattara@gmail.com
# Script for running MUSICA algorithm on a grayscale image:
import logging
import numpy as np
import copy
from skimage.transform import pyramid_reduce, pyramid_expand
logging.getLogger('matplotlib').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# change level here for console output
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('musica_py.log')
formatter = logging.Formatter(
'%(asctime)s:%(levelname)s:%(name)s: %(message)s'
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_format = logging.Formatter('%(levelname)s:\t%(message)s')
stream_handler.setFormatter(stream_format)
logger.addHandler(stream_handler)
def isPowerofTwo(x):
# check if number x is a power of two
return x and (not(x & (x - 1)))
def findNextPowerOf2(n):
# taken from https://www.techiedelight.com/round-next-highest-power-2/
# Function will find next power of 2
# decrement `n` (to handle cases when `n` itself
# is a power of 2)
n = n - 1
# do till only one bit is left
while n & n - 1:
n = n & n - 1 # unset rightmost bit
# `n` is now a power of two (less than `n`)
# return next power of 2
return n << 1
def resize_image(img):
"""MUSICA works for dimension like 2^N*2^M.
Hence padding is required for arbitrary shapes
Parameters
----------
img : numpy.ndarray
Original image
Returns
-------
numpy.ndarray
Resized image after padding
"""
row, col = img.shape
# check if dimensions are power of two
# if not pad the image accordingly
logger.debug("Calculating how much padding is required...")
if isPowerofTwo(row):
rowdiff = 0
else:
nextpower = findNextPowerOf2(row)
rowdiff = nextpower - row
if isPowerofTwo(col):
coldiff = 0
else:
nextpower = findNextPowerOf2(col)
coldiff = nextpower - col
img_ = np.pad(
img,
((0, rowdiff), (0, coldiff)),
'reflect')
logger.info(
'Image padded from [{},{}] to [{},{}]'.format(
img.shape[0], img.shape[1],
img_.shape[0], img_.shape[1]))
return img_
def gaussian_pyramid(img, L):
"""Function for creating a Gaussian Pyramid
Parameters
----------
img : numpy.ndarray
Input image or g0.
L : Int
Maximum level of decomposition.
Returns
-------
list
list containing images from g0 to gL in order
"""
logger.debug('Creating Gaussian pyramid...')
# Gaussian Pyramid
tmp = copy.deepcopy(img)
gp = [tmp]
for layer in range(L):
logger.debug('creating Layer %d...' % (layer+1))
tmp = pyramid_reduce(tmp, preserve_range=True)
gp.append(tmp)
logger.info('Finished creating Gaussian Pyramid')
return gp
def laplacian_pyramid(img, L):
"""Function for creating Laplacian Pyramid
Parameters
----------
img : numpy.ndarray
Input image or g0.
L : Int
Max layer of decomposition
Returns
-------
list
list containing laplacian layers from L_0 to L_L in order
list
list containing layers of gauss pyramid
"""
gauss = gaussian_pyramid(img, L)
logger.debug('Creating Laplacian pyramid...')
# Laplacian Pyramid:
lp = []
for layer in range(L):
logger.debug('Creating layer %d' % (layer))
tmp = pyramid_expand(gauss[layer+1], preserve_range=True)
tmp = gauss[layer] - tmp
lp.append(tmp)
lp.append(gauss[L])
logger.info("Finished creating Laplacian pyramid")
return lp, gauss
def enhance_coefficients(laplacian, L, params):
"""Non linear operation of pyramid coefficients
Parameters
----------
laplacian : list
Laplacian pyramid of the image.
L : Int
Max layer of decomposition
params : dict
Store values of a, M and p.
Returns
-------
list
List of enhanced pyramid coeffiencts.
"""
logger.debug('Non linear transformation of coefficients...')
# Non linear operation goes here:
M = params['M']
p = params['p']
a = params['a']
for layer in range(L):
logger.info('Modifying Layer %d' % (layer))
x = laplacian[layer]
# removing all negative coefficients:
# an attempt to reduce double edges
x[x < 0] = 0.0
G = a[layer]*M
laplacian[layer] = G*np.multiply(
np.divide(
x, np.abs(x), out=np.zeros_like(x), where=x != 0),
np.power(
np.divide(
np.abs(x), M), p))
return laplacian
def reconstruct_image(laplacian, L):
"""Function for reconstructing original image
from a laplacian pyramid
Parameters
----------
laplacian : list
Laplacian pyramid with enhanced coefficients
L : int
Max level of decomposition
Returns
-------
numpy.ndarray
Resultant image matrix after reconstruction.
"""
logger.debug('Reconstructing image...')
# Reconstructing original image from laplacian pyramid
rs = laplacian[L]
for i in range(L-1, -1, -1):
rs = pyramid_expand(rs, preserve_range=True)
rs = np.add(rs, laplacian[i])
logger.debug('Layer %d completed' % (i))
logger.info(
'Finished reconstructing image from modified pyramid')
return rs
def musica(img, L, params):
"""Function for running MUSICA algorithm
Parameters
----------
img : numpy.ndarray
Input image
L : int
Max level of decomposition
params : dict
Contains parameter values required
for non linear enhancement
plot : bool, optional
To plot the result, by default False
Returns
-------
numpy.ndarray
Final enhanced image with original dimensions
"""
img_resized = resize_image(img)
lp, _ = laplacian_pyramid(img_resized, L)
lp = enhance_coefficients(lp, L, params)
rs = reconstruct_image(lp, L)
rs = rs[:img.shape[0], :img.shape[1]]
return rs