Skip to content

Commit f63e1cb

Browse files
author
Nic Bricknell
committed
mxSampleDetect: Copy original array when annotated, tweak colour depth behaviour
1 parent fef98d7 commit f63e1cb

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

adPythonApp/scripts/adPythonMxSampleDetect.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,8 @@ def draw_circle(arr, (y, x), color, radius=5):
178178

179179

180180
def draw_edges(arr, edges, color):
181-
try:
182-
for x, y in chain(*map(enumerate, edges)):
183-
if y != none_value: arr[y, x] = color
184-
except ValueError:
185-
# Probably trying to write onto the read-only array from upstream.
186-
# TODO: Could do a deepcopy to avoid this exception?
187-
pass
181+
for x, y in chain(*map(enumerate, edges)):
182+
if y != none_value: arr[y, x] = color
188183

189184

190185
class MxSampleDetect(AdPythonPlugin):
@@ -248,20 +243,24 @@ def processArray(self, arr, attr={}):
248243
# image processing chain.
249244
out = (arr, gray_arr, pp_arr, edge_arr, closed_arr)[self['out_arr']]
250245

246+
# Find out which annotation(s) the user wants (if any).
247+
do_circle, do_edges = self['draw_circle'], self['draw_edges']
248+
251249
# Expand the output colour depth to enable colour annotations if
252-
# requested to do so. Choose the colour for annotations.
253-
if self['force_color']:
250+
# required and requested to do so. Choose the colour for annotations.
251+
if self['force_color'] and (do_circle or do_edges):
254252
color = [255, 0, 0]
255253
if out.ndim == 2:
256-
out = cv2.cvtColor(out, cv2.COLOR_GRAY2BGR)
254+
out = cv2.cvtColor(out, cv2.COLOR_GRAY2RGB)
257255
else:
258256
color = [255, 0, 0] if out.ndim == 3 else 255
259257

260-
# Optionally annotate the output array before returning.
261-
if self['draw_circle']:
262-
draw_circle(out, tip, color)
263-
if self['draw_edges']:
264-
draw_edges(out, edges, color)
258+
# If we're annotating the original, we need to make a writable copy.
259+
if out is arr and (do_circle or do_edges): out = arr.copy()
260+
261+
# Do the annotations.
262+
if do_circle: draw_circle(out, tip, color)
263+
if do_edges: draw_edges(out, edges, color)
265264

266265
return out
267266

0 commit comments

Comments
 (0)