Repository navigation
Expand file tree
/
Copy pathimage_processing.py
More file actions
422 lines (323 loc) · 11 KB
/
Copy pathimage_processing.py
File metadata and controls
422 lines (323 loc) · 11 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
__all__ = [
"color_space",
"gray2rgb",
"hsv2rgb",
"rgb2gray",
"rgb2hsv",
"rgb2ycbcr",
"anisotropic_diffusion",
"bilateral",
"canny",
"inverse_deconv",
"iterative_deconv",
"maxfilt",
"mean_shift",
"medfilt",
"medfilt1",
"medfilt2",
"minfilt",
"sat",
"sobel_operator",
"gaussian_kernel",
"hist_equal",
"histogram",
"resize",
"rotate",
"scale",
"skew",
"transform",
"transform_coordinates",
"translate",
"confidence_cc",
"regions",
"dilate",
"erode",
"wrap",
"unwrap",
]
from typing import cast
import arrayfire_wrapper.lib as wrapper
from arrayfire import Array
from arrayfire.array_object import afarray_as_array
from arrayfire.dtypes import Dtype, float32
from arrayfire.library.constants import (
CannyThreshold,
Connectivity,
CSpace,
Diffusion,
Flux,
Interp,
IterativeDeconv,
Pad,
YCCStd,
)
from .array_functions import constant
from .vector_algorithms import max as afmax
from .vector_algorithms import min as afmin
# Colorspace conversions
@afarray_as_array
def color_space(image: Array, to_type: CSpace, from_type: CSpace, /) -> Array:
return cast(Array, wrapper.color_space(image.arr, to_type, from_type))
@afarray_as_array
def gray2rgb(image: Array, /, *, r_factor: float = 1.0, g_factor: float = 1.0, b_factor: float = 1.0) -> Array:
return cast(Array, wrapper.gray2rgb(image.arr, r_factor, g_factor, b_factor))
@afarray_as_array
def hsv2rgb(image: Array, /) -> Array:
return cast(Array, wrapper.hsv2rgb(image.arr))
@afarray_as_array
def rgb2gray(
image: Array, /, *, r_factor: float = 0.2126, g_factor: float = 0.7152, b_factor: float = 0.0722
) -> Array:
return cast(Array, wrapper.rgb2gray(image.arr, r_factor, g_factor, b_factor))
@afarray_as_array
def rgb2hsv(image: Array, /) -> Array:
return cast(Array, wrapper.rgb2hsv(image.arr))
@afarray_as_array
def rgb2ycbcr(image: Array, /, *, standard: YCCStd = YCCStd.YCC_601) -> Array:
return cast(Array, wrapper.rgb2ycbcr(image.arr, standard))
@afarray_as_array
def ycbcr2rgb(image: Array, /, *, standard: YCCStd = YCCStd.YCC_601) -> Array:
return cast(Array, wrapper.ycbcr2rgb(image.arr, standard))
# Filters
@afarray_as_array
def anisotropic_diffusion(
image: Array,
timestep: float,
conductance: float,
iterations: int,
/,
*,
fftype: Flux = Flux.QUADRATIC,
diffusion_kind: Diffusion = Diffusion.GRAD,
) -> Array:
return cast(
Array, wrapper.anisotropic_diffusion(image.arr, timestep, conductance, iterations, fftype, diffusion_kind)
)
@afarray_as_array
def bilateral(image: Array, spatial_sigma: float, chromatic_sigma: float, /, *, is_color: bool = False) -> Array:
return cast(Array, wrapper.bilateral(image.arr, spatial_sigma, chromatic_sigma, is_color))
@afarray_as_array
def canny(
image: Array,
low_threshold: float,
high_threshold: float,
/,
*,
threshold_type: CannyThreshold = CannyThreshold.MANUAL,
sobel_window: int = 3,
is_fast: bool = False,
) -> Array:
return cast(Array, wrapper.canny(image.arr, threshold_type, low_threshold, high_threshold, sobel_window, is_fast))
@afarray_as_array
def inverse_deconv(
image: Array, psf: Array, gamma: float, /, *, algo: IterativeDeconv = IterativeDeconv.DEFAULT
) -> Array:
return cast(Array, wrapper.inverse_deconv(image.arr, psf.arr, gamma, algo))
@afarray_as_array
def iterative_deconv(
image: Array,
psf: Array,
iterations: int,
relax_factor: float,
/,
*,
algo: IterativeDeconv = IterativeDeconv.DEFAULT,
) -> Array:
return cast(Array, wrapper.iterative_deconv(image.arr, psf.arr, iterations, relax_factor, algo))
@afarray_as_array
def maxfilt(image: Array, /, *, wind_length: int = 3, wind_width: int = 3, edge_pad: Pad = Pad.ZERO) -> Array:
return cast(Array, wrapper.maxfilt(image.arr, wind_length, wind_width, edge_pad))
@afarray_as_array
def mean_shift(
image: Array, spatial_sigma: float, chromatic_sigma: float, iterations: int, /, *, is_color: bool = False
) -> Array:
return cast(Array, wrapper.mean_shift(image.arr, spatial_sigma, chromatic_sigma, iterations, is_color))
@afarray_as_array
def medfilt(image: Array, /, *, wind_length: int = 3, wind_width: int = 3, edge_pad: Pad = Pad.ZERO) -> Array:
return cast(Array, wrapper.medfilt(image.arr, wind_length, wind_width, edge_pad))
@afarray_as_array
def medfilt1(image: Array, /, *, wind_width: int = 3, edge_pad: Pad = Pad.ZERO) -> Array:
return cast(Array, wrapper.medfilt1(image.arr, wind_width, edge_pad))
@afarray_as_array
def medfilt2(image: Array, /, *, wind_length: int = 3, wind_width: int = 3, edge_pad: Pad = Pad.ZERO) -> Array:
return cast(Array, wrapper.medfilt2(image.arr, wind_length, wind_width, edge_pad))
@afarray_as_array
def minfilt(image: Array, /, *, wind_length: int = 3, wind_width: int = 3, edge_pad: Pad = Pad.ZERO) -> Array:
return cast(Array, wrapper.minfilt(image.arr, wind_length, wind_width, edge_pad))
@afarray_as_array
def sat(image: Array, /) -> Array:
return cast(Array, wrapper.sat(image.arr))
def sobel_operator(image: Array, /, *, kernel_size: int = 3) -> tuple[Array, Array]:
dx, dy = wrapper.sobel_operator(image.arr, kernel_size)
return Array.from_afarray(dx), Array.from_afarray(dy)
# Gaussian kernel
def gaussian_kernel(
rows: int, columns: int, /, *, rows_sigma: None | float = None, columns_sigma: None | float = None
) -> Array:
rs = 0.25 * rows + 0.75 if not rows_sigma else rows_sigma
cs = 0.25 * columns + 0.75 if not columns_sigma else columns_sigma
return cast(Array, wrapper.gaussian_kernel(rows, columns, rs, cs))
# Histograms
@afarray_as_array
def hist_equal(image: Array, histogram: Array, /) -> Array:
return cast(Array, wrapper.hist_equal(image.arr, histogram.arr))
@afarray_as_array
def histogram(
image: Array, n_bins: int, /, *, min_value: None | float = None, max_value: None | float = None
) -> Array:
min_v = afmin(image) if not min_value else min_value
max_v = afmax(image) if not max_value else max_value
return cast(Array, wrapper.histogram(image.arr, n_bins, min_v, max_v)) # type: ignore[arg-type]
# Image transformation
@afarray_as_array
def resize(image: Array, size_scale: float | tuple[int, int], /, *, method: Interp = Interp.NEAREST) -> Array:
if isinstance(size_scale, float):
dims = (int(size_scale * image.shape[0]), int(size_scale * image.shape[1]))
elif isinstance(size_scale, tuple):
dims = size_scale
else:
raise TypeError(
"`size_scale` should be specified either as scale of original image, or the specific output size."
)
return cast(Array, wrapper.resize(image.arr, dims[0], dims[1], method))
@afarray_as_array
def rotate(image: Array, theta: float, /, *, is_crop: bool = True, method: Interp = Interp.NEAREST) -> Array:
return cast(Array, wrapper.rotate(image.arr, theta, is_crop, method))
@afarray_as_array
def scale(
image: Array,
scale: tuple[float, float],
/,
*,
output_size: tuple[int, int] = (0, 0),
method: Interp = Interp.NEAREST,
) -> Array:
return cast(Array, wrapper.scale(image.arr, scale[0], scale[1], output_size[0], output_size[1], method))
@afarray_as_array
def skew(
image: Array,
skew: tuple[float, float],
/,
*,
output_size: tuple[int, int] = (0, 0),
method: Interp = Interp.NEAREST,
is_inverse: bool = True,
) -> Array:
return cast(Array, wrapper.skew(image.arr, skew[0], skew[1], output_size[0], output_size[1], method, is_inverse))
@afarray_as_array
def transform(
image: Array,
transform_matrix: Array,
/,
*,
output_size: tuple[int, int] = (0, 0),
method: Interp = Interp.NEAREST,
is_inverse: bool = True,
) -> Array:
return cast(
Array, wrapper.transform(image.arr, transform_matrix.arr, output_size[0], output_size[1], method, is_inverse)
)
@afarray_as_array
def transform_coordinates(image: Array, coordinates: tuple[float, float], /) -> Array:
return cast(Array, wrapper.transform_coordinates(image.arr, coordinates[0], coordinates[1]))
@afarray_as_array
def translate(
image: Array, translation: tuple[float, float], /, *, output_size: tuple[int, int], method: Interp = Interp.NEAREST
) -> Array:
return cast(
Array, wrapper.translate(image.arr, translation[0], translation[1], output_size[0], output_size[1], method)
)
# Labeling
@afarray_as_array
def confidence_cc(
image: Array,
seed: tuple[Array, Array],
radius: int,
multiplier: int,
iterations: int,
segmented_value: float,
/,
) -> Array:
return cast(
Array,
wrapper.confidence_cc(image.arr, seed[0].arr, seed[1].arr, radius, multiplier, iterations, segmented_value),
)
@afarray_as_array
def regions(
image: Array, /, *, connectivity: Connectivity = Connectivity.FOUR, output_dtype: Dtype = float32
) -> Array:
return cast(Array, wrapper.regions(image.arr, connectivity, output_dtype))
# Morphological operations
# TODO
# Split back to two separate functions
def dilate(image: Array, /, *, mask: Array | None = None) -> Array:
if image.ndim == 2:
mask_ = constant(1, (3, 3), float32) if not mask else mask
return Array.from_afarray(wrapper.dilate(image.arr, mask_.arr))
if image.ndim == 3:
mask_ = constant(1, (3, 3, 3), float32) if not mask else mask
return Array.from_afarray(wrapper.dilate3(image.arr, mask_.arr))
raise ValueError("Image should be either 2D or 3D.")
# TODO
# Split back to two separate functions
def erode(image: Array, /, *, mask: Array | None = None) -> Array:
if image.ndim == 2:
mask_ = constant(1, (3, 3), float32) if not mask else mask
return Array.from_afarray(wrapper.erode(image.arr, mask_.arr))
if image.ndim == 3:
mask_ = constant(1, (3, 3, 3), float32) if not mask else mask
return Array.from_afarray(wrapper.erode3(image.arr, mask_.arr))
raise ValueError("Image should be either 2D or 3D.")
# Wrapping
@afarray_as_array
def wrap(
image: Array,
output_size: tuple[int, int],
window: tuple[int, int],
stride: tuple[int, int],
/,
*,
padding: tuple[int, int],
is_column: bool = True,
) -> Array:
return cast(
Array,
wrapper.wrap(
image.arr,
output_size[0],
output_size[1],
window[0],
window[1],
stride[0],
stride[1],
padding[0],
padding[1],
is_column,
),
)
@afarray_as_array
def unwrap(
image: Array,
output_size: tuple[int, int],
window: tuple[int, int],
stride: tuple[int, int],
/,
*,
padding: tuple[int, int],
is_column: bool = True,
) -> Array:
return cast(
Array,
wrapper.unwrap(
image.arr,
output_size[0],
output_size[1],
window[0],
window[1],
stride[0],
stride[1],
padding[0],
padding[1],
is_column,
),
)