-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldscale.py
executable file
·187 lines (149 loc) · 7.2 KB
/
fieldscale.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
"""
Source code for the paper:
Fieldscale: Locality-Aware Field-based Adaptive Rescaling for Thermal Infrared Image
Hyeonjae Gil, Myeon-Hwan Jeon, and Ayoung Kim
Please cite the paper if you use this code.
@article{gil2024fieldscale,
title={Fieldscale: Locality-Aware Field-based Adaptive Rescaling for Thermal Infrared Image},
author={Gil, Hyeonjae and Jeon, Myung-Hwan and Kim, Ayoung},
journal={IEEE Robotics and Automation Letters},
year={2024},
publisher={IEEE}
}
Original author: Hyeonjae Gil
Author email: [email protected]
"""
import numpy as np
import cv2
from typing import Literal, Union
def gridwise_min(image: np.ndarray, grid_shape: tuple = (1, 1)) -> np.ndarray:
"""Return the minimum value of each patch in the image."""
patch_shape = (image.shape[0] // grid_shape[0], image.shape[1] // grid_shape[1])
output = np.zeros(grid_shape, dtype=image.dtype)
for i, j in np.ndindex(grid_shape):
output[i, j] = np.amin(image[
patch_shape[0] * i : patch_shape[0] * (i + 1),
patch_shape[1] * j : patch_shape[1] * (j + 1)
])
return output
def gridwise_max(image: np.ndarray, grid_shape: tuple = (1, 1)) -> np.ndarray:
"""Return the maximum value of each patch in the image."""
patch_shape = (image.shape[0] // grid_shape[0], image.shape[1] // grid_shape[1])
output = np.zeros(grid_shape, dtype=image.dtype)
for i, j in np.ndindex(grid_shape):
output[i, j] = np.amax(image[
patch_shape[0] * i : patch_shape[0] * (i + 1),
patch_shape[1] * j : patch_shape[1] * (j + 1)
])
return output
def get_neighbor_grids(grid: np.ndarray, xy: tuple, max_distance: int = 1) -> list:
"""Return the neighbors of a pixel in a grid."""
h, w = grid.shape
x, y = xy
neighbors = [
(x + i, y + j)
for i in range(-max_distance, max_distance + 1)
for j in range(-max_distance, max_distance + 1)
if (i != 0 or j != 0) and (0 <= x + i < h) and (0 <= y + j < w)
]
return sorted(neighbors, key=lambda k: (k[0], k[1]))
def local_extrema_suppression(grid: np.ndarray,
local_distance: int,
diff_threshold: float,
extrema: Literal['max', 'min']) -> np.ndarray:
"""Clip the extreme values in the grid."""
assert extrema in ['max', 'min']
if local_distance <= 0 or diff_threshold <= 0:
return grid
for i, j in np.ndindex(grid.shape):
neighbors = get_neighbor_grids(grid, (i, j), max_distance=local_distance)
neighbor_values = np.array([grid[xy] for xy in neighbors])
if extrema == 'max' and grid[i, j] >= neighbor_values.max():
diff = grid[i, j] - neighbor_values.mean()
if diff > diff_threshold:
grid[i, j] = neighbor_values.mean() + diff_threshold
elif extrema == 'min' and grid[i, j] <= neighbor_values.min():
diff = neighbor_values.mean() - grid[i, j]
if diff > diff_threshold:
grid[i, j] = neighbor_values.mean() - diff_threshold
return grid
def message_passing(grid: np.ndarray,
direction: Literal['increase', 'decrease']) -> np.ndarray:
"""Message passing algorithm for grid."""
assert direction in ['increase', 'decrease']
grid_new = np.zeros_like(grid, dtype=np.float64)
for i, j in np.ndindex(grid.shape):
neighbors = get_neighbor_grids(grid, (i, j), max_distance=1)
neighbors_value = [grid[neighbor] for neighbor in neighbors]
mean = np.mean(neighbors_value + [grid[i, j]])
bigger, smaller = (mean, grid[i, j]) if mean > grid[i, j] else (grid[i, j], mean)
grid_new[i, j] = bigger if direction == 'increase' else smaller
return grid_new
def rescale_image_with_fields(image: np.ndarray,
min_field: np.ndarray,
max_field: np.ndarray) -> np.ndarray:
"""Rescale the image with min_field and max_field as the lower and upper bound."""
assert image.shape == min_field.shape == max_field.shape
image = image.astype(np.float64)
min_field = min_field.astype(np.float64)
max_field = max_field.astype(np.float64)
min_field = np.where(min_field > max_field, max_field, min_field)
max_field = np.where(max_field < min_field, min_field, max_field)
image = np.clip(image, min_field, max_field)
image = (image - min_field) / (max_field - min_field) * 255
return image.astype(np.uint8)
class Fieldscale:
def __init__(self, max_diff: float = 400, min_diff: float = 400,
iteration: int = 7, gamma: float = 1.5,
clahe: bool = True, video: bool = False):
assert max_diff >= 0 and isinstance(max_diff, (int, float))
assert min_diff >= 0 and isinstance(min_diff, (int, float))
assert iteration > 0 and isinstance(iteration, int)
assert gamma > 0 and isinstance(gamma, (int, float))
assert isinstance(clahe, bool)
assert isinstance(video, bool)
self.max_diff = max_diff
self.min_diff = min_diff
self.iteration = iteration
self.gamma = gamma
self.clahe = clahe
self.video = video
self.prev_min_field = None
self.prev_max_field = None
def __call__(self, input: Union[str, np.ndarray]) -> np.ndarray:
"""Process an image or a path to an image."""
if isinstance(input, str):
image = cv2.imread(input, -1)
if image is None:
raise ValueError(f"Unable to read image from path: {input}")
elif isinstance(input, np.ndarray):
image = input
else:
raise TypeError("Input should be a file path or an numpy.ndarray.")
min_grid = gridwise_min(image, (8, 8))
max_grid = gridwise_max(image, (8, 8))
max_grid = local_extrema_suppression(
max_grid, local_distance=2, diff_threshold=self.max_diff, extrema='max'
)
max_grid = local_extrema_suppression(
max_grid, local_distance=2, diff_threshold=self.min_diff, extrema='min'
)
for _ in range(self.iteration):
min_grid = message_passing(min_grid, direction='decrease').astype(np.float64)
max_grid = message_passing(max_grid, direction='increase').astype(np.float64)
min_field = cv2.resize(min_grid, dsize=(image.shape[1], image.shape[0]),
interpolation=cv2.INTER_LINEAR)
max_field = cv2.resize(max_grid, dsize=(image.shape[1], image.shape[0]),
interpolation=cv2.INTER_LINEAR)
if self.video and self.prev_min_field is not None:
min_field = 0.1 * min_field + 0.9 * self.prev_min_field
max_field = 0.1 * max_field + 0.9 * self.prev_max_field
self.prev_min_field = min_field
self.prev_max_field = max_field
rescaled = rescale_image_with_fields(image, min_field, max_field)
if self.gamma > 0:
rescaled = (255 * np.power(rescaled.astype(np.float64) / 255, self.gamma)).astype(np.uint8)
if self.clahe:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
rescaled = clahe.apply(rescaled)
return rescaled