@@ -12,7 +12,6 @@ def get_bayer_indices(pattern):
1212 Get (x_start_idx, y_start_idx) for R, Gr, Gb, and B channels
1313 in Bayer array, respectively
1414 """
15-
1615 return {'gbrg' : ((0 , 1 ), (1 , 1 ), (0 , 0 ), (1 , 0 )),
1716 'rggb' : ((0 , 0 ), (1 , 0 ), (0 , 1 ), (1 , 1 )),
1817 'bggr' : ((1 , 1 ), (0 , 1 ), (1 , 0 ), (0 , 0 )),
@@ -26,7 +25,6 @@ def split_bayer(bayer_array, bayer_pattern):
2625 :param bayer_pattern: 'gbrg' | 'rggb' | 'bggr' | 'grbg'
2726 :return: 4-element list of R, Gr, Gb, and B channel sub-arrays, each is an np.ndarray(H/2, W/2)
2827 """
29-
3028 rggb_indices = get_bayer_indices (bayer_pattern )
3129
3230 sub_arrays = []
@@ -47,7 +45,6 @@ def reconstruct_bayer(sub_arrays, bayer_pattern):
4745 :param bayer_pattern: 'gbrg' | 'rggb' | 'bggr' | 'grbg'
4846 :return: np.ndarray(H, W)
4947 """
50-
5148 rggb_indices = get_bayer_indices (bayer_pattern )
5249
5350 height , width = sub_arrays [0 ].shape
@@ -71,7 +68,6 @@ def pad(array, pads, mode='reflect'):
7168 :param mode: padding mode, see np.pad
7269 :return: padded array: np.ndarray(H', W', ...)
7370 """
74-
7571 if isinstance (pads , (list , tuple , np .ndarray )):
7672 if len (pads ) == 2 :
7773 pads = ((pads [0 ], pads [0 ]), (pads [1 ], pads [1 ])) + ((0 , 0 ),) * (array .ndim - 2 )
@@ -93,7 +89,6 @@ def crop(array, crops):
9389 if 4-element sequence: (top crop, bottom crop, left crop, right crop)
9490 :return: cropped array: np.ndarray(H', W', ...)
9591 """
96-
9792 if isinstance (crops , (list , tuple , np .ndarray )):
9893 if len (crops ) == 2 :
9994 top_crop = bottom_crop = crops [0 ]
@@ -117,7 +112,6 @@ def shift_array(padded_array, window_size):
117112 :return: a generator of length (2r+1)*(2r+1), each is an np.ndarray(H, W), and the original
118113 array before padding locates in the middle of the generator
119114 """
120-
121115 wy , wx = window_size if isinstance (window_size , (list , tuple )) else (window_size , window_size )
122116 assert wy % 2 == 1 and wx % 2 == 1 , 'only odd window size is valid'
123117
@@ -150,50 +144,48 @@ def gen_gaussian_kernel(kernel_size, sigma):
150144 return kernel / kernel .sum ()
151145
152146
153- def mean_filter (array , filter_size = 3 ):
154- """
155- A faster reimplementation of the mean-filter
156- :param array: array to be filter: np.ndarray(H, W, ...)
157- :param filter_size: int, diameter of the mean-filter
158- :return: filtered array: np.ndarray(H, W, ...)
147+ def generic_filter (array , kernel ):
159148 """
160-
161- assert filter_size % 2 == 1 , 'only odd filter size is valid'
162-
163- padded_array = pad (array , pads = filter_size // 2 )
164- shifted_arrays = shift_array (padded_array , window_size = filter_size )
165- return (sum (shifted_arrays ) / filter_size ** 2 ).astype (array .dtype )
166-
167-
168- def gaussian_filter (array , kernel ):
169- """
170- A faster reimplementation of the bilateral-filter
149+ Filter input image array with given kernel
171150 :param array: array to be filter: np.ndarray(H, W, ...), must be np.int dtype
172151 :param kernel: np.ndarray(h, w)
173152 :return: filtered array: np.ndarray(H, W, ...)
174153 """
175-
176154 kh , kw = kernel .shape [:2 ]
177155 kernel = kernel .flatten ()
178156
179157 padded_array = pad (array , pads = (kh // 2 , kw // 2 ))
180158 shifted_arrays = shift_array (padded_array , window_size = (kh , kw ))
181159
182- gf_array = np .zeros_like (array )
160+ filtered_array = np .zeros_like (array )
183161 weights = np .zeros_like (array )
184162
185163 for i , shifted_array in enumerate (shifted_arrays ):
186- gf_array += kernel [i ] * shifted_array
164+ filtered_array += kernel [i ] * shifted_array
187165 weights += kernel [i ]
188166
189- gf_array = (gf_array / weights ).astype (array .dtype )
167+ filtered_array = (filtered_array / weights ).astype (array .dtype )
168+ return filtered_array
190169
191- return gf_array
170+
171+ def mean_filter (array , filter_size = 3 ):
172+ """
173+ A faster reimplementation of the mean filter
174+ :param array: array to be filter: np.ndarray(H, W, ...)
175+ :param filter_size: int, diameter of the mean-filter
176+ :return: filtered array: np.ndarray(H, W, ...)
177+ """
178+
179+ assert filter_size % 2 == 1 , 'only odd filter size is valid'
180+
181+ padded_array = pad (array , pads = filter_size // 2 )
182+ shifted_arrays = shift_array (padded_array , window_size = filter_size )
183+ return (sum (shifted_arrays ) / filter_size ** 2 ).astype (array .dtype )
192184
193185
194186def bilateral_filter (array , spatial_weights , intensity_weights_lut , right_shift = 0 ):
195187 """
196- A faster reimplementation of the bilateral- filter
188+ A faster reimplementation of the bilateral filter
197189 :param array: array to be filter: np.ndarray(H, W, ...), must be np.int dtype
198190 :param spatial_weights: np.ndarray(h, w): predefined spatial gaussian kernel, where h and w are
199191 kernel height and width respectively
@@ -202,7 +194,6 @@ def bilateral_filter(array, spatial_weights, intensity_weights_lut, right_shift=
202194 right to avoid integer overflow when multiply this result to the input array
203195 :return: filtered array: np.ndarray(H, W, ...)
204196 """
205-
206197 filter_height , filter_width = spatial_weights .shape [:2 ]
207198 spatial_weights = spatial_weights .flatten ()
208199
@@ -222,4 +213,4 @@ def bilateral_filter(array, spatial_weights, intensity_weights_lut, right_shift=
222213
223214 bf_array = (bf_array / weights ).astype (array .dtype )
224215
225- return bf_array
216+ return bf_array
0 commit comments