-
Notifications
You must be signed in to change notification settings - Fork 4
/
scanner.py
566 lines (477 loc) · 20.4 KB
/
scanner.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
from enum import Enum
import numpy as np
class MatScanner:
"""Returns a 1-Dimensional pixel sequence giving the starting point and
direction.
"""
class Direction(Enum):
raster = 0
right_up = 1
left_up = 2
left_down = 3
down_right = 4
down_left = 5
up_right = 6
up_left = 7
z_raster = 8
z_right_up = 9
z_left_up = 10
z_left_down = 11
z_down_right = 12
z_down_left = 13
z_up_right = 14
z_up_left = 15
@staticmethod
def _zig_zag(mat, axis=0, inverse=False):
"""Computes the no row-jump or column-jump order for the 8 last
directions.
If inverse mode is True, even indexes will be flipped only if the
column or row shape is odd. Used for column or row inverse flip
order. Up to Down or Right to Left.
Example:
>>> mat = np.arange(9).reshape(3, 3)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> _zig_zag(mat)
array([[0, 1, 2],
[5, 4, 3],
[6, 7, 8]])
>>> _zig_zag(mat, axis=1)
array([[0, 7, 2],
[5, 4, 3],
[6, 1, 8]])
>>> mat = np.arange(12).reshape(5,2)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> mat = _zig_zag(mat, axis=1, inverse=True)
array([[0, 1, 7, 3, 9],
[5, 6, 2, 8, 4]])
>>> mat = _zig_zag(mat, axis=1)
array([[0, 6, 7, 8, 9],
[5, 1, 2, 3, 4]])
Args:
mat: matrix
axis: matrix flip axis
inverse: change how the rows or columns will be flipped
"""
if axis == 0:
b_size = mat.shape[1]
order = 'C'
elif axis == 1:
b_size = mat.shape[0]
order = 'F'
else:
return None
it = np.nditer(mat, flags=['external_loop', 'buffered'], order=order,
op_flags=['readwrite'], buffersize=b_size)
if inverse and mat.shape[axis] % 2 == 1:
parity = 0
else:
parity = 1
# Flip the rows or columns
while it.iternext():
if (it.iterindex / b_size) % 2 == parity:
it.value[...] = np.flip(it.value, 0)
@staticmethod
def _raster_scan(mat, y, x, shape):
"""Scans in raster order. From Left to Right. From Top to Down.
Args:
mat: matrix
y: starting row
x: starting column
shape: matrix shape
Return:
np.array: flattened array from the starting point
"""
idx = y * shape[1] + x
return np.roll(mat, -idx).flatten()
@staticmethod
def _un_raster_scan(mat, y, x, shape):
"""Undo raster order"""
idx = y * shape[1] + x
return np.roll(mat, idx).reshape(shape)
@staticmethod
def _left_up(mat, y, x, shape):
"""Scans from Right to Left. From Down to Up."""
idx = y * shape[1] + x + 1
roll = np.roll(mat, -idx)
return np.flip(roll.flatten(), 0)
@staticmethod
def _un_left_up(mat, y, x, shape):
"""Undo left up order"""
idx = y * shape[1] + x + 1
reshape = np.flip(mat, 0).reshape(shape)
return np.roll(reshape, idx)
@staticmethod
def _left_down(mat, y, x, shape):
"""Scans from Right to Left. From Up to Down"""
x = shape[1] - x -1
idx = y * shape[1] + x
flip = np.flip(mat, 1)
return np.roll(flip, -idx).flatten()
@staticmethod
def _un_left_down(mat, y, x, shape):
"""Undo left down order"""
x = shape[1] - x -1
idx = y * shape[1] + x
reshape = np.roll(mat.reshape(shape), idx)
return np.flip(reshape, 1)
@staticmethod
def _right_up(mat, y, x, shape):
"""Scans from Left to Right. From Down to Up."""
y = shape[0] - y - 1
idx = y * shape[1] + x
flip = np.flip(mat, 0)
return np.roll(flip.flatten(), -idx)
@staticmethod
def _un_right_up(mat, y, x, shape):
"""Undo right up order"""
y = shape[0] - y - 1
idx = y * shape[1] + x
reshape = np.roll(mat, idx).reshape(shape)
return np.flip(reshape, 0)
@staticmethod
def _down_right(mat, y, x, shape):
"""Scans from Top-Down. From Left to Right"""
idx = x * shape[0] + y
return np.roll(mat.flatten('F'), -idx)
@staticmethod
def _un_down_right(mat, y, x, shape):
"""Undo down right order"""
idx = x * shape[0] + y
return np.roll(mat, idx).reshape(shape, order='F')
@staticmethod
def _down_left(mat, y, x, shape):
"""Scans from Top-Down. From Right to Left"""
x = shape[1] - x - 1
idx = x * shape[0] + y
flip = np.flip(mat, 1)
return np.roll(flip.flatten('F'), -idx)
@staticmethod
def _un_down_left(mat, y, x, shape):
"""Undo down left order"""
x = shape[1] - x - 1
idx = x * shape[0] + y
reshape = np.roll(mat, idx).reshape(shape, order='F')
return np.flip(reshape, 1)
@staticmethod
def _up_right(mat, y, x, shape):
"""Scans from Down Top. From left to right"""
y = shape[0] - y - 1
idx = x * shape[0] + y
flip = np.flip(mat, 0)
return np.roll(flip.flatten('F'), -idx)
@staticmethod
def _un_up_right(mat, y, x, shape):
"""Undo up right order"""
y = shape[0] - y - 1
idx = x * shape[0] + y
reshape = np.roll(mat, idx).reshape(shape, order='F')
return np.flip(reshape, 0)
@staticmethod
def _up_left(mat, y, x, shape):
"""Scans from Down Top. From right to left"""
y = shape[0] -y - 1
x = shape[1] - x - 1
idx = x * shape[0] + y
flip = np.flip(np.flip(mat, 0), 1)
return np.roll(flip.flatten('F'), -idx)
@staticmethod
def _un_up_left(mat, y, x, shape):
"""Undo up left order"""
y = shape[0] -y - 1
x = shape[1] - x - 1
idx = x * shape[0] + y
reshape = np.roll(mat, idx).reshape(shape, order='F')
return np.flip(np.flip(reshape, 1), 0)
@classmethod
def _z_scan(cls, img, y, x, direction):
"""This method returns the flattened pixel sequence given the starting
point an direction using the no row-jump or column-jump order.
Args:
img: raw image (np.array)
y: starting row
x: starting column
direction: scan direction
Return:
numpy.array
"""
if (direction == cls.Direction.z_raster
or direction == cls.Direction.z_left_down
or direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up):
idx = -y
ax = 0
elif (direction == cls.Direction.z_down_right
or direction == cls.Direction.z_up_right
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
idx = -x
ax = 1
else:
return img
# Check inverse mode
if (direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
inv = True
else:
inv = False
# Apply transformations
img = np.roll(img, idx, axis=ax)
cls._zig_zag(img, axis=ax, inverse=inv)
if direction == cls.Direction.z_raster:
return cls.scan(img, 0, x, cls.Direction.raster)
elif direction == cls.Direction.z_left_down:
return cls.scan(img, 0, x, cls.Direction.left_down)
elif direction == cls.Direction.z_right_up:
return cls.scan(img, 0, x, cls.Direction.right_up)
elif direction == cls.Direction.z_left_up:
return cls.scan(img, 0, x, cls.Direction.left_up)
elif direction == cls.Direction.z_down_right:
return cls.scan(img, y, 0, cls.Direction.down_right)
elif direction == cls.Direction.z_up_right:
return cls.scan(img, y, 0, cls.Direction.up_right)
elif direction == cls.Direction.z_down_left:
return cls.scan(img, y, 0, cls.Direction.down_left)
elif direction == cls.Direction.z_up_left:
return cls.scan(img, y, 0, cls.Direction.up_left)
@classmethod
def _un_z_scan(cls, img, shape, y, x, direction):
"""Returns the reshaped array given the direction using the no row-jump
or column-jump directions.
Args:
img: flattened image (np.array)
shape: Shape of array
y: starting row
x: starting column
direction: scan direction
Return:
numpy.array
"""
if direction == cls.Direction.z_raster:
img = cls.reshape(img, shape, 0, x, cls.Direction.raster)
elif direction == cls.Direction.z_left_down:
img = cls.reshape(img, shape, 0, x, cls.Direction.left_down)
elif direction == cls.Direction.z_right_up:
img = cls.reshape(img, shape, 0, x, cls.Direction.right_up)
elif direction == cls.Direction.z_left_up:
img = cls.reshape(img, shape, 0, x, cls.Direction.left_up)
elif direction == cls.Direction.z_down_right:
img = cls.reshape(img, shape, y, 0, cls.Direction.down_right)
elif direction == cls.Direction.z_up_right:
img = cls.reshape(img, shape, y, 0, cls.Direction.up_right)
elif direction == cls.Direction.z_down_left:
img = cls.reshape(img, shape, y, 0, cls.Direction.down_left)
elif direction == cls.Direction.z_up_left:
img = cls.reshape(img, shape, y, 0, cls.Direction.up_left)
if (direction == cls.Direction.z_raster
or direction == cls.Direction.z_left_down
or direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up):
idx = y
ax = 0
elif (direction == cls.Direction.z_down_right
or direction == cls.Direction.z_up_right
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
idx = x
ax = 1
# Check inverse mode
if (direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
inv = True
else:
inv = False
# Apply transformations
cls._zig_zag(img, axis=ax, inverse=inv)
img = np.roll(img, idx, axis=ax)
return img
@classmethod
def scan(cls, img, y, x, direction):
"""This method returns the flattened pixel sequence given the starting
point and direction.
Args:
img: raw image (np.array)
y: starting row
x: starting column
direction: scan direction
Return:
numpy.array
"""
direction = cls.Direction(direction)
if direction == cls.Direction.raster:
return cls._raster_scan(img, y, x, img.shape)
elif direction == cls.Direction.right_up:
return cls._right_up(img, y, x, img.shape)
elif direction == cls.Direction.left_up:
return cls._left_up(img, y, x, img.shape)
elif direction == cls.Direction.left_down:
return cls._left_down(img, y, x, img.shape)
elif direction == cls.Direction.down_right:
return cls._down_right(img, y, x, img.shape)
elif direction == cls.Direction.down_left:
return cls._down_left(img, y, x, img.shape)
elif direction == cls.Direction.up_right:
return cls._up_right(img, y, x, img.shape)
elif direction == cls.Direction.up_left:
return cls._up_left(img, y, x, img.shape)
elif (direction == cls.Direction.z_raster
or direction == cls.Direction.z_left_down
or direction == cls.Direction.z_down_right
or direction == cls.Direction.z_up_right
or direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
return cls._z_scan(img, y, x, direction)
@classmethod
def scan_genetic(cls, img, chromosome):
"""This method return the flattened pixel sequence given using
the provided chromosome
Args:
img: raw image (np.array)
chromosome: chromosome encoding x, y, direction genes
Return:
numpy.array
"""
return cls.scan(img, chromosome[2], chromosome[1], chromosome[0])
@classmethod
def reshape(cls, img, shape, y, x, direction):
"""Returns the reshaped array given the direction.
Args:
img: flattened image (np.array)
shape: Shape of array
y: starting row
x: starting column
direction: scan direction
Return:
numpy.array
"""
direction = cls.Direction(direction)
if direction == cls.Direction.raster:
return cls._un_raster_scan(img, y, x, shape)
elif direction == cls.Direction.right_up:
return cls._un_right_up(img, y, x, shape)
elif direction == cls.Direction.left_up:
return cls._un_left_up(img, y, x, shape)
elif direction == cls.Direction.left_down:
return cls._un_left_down(img, y, x, shape)
elif direction == cls.Direction.down_right:
return cls._un_down_right(img, y, x, shape)
elif direction == cls.Direction.down_left:
return cls._un_down_left(img, y, x, shape)
elif direction == cls.Direction.up_right:
return cls._un_up_right(img, y, x, shape)
elif direction == cls.Direction.up_left:
return cls._un_up_left(img, y, x, shape)
elif (direction == cls.Direction.z_raster
or direction == cls.Direction.z_left_down
or direction == cls.Direction.z_down_right
or direction == cls.Direction.z_up_right
or direction == cls.Direction.z_right_up
or direction == cls.Direction.z_left_up
or direction == cls.Direction.z_down_left
or direction == cls.Direction.z_up_left):
return cls._un_z_scan(img, shape, y, x, direction)
@classmethod
def reshape_genetic(cls, img, shape, chromosome):
"""Returns the reshaped array using the provided chromosome
Args:
img: flattened image (np.array)
shape: shape of array
chromosome: chromosome encoding x, y, direction genes
Return:
numpy.array
"""
return cls.reshape(img, shape, chromosome[2], chromosome[1], chromosome[0])
if __name__ == '__main__':
mat = np.arange(10).reshape(2,5)
print('- Original: \n{}'.format(mat))
mat_scanned = MatScanner.scan(mat, 0, 3, MatScanner.Direction.raster)
print('\n- Raster order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, (2, 5), 0, 3,
MatScanner.Direction.raster)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 3, MatScanner.Direction.right_up)
print('\n- Right Up order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, (2, 5), 1, 3,
MatScanner.Direction.right_up)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 2, MatScanner.Direction.left_up)
print('\n- Left Up order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, (2, 5), 1, 2,
MatScanner.Direction.left_up)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 0, 3, MatScanner.Direction.left_down)
print('\n- Left Down order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, (2, 5), 0, 3,
MatScanner.Direction.left_down)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 3, MatScanner.Direction.down_right)
print('\n- Down right order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 3,
MatScanner.Direction.down_right)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 0, 3, MatScanner.Direction.down_left)
print('\n- Down left order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 0, 3,
MatScanner.Direction.down_left)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 0, 2, MatScanner.Direction.up_right)
print('\n- Up Right order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 0, 2,
MatScanner.Direction.up_right)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 2, MatScanner.Direction.up_left)
print('\n- Up Left order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 2,
MatScanner.Direction.up_left)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 3, MatScanner.Direction.z_raster)
print('\n- Zig zag Raster order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 3,
MatScanner.Direction.z_raster)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 0, 3, MatScanner.Direction.z_left_down)
print('\n- Zig zag Left Down order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 0, 3,
MatScanner.Direction.z_left_down)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_down_right)
print('\n- Zig zag Down Right order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_down_right)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_up_right)
print('\n- Zig zag Up Right order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_up_right)
print('- Original: \n{}'.format(mat_reshaped))
# Created new (3,3) matrix
mat = np.arange(9).reshape(3,3)
print('\n- New Matrix: \n{}'.format(mat))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_right_up)
print('\n- Zig zag Right up order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_right_up)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_left_up)
print('\n- Zig zag Left up order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_left_up)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_down_left)
print('\n- Zig zag Down left order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_down_left)
print('- Original: \n{}'.format(mat_reshaped))
mat_scanned = MatScanner.scan(mat, 1, 1, MatScanner.Direction.z_up_left)
print('\n- Zig zag Up left order: {}'.format(mat_scanned))
mat_reshaped = MatScanner.reshape(mat_scanned, mat.shape, 1, 1,
MatScanner.Direction.z_up_left)
print('- Original: \n{}'.format(mat_reshaped))