Created
June 12, 2024 19:20
-
-
Save clemisch/20baa603ed5c9f799196f6ae9cf310c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import numba | |
@numba.jit(nopython=True, nogil=True, fastmath=True) | |
def local_median(data, weights, kernel): | |
return median(data * weights * kernel) | |
@numba.jit(nopython=True, nogil=True, fastmath=True, parallel=True) | |
def weighted_median_filter(data, weights, kernel, out): | |
# half length of kernel (rounded down) | |
kh = [int(s / 2.) for s in kernel.shape] | |
for i in range(kh[0], data.shape[0] - kh[0]): | |
for j in range(kh[1], data.shape[1] - kh[1]): | |
for k in range(kh[2], data.shape[2] - kh[2]): | |
i_low, i_high = i - kh[0], i + kh[0] | |
j_low, j_high = j - kh[1], j + kh[1] | |
k_low, k_high = k - kh[2], k + kh[2] | |
local_data = data[i_low:i_high, j_low:j_high, k_low:k_high] | |
local_weights = weights[i_low:i_high, j_low:j_high, k_low:k_high] | |
local_kernel = kernel[i_low:i_high, j_low:j_high, k_low:k_high] | |
out[i, j, k] = median(local_data * local_weights) | |
data = rand(100, 100, 100) | |
kernel = ones((5, 5, 5)) | |
weights = ones_like(data) | |
out = zeros_like(data) | |
weighted_median_filter(data, weights, kernel, out) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment