Skip to content

Commit f0fcf89

Browse files
author
Robert Sachunsky
committed
ocrolib.morph: opt out of np.unique counts
1 parent 6f8a612 commit f0fcf89

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ocrd_cis/ocropy/ocrolib/morph.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,26 +226,30 @@ def remove_marked(image,markers):
226226
return image*(marked==0)
227227

228228
@checks(SEGMENTATION,SEGMENTATION)
229-
def correspondences(labels1,labels2):
229+
def correspondences(labels1,labels2,return_counts=True):
230230
"""Given two labeled images, compute an array giving the correspondences
231231
between labels in the two images (as tuples of label in `labels1`,
232232
label in `labels2`, and pixel count)."""
233233
q = 100000
234234
assert amin(labels1)>=0 and amin(labels2)>=0
235235
assert amax(labels2)<q
236236
combo = labels1*q+labels2
237-
result, counts = unique(combo, return_counts=True)
238-
result = array([result//q,result%q,counts])
237+
result = unique(combo, return_counts=return_counts)
238+
if return_counts:
239+
result, counts = result
240+
result = array([result//q,result%q,counts])
241+
else:
242+
result = array([result//q,result%q])
239243
return result
240244

241245
@checks(ABINARY2,SEGMENTATION)
242246
def propagate_labels_simple(regions,labels):
243247
"""Given an image and a set of labels, apply the labels
244248
to all the connected components in the image that overlap a label."""
245249
rlabels,_ = label(regions)
246-
cors = correspondences(rlabels,labels)
250+
cors = correspondences(rlabels,labels,False)
247251
outputs = zeros(amax(rlabels)+1,'i')
248-
for o,i,_ in cors.T: outputs[o] = i
252+
for o,i in cors.T: outputs[o] = i
249253
outputs[0] = 0
250254
return outputs[rlabels]
251255

@@ -275,10 +279,10 @@ def propagate_labels(image,labels,conflict=0):
275279
to all the connected components in the image that overlap a label.
276280
Assign the value `conflict` to any components that have a conflict."""
277281
rlabels,_ = label(image)
278-
cors = correspondences(rlabels,labels)
282+
cors = correspondences(rlabels,labels,False)
279283
outputs = zeros(amax(rlabels)+1,'i')
280284
oops = -(1<<30)
281-
for o,i,_ in cors.T:
285+
for o,i in cors.T:
282286
if outputs[o]!=0: outputs[o] = oops
283287
else: outputs[o] = i
284288
outputs[outputs==oops] = conflict

0 commit comments

Comments
 (0)