@@ -24,8 +24,7 @@ def run(self, img_arr):
24
24
return None
25
25
26
26
try :
27
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2GRAY )
28
- return img_arr
27
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2GRAY )
29
28
except :
30
29
logger .error ("Unable to convert RGB image to greyscale" )
31
30
return None
@@ -40,7 +39,7 @@ def run(self, img_arr):
40
39
return None
41
40
42
41
try :
43
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_GRAY2RGB )
42
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_GRAY2RGB )
44
43
except :
45
44
logger .error (F"Unable to convert greyscale image of shape { img_arr .shape } to RGB" )
46
45
return None
@@ -55,7 +54,7 @@ def run(self, img_arr):
55
54
return None
56
55
57
56
try :
58
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_GRAY2BGR )
57
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_GRAY2BGR )
59
58
except :
60
59
logger .error (F"Unable to convert greyscale image of shape { img_arr .shape } to RGB" )
61
60
return None
@@ -75,8 +74,7 @@ def run(self, img_arr):
75
74
return None
76
75
77
76
try :
78
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2GRAY )
79
- return img_arr
77
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2GRAY )
80
78
except :
81
79
logger .error ("Unable to convert BGR image to greyscale" )
82
80
return None
@@ -92,8 +90,7 @@ def run(self, img_arr):
92
90
return None
93
91
94
92
try :
95
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2GRAY )
96
- return img_arr
93
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2GRAY )
97
94
except :
98
95
logger .error ("Unable to convert HSV image to greyscale" )
99
96
return None
@@ -121,8 +118,7 @@ def run(self, img_arr):
121
118
return None
122
119
123
120
try :
124
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2RGB )
125
- return img_arr
121
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2RGB )
126
122
except :
127
123
logger .error ("Unable to convert BGR image to RGB" )
128
124
return None
@@ -138,8 +134,7 @@ def run(self, img_arr):
138
134
return None
139
135
140
136
try :
141
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2BGR )
142
- return img_arr
137
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2BGR )
143
138
except :
144
139
logger .error ("Unable to convert RGB image to BRG" )
145
140
return None
@@ -155,8 +150,7 @@ def run(self, img_arr):
155
150
return None
156
151
157
152
try :
158
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2RGB )
159
- return img_arr
153
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2RGB )
160
154
except :
161
155
logger .error ("Unable to convert HSV image to RGB" )
162
156
return None
@@ -172,8 +166,7 @@ def run(self, img_arr):
172
166
return None
173
167
174
168
try :
175
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2HSV )
176
- return img_arr
169
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_RGB2HSV )
177
170
except :
178
171
logger .error ("Unable to convert RGB image to HSV" )
179
172
return None
@@ -189,8 +182,7 @@ def run(self, img_arr):
189
182
return None
190
183
191
184
try :
192
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2BGR )
193
- return img_arr
185
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_HSV2BGR )
194
186
except :
195
187
logger .error ("Unable to convert HSV image to BGR" )
196
188
return None
@@ -206,8 +198,7 @@ def run(self, img_arr):
206
198
return None
207
199
208
200
try :
209
- img_arr = cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2HSV )
210
- return img_arr
201
+ return cv2 .cvtColor (img_arr , cv2 .COLOR_BGR2HSV )
211
202
except :
212
203
logger .error ("Unable to convert BGR image to HSV" )
213
204
return None
@@ -422,6 +413,62 @@ def shutdown(self):
422
413
self .masks = {} # free cached masks
423
414
424
415
416
+ class ImgTrapezoidalEdgeMask :
417
+ def __init__ (self , upper_left , upper_right , lower_left , lower_right , top , bottom , fill = [255 ,255 ,255 ]) -> None :
418
+ """
419
+ Apply a trapezoidal mask to an image, where bounds are
420
+ relative the the edge of the image, conserving the
421
+ image pixels within the trapezoid and masking everything
422
+ other pixels with the fill color
423
+ """
424
+ self .lower_left = lower_left
425
+ self .lower_right = lower_right
426
+ self .upper_left = upper_left
427
+ self .upper_right = upper_right
428
+ self .top = top
429
+ self .bottom = bottom
430
+ self .fill = fill
431
+ self .masks = {}
432
+
433
+ def run (self , image ):
434
+ """
435
+ Apply trapezoidal mask
436
+ # # # # # # # # # # # # #
437
+ # xxxxxxxxxxxxxxxxxxxxxxx
438
+ # xxxx ul ur xxxxxxxx min_y
439
+ # xxx xxxxxxx
440
+ # xx xxxxxx
441
+ # x xxxxx
442
+ # ll lr xx max_y
443
+ """
444
+ transformed = None
445
+ if image is not None :
446
+ mask = None
447
+ key = str (image .shape )
448
+ if self .masks .get (key ) is None :
449
+ height , width , depth = image_shape (image )
450
+ mask = np .zeros (image .shape , dtype = np .int32 )
451
+ points = [
452
+ [self .upper_left , self .top ],
453
+ [width - self .upper_right , self .top ],
454
+ [width - self .lower_right , height - self .bottom ],
455
+ [self .lower_left , height - self .bottom ]
456
+ ]
457
+ cv2 .fillConvexPoly (mask ,
458
+ np .array (points , dtype = np .int32 ),
459
+ self .fill )
460
+ mask = np .asarray (mask , dtype = 'bool' )
461
+ self .masks [key ] = mask
462
+
463
+ mask = self .masks [key ]
464
+ transformed = np .multiply (image , mask )
465
+
466
+ return transformed
467
+
468
+ def shutdown (self ):
469
+ self .masks = {} # free cached masks
470
+
471
+
425
472
class ImgCropMask :
426
473
def __init__ (self , left = 0 , top = 0 , right = 0 , bottom = 0 , fill = [255 , 255 , 255 ]) -> None :
427
474
"""
@@ -746,7 +793,7 @@ def shutdown(self):
746
793
#
747
794
# masking tranformations
748
795
#
749
- if "TRAPEZE" == transformation or "CROP" == transformation :
796
+ if "TRAPEZE" == transformation or "TRAPEZE_EDGE" == transformation or " CROP" == transformation :
750
797
#
751
798
# masking transformations
752
799
#
@@ -759,6 +806,15 @@ def shutdown(self):
759
806
args .top if args .top is not None else 0 ,
760
807
args .bottom if args .bottom is not None else height
761
808
)
809
+ elif "TRAPEZE_EDGE" == transformation :
810
+ transformer = ImgTrapezoidalEdgeMask (
811
+ args .left if args .left is not None else 0 ,
812
+ args .right if args .right is not None else width ,
813
+ args .left_bottom if args .left_bottom is not None else 0 ,
814
+ args .right_bottom if args .right_bottom is not None else width ,
815
+ args .top if args .top is not None else 0 ,
816
+ args .bottom if args .bottom is not None else height
817
+ )
762
818
else :
763
819
transformer = ImgCropMask (
764
820
args .left if args .left is not None else 0 ,
@@ -782,7 +838,7 @@ def shutdown(self):
782
838
transformer = ImgHSV2BGR ()
783
839
elif "RGB2GREY" == transformation :
784
840
transformer = ImgRGB2GRAY ()
785
- elif "RBGR2GREY " == transformation :
841
+ elif "BGR2GREY " == transformation :
786
842
transformer = ImgBGR2GRAY ()
787
843
elif "HSV2GREY" == transformation :
788
844
transformer = ImgHSV2GRAY ()
0 commit comments