-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathAug_Operations.py
1620 lines (1339 loc) · 67.4 KB
/
Aug_Operations.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Operations.py
# Author: Marcus D. Bloice <https://github.com/mdbloice>
# Licensed under the terms of the MIT Licence.
"""
The Operations module contains classes for all operations used by Augmentor.
The classes contained in this module are not called or instantiated directly
by the user, instead the user interacts with the
:class:`~Augmentor.Pipeline.Pipeline` class and uses the utility functions contained
there.
In this module, each operation is a subclass of type :class:`Operation`.
The :class:`~Augmentor.Pipeline.Pipeline` objects expect :class:`Operation`
types, and therefore all operations are of type :class:`Operation`, and
provide their own implementation of the :func:`~Operation.perform_operation`
function.
Hence, the documentation for this module is intended for developers who
wish to extend Augmentor or wish to see how operations function internally.
For detailed information on extending Augmentor, see :ref:`extendingaugmentor`.
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from future.builtins import *
from PIL import Image, ImageOps
import math
from math import floor, ceil
import numpy as np
# from skimage import img_as_ubyte
# from skimage import transform
import os
import random
import warnings
import colorsys
# Python 2-3 compatibility - not currently needed.
# try:
# from StringIO import StringIO
# except ImportError:
# from io import StringIO
class Operation(object):
"""
The class :class:`Operation` represents the base class for all operations
that can be performed. Inherit from :class:`Operation`, overload
its methods, and instantiate super to create a new operation. See
the section on extending Augmentor with custom operations at
:ref:`extendingaugmentor`.
"""
def __init__(self, probability):
"""
All operations must at least have a :attr:`probability` which is
initialised when creating the operation's object.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:type probability: Float
"""
self.probability = probability
def __str__(self):
"""
Used to display a string representation of the operation, which is
used by the :func:`Pipeline.status` to display the current pipeline's
operations in a human readable way.
:return: A string representation of the operation. Can be overridden
if required, for example as is done in the :class:`Rotate` class.
"""
return self.__class__.__name__
def perform_operation(self, image):
"""
Perform the operation on the image. Each operation must at least
have this function, which accepts an image of type PIL.Image, performs
its operation, and returns an image of type PIL.Image.
:param image: The image to transform.
:type image: PIL.Image
:return: The transformed image of type PIL.Image.
"""
raise RuntimeError("Illegal call to base class.")
class HistogramEqualisation(Operation):
"""
The class :class:`HistogramEqualisation` is used to perform histogram
equalisation on images passed to its :func:`perform_operation` function.
"""
def __init__(self, probability):
"""
As there are no further user definable parameters, the class is
instantiated using only the :attr:`probability` argument.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:type probability: Float
"""
Operation.__init__(self, probability)
def perform_operation(self, image):
"""
Performs histogram equalisation on the image passed as an argument
and returns the equalised image. There are no user definable parameters
for this method.
:param image: The image on which to perform the histogram equalisation.
:type image: PIL.Image
:return: The transformed image of type PIL.Image
"""
# If an image is a colour image, the histogram will
# will be computed on the flattened image, which fires
# a warning.
# We may want to apply this instead to each colour channel,
# but I see no reason why right now. It would remove
# the need to catch these warnings, however.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return ImageOps.equalize(image)
class Greyscale(Operation):
"""
This class is used to convert images into greyscale. That is, it converts
images into having only shades of grey (pixel value intensities)
varying from 0 to 255 which represent black and white respectively.
.. seealso:: The :class:`BlackAndWhite` class.
"""
def __init__(self, probability):
"""
As there are no further user definable parameters, the class is
instantiated using only the :attr:`probability` argument.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:type probability: Float
"""
Operation.__init__(self, probability)
def perform_operation(self, image):
"""
Converts the passed image to greyscale and returns the transformed
image. There are no user definable parameters for this method.
:param image: The image to convert to greyscale.
:type image: PIL.Image
:return: The transformed image as type PIL.Image
"""
return ImageOps.grayscale(image)
class Invert(Operation):
"""
This class is used to negate images. That is to reverse the pixel values
for any image processed by it.
"""
def __init__(self, probability):
"""
As there are no further user definable parameters, the class is
instantiated using only the :attr:`probability` argument.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:type probability: Float
"""
Operation.__init__(self, probability)
def perform_operation(self, image):
"""
Negates the image passed as an argument. There are no user definable
parameters for this method.
:param image: The image to negate.
:type image: PIL.Image
:return: The transformed image as type PIL.Image
"""
return ImageOps.invert(image)
class BlackAndWhite(Operation):
"""
This class is used to convert images into black and white. In other words,
into using a 1-bit, monochrome binary colour palette. This is not to be
confused with greyscale, where an 8-bit greyscale pixel intensity range
is used.
.. seealso:: The :class:`Greyscale` class.
"""
def __init__(self, probability, threshold):
"""
As well as the required :attr:`probability` parameter, a
:attr:`threshold` can also be defined to define the cutoff point where
a pixel is converted to black or white. The :attr:`threshold` defaults
to 128 at the user-facing
:func:`~Augmentor.Pipeline.Pipeline.black_and_white` function.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param threshold: A value between 0 and 255 that defines the cut off
point where an individual pixel is converted into black or white.
:type probability: Float
:type threshold: Integer
"""
Operation.__init__(self, probability)
self.threshold = threshold
def perform_operation(self, image):
"""
Convert the image passed as an argument to black and white, 1-bit
monochrome. Uses the :attr:`threshold` passed to the constructor
to control the cut-off point where a pixel is converted to black or
white.
:param image: The image to convert into monochrome.
:type image: PIL.Image
:return: The converted image as type PIL.Image
"""
image = ImageOps.grayscale(image)
# An alternative would be to use PIL.ImageOps.posterize(image=image, bits=1)
return image.point(lambda x: 0 if x < self.threshold else 255, '1')
class Skew(Operation):
"""
This class is used to perform perspective skewing on images. It allows
for skewing from a total of 12 different perspectives.
"""
def __init__(self, probability, skew_type, magnitude):
"""
As well as the required :attr:`probability` parameter, the type of
skew that is performed is controlled using a :attr:`skew_type` and a
:attr:`magnitude` parameter. The :attr:`skew_type` controls the
direction of the skew, while :attr:`magnitude` controls the degree
to which the skew is performed.
To see examples of the various skews, see :ref:`perspectiveskewing`.
Images are skewed **in place** and an image of the same size is
returned by this function. That is to say, that after a skew
has been performed, the largest possible area of the same aspect ratio
of the original image is cropped from the skewed image, and this is
then resized to match the original image size. The
:ref:`perspectiveskewing` section describes this in detail.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param skew_type: Must be one of ``TILT``, ``TILT_TOP_BOTTOM``,
``TILT_LEFT_RIGHT``, or ``CORNER``.
- ``TILT`` will randomly skew either left, right, up, or down.
Left or right means it skews on the x-axis while up and down
means that it skews on the y-axis.
- ``TILT_TOP_BOTTOM`` will randomly skew up or down, or in other
words skew along the y-axis.
- ``TILT_LEFT_RIGHT`` will randomly skew left or right, or in other
words skew along the x-axis.
- ``CORNER`` will randomly skew one **corner** of the image either
along the x-axis or y-axis. This means in one of 8 different
directions, randomly.
To see examples of the various skews, see :ref:`perspectiveskewing`.
:param magnitude: The degree to which the image is skewed.
:type probability: Float
:type skew_type: String
:type magnitude: Integer
"""
Operation.__init__(self, probability)
self.skew_type = skew_type
self.magnitude = magnitude
def perform_operation(self, image):
"""
Perform the skew on the passed image and returns the transformed
image. Uses the :attr:`skew_type` and :attr:`magnitude` parameters to
control the type of skew to perform as well as the degree to which it
is performed.
:param image: The image to skew.
:type image: PIL.Image
:return: The skewed image as type PIL.Image
"""
w, h = image.size
x1 = 0
x2 = h
y1 = 0
y2 = w
original_plane = [(y1, x1), (y2, x1), (y2, x2), (y1, x2)]
max_skew_amount = max(w, h)
max_skew_amount = int(ceil(max_skew_amount * self.magnitude))
skew_amount = random.randint(1, max_skew_amount)
# Old implementation, remove.
# if not self.magnitude:
# skew_amount = random.randint(1, max_skew_amount)
# elif self.magnitude:
# max_skew_amount /= self.magnitude
# skew_amount = max_skew_amount
# TODO: Fix this abomination
if self.skew_type == "RANDOM":
skew = random.choice(["TILT", "TILT_LEFT_RIGHT", "TILT_TOP_BOTTOM", "CORNER"])
else:
skew = self.skew_type
# We have two choices now: we tilt in one of four directions
# or we skew a corner.
if skew == "TILT" or skew == "TILT_LEFT_RIGHT" or skew == "TILT_TOP_BOTTOM":
if skew == "TILT":
skew_direction = random.randint(0, 3)
elif skew == "TILT_LEFT_RIGHT":
skew_direction = random.randint(0, 1)
elif skew == "TILT_TOP_BOTTOM":
skew_direction = random.randint(2, 3)
if skew_direction == 0:
# Left Tilt
new_plane = [(y1, x1 - skew_amount), # Top Left
(y2, x1), # Top Right
(y2, x2), # Bottom Right
(y1, x2 + skew_amount)] # Bottom Left
elif skew_direction == 1:
# Right Tilt
new_plane = [(y1, x1), # Top Left
(y2, x1 - skew_amount), # Top Right
(y2, x2 + skew_amount), # Bottom Right
(y1, x2)] # Bottom Left
elif skew_direction == 2:
# Forward Tilt
new_plane = [(y1 - skew_amount, x1), # Top Left
(y2 + skew_amount, x1), # Top Right
(y2, x2), # Bottom Right
(y1, x2)] # Bottom Left
elif skew_direction == 3:
# Backward Tilt
new_plane = [(y1, x1), # Top Left
(y2, x1), # Top Right
(y2 + skew_amount, x2), # Bottom Right
(y1 - skew_amount, x2)] # Bottom Left
if skew == "CORNER":
skew_direction = random.randint(0, 7)
if skew_direction == 0:
# Skew possibility 0
new_plane = [(y1 - skew_amount, x1), (y2, x1), (y2, x2), (y1, x2)]
elif skew_direction == 1:
# Skew possibility 1
new_plane = [(y1, x1 - skew_amount), (y2, x1), (y2, x2), (y1, x2)]
elif skew_direction == 2:
# Skew possibility 2
new_plane = [(y1, x1), (y2 + skew_amount, x1), (y2, x2), (y1, x2)]
elif skew_direction == 3:
# Skew possibility 3
new_plane = [(y1, x1), (y2, x1 - skew_amount), (y2, x2), (y1, x2)]
elif skew_direction == 4:
# Skew possibility 4
new_plane = [(y1, x1), (y2, x1), (y2 + skew_amount, x2), (y1, x2)]
elif skew_direction == 5:
# Skew possibility 5
new_plane = [(y1, x1), (y2, x1), (y2, x2 + skew_amount), (y1, x2)]
elif skew_direction == 6:
# Skew possibility 6
new_plane = [(y1, x1), (y2, x1), (y2, x2), (y1 - skew_amount, x2)]
elif skew_direction == 7:
# Skew possibility 7
new_plane = [(y1, x1), (y2, x1), (y2, x2), (y1, x2 + skew_amount)]
if self.skew_type == "ALL":
# Not currently in use, as it makes little sense to skew by the same amount
# in every direction if we have set magnitude manually.
# It may make sense to keep this, if we ensure the skew_amount below is randomised
# and cannot be manually set by the user.
corners = dict()
corners["top_left"] = (y1 - random.randint(1, skew_amount), x1 - random.randint(1, skew_amount))
corners["top_right"] = (y2 + random.randint(1, skew_amount), x1 - random.randint(1, skew_amount))
corners["bottom_right"] = (y2 + random.randint(1, skew_amount), x2 + random.randint(1, skew_amount))
corners["bottom_left"] = (y1 - random.randint(1, skew_amount), x2 + random.randint(1, skew_amount))
new_plane = [corners["top_left"], corners["top_right"], corners["bottom_right"], corners["bottom_left"]]
# To calculate the coefficients required by PIL for the perspective skew,
# see the following Stack Overflow discussion: https://goo.gl/sSgJdj
matrix = []
new_h = max(new_plane[3][1]-new_plane[0][1], new_plane[2][1]-new_plane[1][1])
new_w = max(new_plane[1][0] - new_plane[0][0], new_plane[2][0] - new_plane[3][0])
x = min(new_plane[0][0], new_plane[1][0],new_plane[2][0],new_plane[3][0])
y = min(new_plane[0][1], new_plane[1][1],new_plane[2][1],new_plane[3][1])
plane = [(new_plane[0][0] - x, new_plane[0][1] - y),
(new_plane[1][0] - x, new_plane[1][1] - y),
(new_plane[2][0] - x, new_plane[2][1] - y),
(new_plane[3][0] - x, new_plane[3][1] - y)]
#print (plane)
for p1, p2 in zip(plane, original_plane):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0] * p1[0], -p2[0] * p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1] * p1[0], -p2[1] * p1[1]])
A = np.matrix(matrix, dtype=np.float)
B = np.array(original_plane).reshape(8)
perspective_skew_coefficients_matrix = np.dot(np.linalg.inv(A.T * A) * A.T, B)
perspective_skew_coefficients_matrix = np.array(perspective_skew_coefficients_matrix).reshape(8)
#print (perspective_skew_coefficients_matrix)
#perspective_skew_coefficients_matrix[2] += (x-5)/2
#perspective_skew_coefficients_matrix[5] += (y-5)/2
#print(perspective_skew_coefficients_matrix)
return image.transform((new_w, new_h),
Image.PERSPECTIVE,
perspective_skew_coefficients_matrix,
resample=Image.BICUBIC), np.array(plane)
class Rotate(Operation):
"""
This class is used to perform rotations on images in multiples of 90
degrees. Arbitrary rotations are handled by the :class:`RotateRange`
class.
"""
def __init__(self, probability, rotation):
"""
As well as the required :attr:`probability` parameter, the
:attr:`rotation` parameter controls the rotation to perform,
which must be one of ``90``, ``180``, ``270`` or ``-1`` (see below).
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param rotation: Controls the rotation to perform. Must be one of
``90``, ``180``, ``270`` or ``-1``.
- ``90`` rotate the image by 90 degrees.
- ``180`` rotate the image by 180 degrees.
- ``270`` rotate the image by 270 degrees.
- ``-1`` rotate the image randomly by either 90, 180, or 270 degrees.
.. seealso:: For arbitrary rotations, see the :class:`RotateRange` class.
"""
Operation.__init__(self, probability)
self.rotation = rotation
def __str__(self):
return "Rotate " + str(self.rotation)
def perform_operation(self, image):
"""
Rotate an image by either 90, 180, or 270 degrees, or randomly from
any of these.
:param image: The image to rotate.
:type image: PIL.Image
:return: The rotated image as type PIL.Image
"""
if self.rotation == -1:
random_factor = random.randint(1, 3)
return image.rotate(90 * random_factor, expand=True)
else:
return image.rotate(self.rotation, expand=True)
class RotateRange(Operation):
"""
This class is used to perform rotations on images by arbitrary numbers of
degrees.
Images are rotated **in place** and an image of the same size is
returned by this function. That is to say, that after a rotation
has been performed, the largest possible area of the same aspect ratio
of the original image is cropped from the skewed image, and this is
then resized to match the original image size.
The method by which this is performed is described as follows:
.. math::
E = \\frac{\\frac{\\sin{\\theta_{a}}}{\\sin{\\theta_{b}}}\\Big(X-\\frac{\\sin{\\theta_{a}}}{\\sin{\\theta_{b}}} Y\\Big)}{1-\\frac{(\\sin{\\theta_{a}})^2}{(\\sin{\\theta_{b}})^2}}
which describes how :math:`E` is derived, and then follows
:math:`B = Y - E` and :math:`A = \\frac{\\sin{\\theta_{a}}}{\\sin{\\theta_{b}}} B`.
The :ref:`rotating` section describes this in detail and has example
images to demonstrate this.
"""
def __init__(self, probability, max_left_rotation, max_right_rotation):
"""
As well as the required :attr:`probability` parameter, the
:attr:`max_left_rotation` parameter controls the maximum number of
degrees by which to rotate to the left, while the
:attr:`max_right_rotation` controls the maximum number of degrees to
rotate to the right.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param max_left_rotation: The maximum number of degrees to rotate
the image anti-clockwise.
:param max_right_rotation: The maximum number of degrees to rotate
the image clockwise.
:type probability: Float
:type max_left_rotation: Integer
:type max_right_rotation: Integer
"""
Operation.__init__(self, probability)
self.max_left_rotation = -abs(max_left_rotation) # Ensure always negative
self.max_right_rotation = abs(max_right_rotation) # Ensure always positive
def perform_operation(self, image):
"""
Perform the rotation on the passed :attr:`image` and return
the transformed image. Uses the :attr:`max_left_rotation` and
:attr:`max_right_rotation` passed into the constructor to control
the amount of degrees to rotate by. Whether the image is rotated
clockwise or anti-clockwise is chosen at random.
:param image: The image to rotate.
:type image: PIL.Image
:return: The rotated image as type PIL.Image
"""
# TODO: Small rotations of 1 or 2 degrees sometimes results in black pixels in the corners. Fix.
random_left = random.randint(self.max_left_rotation, 0)
random_right = random.randint(0, self.max_right_rotation)
left_or_right = random.randint(0, 1)
rotation = 0
if left_or_right == 0:
rotation = random_left
elif left_or_right == 1:
rotation = random_right
# Get size before we rotate
x = image.size[0]
y = image.size[1]
# Rotate, while expanding the canvas size
image = image.rotate(rotation, expand=True, resample=Image.BICUBIC)
# Get size after rotation, which includes the empty space
X = image.size[0]
Y = image.size[1]
# Get our two angles needed for the calculation of the largest area
angle_a = abs(rotation)
angle_b = 90 - angle_a
# Python deals in radians so get our radians
angle_a_rad = math.radians(angle_a)
angle_b_rad = math.radians(angle_b)
# Calculate the sins
angle_a_sin = math.sin(angle_a_rad)
angle_b_sin = math.sin(angle_b_rad)
# Find the maximum area of the rectangle that could be cropped
E = (math.sin(angle_a_rad)) / (math.sin(angle_b_rad)) * \
(Y - X * (math.sin(angle_a_rad) / math.sin(angle_b_rad)))
E = E / 1 - (math.sin(angle_a_rad) ** 2 / math.sin(angle_b_rad) ** 2)
B = X - E
A = (math.sin(angle_a_rad) / math.sin(angle_b_rad)) * B
# Crop this area from the rotated image
# image = image.crop((E, A, X - E, Y - A))
image = image.crop((int(round(E)), int(round(A)), int(round(X - E)), int(round(Y - A))))
# Return the image, re-sized to the size of the image passed originally
return image.resize((x, y), resample=Image.BICUBIC)
class Resize(Operation):
"""
This class is used to resize images by absolute values passed as parameters.
"""
def __init__(self, probability, width, height, resample_filter):
"""
Accepts the required probability parameter as well as parameters
to control the size of the transformed image.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param width: The width in pixels to resize the image to.
:param height: The height in pixels to resize the image to.
:param resample_filter: The resample filter to use. Must be one of
the standard PIL types, i.e. ``NEAREST``, ``BICUBIC``, ``ANTIALIAS``,
or ``BILINEAR``.
:type probability: Float
:type width: Integer
:type height: Integer
:type resample_filter: String
"""
Operation.__init__(self, probability)
self.width = width
self.height = height
self.resample_filter = resample_filter
def perform_operation(self, image):
"""
Resize the passed image and returns the resized image. Uses the
parameters passed to the constructor to resize the passed image.
:param image: The image to resize.
:type image: PIL.Image
:return: The resized image as type PIL.Image
"""
# TODO: Automatically change this to ANTIALIAS or BICUBIC depending on the size of the file
return image.resize((self.width, self.height), eval("Image.%s" % self.resample_filter))
class Flip(Operation):
"""
This class is used to mirror images through the x or y axes.
The class allows an image to be mirrored along either
its x axis or its y axis, or randomly.
"""
def __init__(self, probability, top_bottom_left_right):
"""
The direction of the flip, or whether it should be randomised, is
controlled using the :attr:`top_bottom_left_right` parameter.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param top_bottom_left_right: Controls the direction the image should
be mirrored. Must be one of ``LEFT_RIGHT``, ``TOP_BOTTOM``, or
``RANDOM``.
- ``LEFT_RIGHT`` defines that the image is mirrored along its x axis.
- ``TOP_BOTTOM`` defines that the image is mirrored along its y axis.
- ``RANDOM`` defines that the image is mirrored randomly along
either the x or y axis.
"""
Operation.__init__(self, probability)
self.top_bottom_left_right = top_bottom_left_right
def perform_operation(self, image):
"""
Mirror the image according to the `attr`:top_bottom_left_right`
argument passed to the constructor and return the mirrored image.
:param image: The image to mirror.
:type image: PIL.Image
:return: The mirrored image as type PIL.Image
"""
# TODO: Does it make sense to flip both ways?
if self.top_bottom_left_right == "LEFT_RIGHT":
return image.transpose(Image.FLIP_LEFT_RIGHT)
elif self.top_bottom_left_right == "TOP_BOTTOM":
return image.transpose(Image.FLIP_TOP_BOTTOM)
elif self.top_bottom_left_right == "RANDOM":
random_axis = random.randint(0, 1)
if random_axis == 0:
return image.transpose(Image.FLIP_LEFT_RIGHT)
elif random_axis == 1:
return image.transpose(Image.FLIP_TOP_BOTTOM)
class Crop(Operation):
"""
This class is used to crop images by absolute values passed as parameters.
"""
def __init__(self, probability, width, height, centre):
"""
As well as the always required :attr:`probability` parameter, the
constructor requires a :attr:`width` to control the width of
of the area to crop as well as a :attr:`height` parameter
to control the height of the area to crop. Also, whether the
area to crop should be taken from the centre of the image or from a
random location within the image is toggled using :attr:`centre`.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param width: The width in pixels of the area to crop from the image.
:param height: The height in pixels of the area to crop from the image.
:param centre: Whether to crop from the centre of the image or a random
location within the image, while maintaining the size of the crop
without cropping out of the original image's area.
:type probability: Float
:type width: Integer
:type height: Integer
:type centre: Boolean
"""
Operation.__init__(self, probability)
self.width = width
self.height = height
self.centre = centre
def perform_operation(self, image):
"""
Crop an area from an image, either from a random location or centred,
using the dimensions supplied during instantiation.
:param image: The image to crop the area from.
:type image: PIL.Image
:return: The cropped area as an image of type PIL.Image
"""
w, h = image.size
# Just return the original image if the crop is too large for the
# current image.
if self.width > w or self.height > h:
return image
if self.centre:
return image.crop(((w/2)-(self.width/2), (h/2)-(self.height/2), (w/2)+(self.width/2), (h/2)+(self.height/2)))
else:
left_shift = random.randint(0, int((w - self.width)))
down_shift = random.randint(0, int((h - self.height)))
return image.crop((left_shift, down_shift, self.width + left_shift, self.height + down_shift))
################################################################################################################
#if self.centre:
# new_width = self.width / 2.
# new_height = self.height / 2.
# half_the_width = w / 2
# half_the_height = h / 2
#
# return image.crop(
# (
# half_the_width - ceil(new_width),
# half_the_height - ceil(new_height),
# half_the_width + floor(new_width),
# half_the_height + floor(new_height)
# )
# )
#else:
# random_right_shift = random.randint(0, (w - self.width))
# random_down_shift = random.randint(0, (h - self.height))
#
# return image.crop(
# (
# random_right_shift,
# random_down_shift,
# self.width+random_right_shift,
# self.height+random_down_shift
# )
# )
class CropPercentage(Operation):
"""
This class is used to crop images by a percentage of their area.
"""
def __init__(self, probability, percentage_area, centre, randomise_percentage_area):
"""
As well as the always required :attr:`probability` parameter, the
constructor requires a :attr:`percentage_area` to control the area
of the image to crop in terms of its percentage of the original image,
and a :attr:`centre` parameter toggle whether a random area or the
centre of the images should be cropped.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param percentage_area: The percentage area of the original image
to crop. A value of 0.5 would crop an area that is 50% of the area
of the original image's size.
:param centre: Whether to crop from the centre of the image or
crop a random location within the image.
:type probability: Float
:type percentage_area: Float
:type centre: Boolean
"""
Operation.__init__(self, probability)
self.percentage_area = percentage_area
self.centre = centre
self.randomise_percentage_area = randomise_percentage_area
def perform_operation(self, image):
"""
Crop the passed :attr:`image` by percentage area, returning the crop as an
image.
:param image: The image to crop an area from.
:type image: PIL.Image
:return: The cropped area as an image of type PIL.Image
"""
if self.randomise_percentage_area:
r_percentage_area = round(random.uniform(0.1, self.percentage_area), 2)
else:
r_percentage_area = self.percentage_area
w, h = image.size
w_new = int(floor(w * r_percentage_area)) # TODO: Floor might return 0, so we need to check this.
h_new = int(floor(h * r_percentage_area))
if self.centre:
return image.crop(((w/2)-(w_new/2), (h/2)-(h_new/2), (w/2)+(w_new/2), (h/2)+(h_new/2)))
else:
left_shift = random.randint(0, int((w - w_new)))
down_shift = random.randint(0, int((h - h_new)))
return image.crop((left_shift, down_shift, w_new + left_shift, h_new + down_shift))
class CropRandom(Operation):
"""
.. warning:: This :class:`CropRandom` class is currently not used by any
of the user-facing functions in the :class:`~Augmentor.Pipeline.Pipeline`
class.
"""
def __init__(self, probability, percentage_area):
"""
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param percentage_area: The percentage area of the original image
to crop. A value of 0.5 would crop an area that is 50% of the area
of the original image's size.
"""
Operation.__init__(self, probability)
self.percentage_area = percentage_area
def perform_operation(self, image):
"""
Randomly crop the passed image, returning the crop as a new image.
:param image: The image to crop.
:type image: PIL.Image
:return: The cropped region as an image of type PIL.Image
"""
w, h = image.size
w_new = int(floor(w * self.percentage_area))
h_new = int(floor(h * self.percentage_area))
random_left_shift = random.randint(0, int((w - w_new))) # Note: randint() is from uniform distribution.
random_down_shift = random.randint(0, int((h - h_new)))
return image.crop((random_left_shift, random_down_shift, w_new + random_left_shift, h_new + random_down_shift))
class Shear(Operation):
"""
This class is used to shear images, that is to tilt them in a certain
direction. Tilting can occur along either the x- or y-axis and in both
directions (i.e. left or right along the x-axis, up or down along the
y-axis).
Images are sheared **in place** and an image of the same size as the input
image is returned by this class. That is to say, that after a shear
has been performed, the largest possible area of the same aspect ratio
of the original image is cropped from the sheared image, and this is
then resized to match the original image size. The
:ref:`shearing` section describes this in detail.
For sample code with image examples see :ref:`shearing`.
"""
def __init__(self, probability, max_shear_left, max_shear_right):
"""
The shearing is randomised in magnitude, from 0 to the
:attr:`max_shear_left` or 0 to :attr:`max_shear_right` where the
direction is randomised. The shear axis is also randomised
i.e. if it shears up/down along the y-axis or
left/right along the x-axis.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param max_shear_left: The maximum shear to the left.
:param max_shear_right: The maximum shear to the right.
:type probability: Float
:type max_shear_left: Integer
:type max_shear_right: Integer
"""
Operation.__init__(self, probability)
self.max_shear_left = max_shear_left
self.max_shear_right = max_shear_right
def perform_operation(self, image):
"""
Shears the passed image according to the parameters defined during
instantiation, and returns the sheared image.
:param image: The image to shear.
:type image: PIL.Image
:return: The sheared image of type PIL.Image
"""
######################################################################
# Old version which uses SciKit Image
######################################################################
# We will use scikit-image for this so first convert to a matrix
# using NumPy
# amount_to_shear = round(random.uniform(self.max_shear_left, self.max_shear_right), 2)
# image_array = np.array(image)
# And here we are using SciKit Image's `transform` class.
# shear_transformer = transform.AffineTransform(shear=amount_to_shear)
# image_sheared = transform.warp(image_array, shear_transformer)
#
# Because of warnings
# with warnings.catch_warnings():
# warnings.simplefilter("ignore")
# return Image.fromarray(img_as_ubyte(image_sheared))
######################################################################
width, height = image.size
# For testing.
# max_shear_left = 20
# max_shear_right = 20
angle_to_shear = int(random.uniform((abs(self.max_shear_left)*-1) - 1, self.max_shear_right + 1))
if angle_to_shear != -1: angle_to_shear += 1
# We use the angle phi in radians later
phi = math.tan(math.radians(angle_to_shear))
# Alternative method
# Calculate our offset when cropping
# We know one angle, phi (angle_to_shear)
# We known theta = 180-90-phi
# We know one side, opposite (height of image)
# Adjacent is therefore:
# tan(theta) = opposite / adjacent
# A = opposite / tan(theta)
# theta = math.radians(180-90-angle_to_shear)
# A = height / math.tan(theta)
# Transformation matrices can be found here:
# https://en.wikipedia.org/wiki/Transformation_matrix
# The PIL affine transform expects the first two rows of
# any of the affine transformation matrices, seen here:
# https://en.wikipedia.org/wiki/Transformation_matrix#/media/File:2D_affine_transformation_matrix.svg
directions = ["x", "y"]
direction = random.choice(directions)
w, h = image.size
x1 = 0
x2 = h
y1 = 0
y2 = w
original_plane = [(y1, x1), (y2, x1), (y2, x2), (y1, x2)]
if direction == "x":
# Here we need the unknown b, where a is
# the height of the image and phi is the
# angle we want to shear (our knowns):
# b = tan(phi) * a
shift_in_pixels = phi * height
if shift_in_pixels > 0:
shift_in_pixels = math.ceil(shift_in_pixels)
else:
shift_in_pixels = math.floor(shift_in_pixels)
# For negative tilts, we reverse phi and set offset to 0
# Also matrix offset differs from pixel shift for neg
# but not for pos so we will copy this value in case
# we need to change it
matrix_offset = shift_in_pixels
if angle_to_shear <= 0:
shift_in_pixels = abs(shift_in_pixels)
matrix_offset = 0
phi = abs(phi) * -1
# Note: PIL expects the inverse scale, so 1/scale_factor for example.
transform_matrix = (1, phi, -matrix_offset,
0, 1, 0)
plane = np.zeros((4, 2))
for i in range(0, 4):
plane[i, 0] = original_plane[i][0] * transform_matrix[0] - original_plane[i][1] * transform_matrix[1] - \
transform_matrix[2]
plane[i, 1] = original_plane[i][0] * transform_matrix[3] + original_plane[i][1] * transform_matrix[4] + \
transform_matrix[5]
image = image.transform((int(round(width + shift_in_pixels)), height),
Image.AFFINE,
transform_matrix,
Image.BICUBIC)
#image = image.crop((abs(shift_in_pixels), 0, width, height))
return image, np.array(plane)#.resize((width, height), resample=Image.BICUBIC)
elif direction == "y":
shift_in_pixels = phi * width