Skip to content

Commit 3dee532

Browse files
authored
fix cv color conversions (autorope#1133)
- several of the color conversions were not correctly returning the converted image; return None instead. - this commit fixes those and makes all color conversions similar in how they return that converted image - This also adds a new transform; a version of the trapezoid mask that takes values from the edges of the image like the crop transform does (rather than absoluted image coordinates as the pre-existing trapezoid transform does).
1 parent c0d4eb3 commit 3dee532

File tree

1 file changed

+78
-22
lines changed

1 file changed

+78
-22
lines changed

donkeycar/parts/cv.py

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def run(self, img_arr):
2424
return None
2525

2626
try:
27-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)
28-
return img_arr
27+
return cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)
2928
except:
3029
logger.error("Unable to convert RGB image to greyscale")
3130
return None
@@ -40,7 +39,7 @@ def run(self, img_arr):
4039
return None
4140

4241
try:
43-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_GRAY2RGB)
42+
return cv2.cvtColor(img_arr, cv2.COLOR_GRAY2RGB)
4443
except:
4544
logger.error(F"Unable to convert greyscale image of shape {img_arr.shape} to RGB")
4645
return None
@@ -55,7 +54,7 @@ def run(self, img_arr):
5554
return None
5655

5756
try:
58-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_GRAY2BGR)
57+
return cv2.cvtColor(img_arr, cv2.COLOR_GRAY2BGR)
5958
except:
6059
logger.error(F"Unable to convert greyscale image of shape {img_arr.shape} to RGB")
6160
return None
@@ -75,8 +74,7 @@ def run(self, img_arr):
7574
return None
7675

7776
try:
78-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY)
79-
return img_arr
77+
return cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY)
8078
except:
8179
logger.error("Unable to convert BGR image to greyscale")
8280
return None
@@ -92,8 +90,7 @@ def run(self, img_arr):
9290
return None
9391

9492
try:
95-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_HSV2GRAY)
96-
return img_arr
93+
return cv2.cvtColor(img_arr, cv2.COLOR_HSV2GRAY)
9794
except:
9895
logger.error("Unable to convert HSV image to greyscale")
9996
return None
@@ -121,8 +118,7 @@ def run(self, img_arr):
121118
return None
122119

123120
try:
124-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
125-
return img_arr
121+
return cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
126122
except:
127123
logger.error("Unable to convert BGR image to RGB")
128124
return None
@@ -138,8 +134,7 @@ def run(self, img_arr):
138134
return None
139135

140136
try:
141-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_RGB2BGR)
142-
return img_arr
137+
return cv2.cvtColor(img_arr, cv2.COLOR_RGB2BGR)
143138
except:
144139
logger.error("Unable to convert RGB image to BRG")
145140
return None
@@ -155,8 +150,7 @@ def run(self, img_arr):
155150
return None
156151

157152
try:
158-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_HSV2RGB)
159-
return img_arr
153+
return cv2.cvtColor(img_arr, cv2.COLOR_HSV2RGB)
160154
except:
161155
logger.error("Unable to convert HSV image to RGB")
162156
return None
@@ -172,8 +166,7 @@ def run(self, img_arr):
172166
return None
173167

174168
try:
175-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_RGB2HSV)
176-
return img_arr
169+
return cv2.cvtColor(img_arr, cv2.COLOR_RGB2HSV)
177170
except:
178171
logger.error("Unable to convert RGB image to HSV")
179172
return None
@@ -189,8 +182,7 @@ def run(self, img_arr):
189182
return None
190183

191184
try:
192-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_HSV2BGR)
193-
return img_arr
185+
return cv2.cvtColor(img_arr, cv2.COLOR_HSV2BGR)
194186
except:
195187
logger.error("Unable to convert HSV image to BGR")
196188
return None
@@ -206,8 +198,7 @@ def run(self, img_arr):
206198
return None
207199

208200
try:
209-
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2HSV)
210-
return img_arr
201+
return cv2.cvtColor(img_arr, cv2.COLOR_BGR2HSV)
211202
except:
212203
logger.error("Unable to convert BGR image to HSV")
213204
return None
@@ -422,6 +413,62 @@ def shutdown(self):
422413
self.masks = {} # free cached masks
423414

424415

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+
425472
class ImgCropMask:
426473
def __init__(self, left=0, top=0, right=0, bottom=0, fill=[255, 255, 255]) -> None:
427474
"""
@@ -746,7 +793,7 @@ def shutdown(self):
746793
#
747794
# masking tranformations
748795
#
749-
if "TRAPEZE" == transformation or "CROP" == transformation:
796+
if "TRAPEZE" == transformation or "TRAPEZE_EDGE" == transformation or "CROP" == transformation:
750797
#
751798
# masking transformations
752799
#
@@ -759,6 +806,15 @@ def shutdown(self):
759806
args.top if args.top is not None else 0,
760807
args.bottom if args.bottom is not None else height
761808
)
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+
)
762818
else:
763819
transformer = ImgCropMask(
764820
args.left if args.left is not None else 0,
@@ -782,7 +838,7 @@ def shutdown(self):
782838
transformer = ImgHSV2BGR()
783839
elif "RGB2GREY" == transformation:
784840
transformer = ImgRGB2GRAY()
785-
elif "RBGR2GREY" == transformation:
841+
elif "BGR2GREY" == transformation:
786842
transformer = ImgBGR2GRAY()
787843
elif "HSV2GREY" == transformation:
788844
transformer = ImgHSV2GRAY()

0 commit comments

Comments
 (0)