-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguided_denoiser.py
More file actions
177 lines (136 loc) · 5.6 KB
/
Copy pathguided_denoiser.py
File metadata and controls
177 lines (136 loc) · 5.6 KB
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
# Adapted from CryoSegNet
# https://github.com/jianlin-cheng/CryoSegNet
import numpy as np
import mrcfile
import cv2
from numpy.fft import fft2, ifft2
from scipy.signal import gaussian
def transform(image: np.ndarray) -> np.ndarray:
"""
Normalize and scale an image to 8-bit range (0-255).
Args:
image (np.ndarray): Input image array.
Returns:
np.ndarray: Normalized and scaled 8-bit image.
"""
i_min = image.min()
i_max = image.max()
if i_max == i_min:
# avoid division by zero; return a zero array when input is constant
return np.zeros_like(image, dtype=np.uint8)
image = ((image - i_min) / (i_max - i_min)) * 255
return image.astype(np.uint8)
def standard_scaler(image: np.ndarray) -> np.ndarray:
"""
Apply Gaussian blur and standardize the image to have zero mean and unit variance.
The input is cast to ``float32`` before any OpenCV operations to avoid the
``CV_16F`` kernel-type error that occurs when processing 16‑bit micrographs
(see issue #41). After blurring and normalization we transform the result to
eight‑bit for downstream filters.
Args:
image (np.ndarray): Input image array.
Returns:
np.ndarray: Scaled and transformed image.
"""
# convert to a supported floating point type for OpenCV kernels
image = image.astype(np.float32)
kernel_size = 9
image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
mu = np.mean(image)
sigma = np.std(image)
image = (image - mu) / sigma
return transform(image).astype(np.uint8)
def contrast_enhancement(image: np.ndarray) -> np.ndarray:
"""
Enhance the contrast of the image using Non-Local Means denoising.
Args:
image (np.ndarray): Input image array.
Returns:
np.ndarray: Contrast-enhanced image.
"""
return cv2.fastNlMeansDenoising(image, None, h=10, templateWindowSize=7, searchWindowSize=21)
def gaussian_kernel(kernel_size: int = 3) -> np.ndarray:
"""
Generate a 2D Gaussian kernel.
Args:
kernel_size (int, optional): Size of the kernel. Defaults to 3.
Returns:
np.ndarray: Gaussian kernel.
"""
h = gaussian(kernel_size, kernel_size / 3).reshape(kernel_size, 1)
h = np.dot(h, h.transpose())
h /= np.sum(h)
return h
def wiener_filter(img: np.ndarray, kernel: np.ndarray, K: float) -> np.ndarray:
"""
Apply Wiener filtering to the image.
Args:
img (np.ndarray): Input image array.
kernel (np.ndarray): Convolution kernel.
K (float): Noise-to-signal power ratio.
Returns:
np.ndarray: Filtered image.
"""
kernel /= np.sum(kernel)
dummy = fft2(img)
kernel = fft2(kernel, s=img.shape)
kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
dummy *= kernel
return np.abs(ifft2(dummy))
def clahe(image: np.ndarray) -> np.ndarray:
"""
Apply Contrast Limited Adaptive Histogram Equalization (CLAHE) to the image.
Args:
image (np.ndarray): Input image array.
Returns:
np.ndarray: CLAHE-processed image.
"""
clahe_instance = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(16, 16))
return clahe_instance.apply(transform(image))
def guided_filter(input_image: np.ndarray, guidance_image: np.ndarray, radius: int = 20, epsilon: float = 0.1) -> np.ndarray:
"""
Apply guided filtering to refine the image using a guidance image.
Args:
input_image (np.ndarray): Input image array.
guidance_image (np.ndarray): Guidance image array.
radius (int, optional): Radius of the filter. Defaults to 20.
epsilon (float, optional): Regularization parameter. Defaults to 0.1.
Returns:
np.ndarray: Filtered image.
"""
input_image = input_image.astype(np.float32) / 255.0
guidance_image = guidance_image.astype(np.float32) / 255.0
mean_guidance = cv2.boxFilter(guidance_image, -1, (radius, radius))
mean_input = cv2.boxFilter(input_image, -1, (radius, radius))
mean_guidance_input = cv2.boxFilter(guidance_image * input_image, -1, (radius, radius))
covariance_guidance_input = mean_guidance_input - mean_guidance * mean_input
mean_guidance_sq = cv2.boxFilter(guidance_image * guidance_image, -1, (radius, radius))
variance_guidance = mean_guidance_sq - mean_guidance * mean_guidance
a = covariance_guidance_input / (variance_guidance + epsilon)
b = mean_input - a * mean_guidance
mean_a = cv2.boxFilter(a, -1, (radius, radius))
mean_b = cv2.boxFilter(b, -1, (radius, radius))
output_image = mean_a * guidance_image + mean_b
return transform(output_image)
def denoise(image_path: str) -> np.ndarray:
"""
Apply a sequence of denoising operations to an input image.
Args:
image_path (str): Path to the input .mrc image file.
Returns:
np.ndarray: Fully denoised image.
"""
kernel = gaussian_kernel(kernel_size=9)
image = mrcfile.read(image_path)
# some MRCs are stored as 16‑bit integers; ensure we work in float32 so that
# subsequent OpenCV calls (GaussianBlur, etc.) don't raise the ktype error
# described in https://github.com/WEHI-ResearchComputing/PartiNet/issues/41
image = image.astype(np.float32)
image = image.T
image = np.rot90(image)
normalized_image = standard_scaler(np.array(image))
contrast_enhanced_image = contrast_enhancement(normalized_image)
weiner_filtered_image = wiener_filter(contrast_enhanced_image, kernel, K=30)
clahe_image = clahe(weiner_filtered_image)
guided_filter_image = guided_filter(clahe_image, weiner_filtered_image)
return guided_filter_image