How do I find rectangular areas in a matrix

155 Views Asked by At

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

0

There are 0 best solutions below