0

I have a matrix with different labels. How do I find all rectangles with identical values?

I tried:

import numpy as np
import scipy.ndimage as nd

a= np.zeros((120, 120), dtype=np.uint16)
a[:100, :100] = 5
a[10:100, 10:100] = 6
a[12:100, 12:100] = 7

for v in np.unique(a):
  if v:
    l=nd.label((a==v).astype(int))
    f = nd.find_objects(l[0])
    print(v, f)

This reports

5 [(slice(0, 100, None), slice(0, 100, None))]
6 [(slice(10, 100, None), slice(10, 100, None))]
7 [(slice(12, 100, None), slice(12, 100, None))]

But a[50, 50] = 7 so there is clearly no rectangle [0:100, 0:100] with the value 5

Ruediger Jungbeck
  • 2,836
  • 5
  • 36
  • 59
  • from the documentation of `find_objects`: "Slices correspond to the *minimal parallelepiped that contains the object*.", so everything is correct. – Stef Sep 28 '21 at 11:27
  • I didn't say find_objects does it wrong. It's just not (exactly) what I need. – Ruediger Jungbeck Sep 28 '21 at 11:28

0 Answers0