1

I've got a large 3d numpy array which consists of ones and zeros. I would like to use the scipy.ndimage.label tool to label the features in each sub-array (2d).

A subset of the 3d-array looks like:

import numpy as np
from scipy.ndimage import label

subset = np.array([[[1, 0, 0],
                    [1, 0, 1],
                    [0, 0, 0]],

                   [[0, 0, 0],
                    [1, 0, 1],
                    [0, 0, 1]],

                   [[0, 0, 0],
                    [1, 0, 0],
                    [0, 1, 1]],

                   [[0, 0, 0],
                    [1, 0, 0],
                    [1, 1, 1]]], dtype=uint8)

When I use the label tool on a small part of this subset is works correct:

>>>label(subset[0:3])    
(array([[[1, 0, 0],
         [1, 0, 2],
         [0, 0, 0]],

        [[0, 0, 0],
         [1, 0, 2],
         [0, 0, 2]],

        [[0, 0, 0],
         [1, 0, 0],
         [0, 2, 2]]]), 2)

However, when I use the entire subset the label tool is not working properly:

>>>label(subset)
(array([[[1, 0, 0],
         [1, 0, 1],
         [0, 0, 0]],

        [[0, 0, 0],
         [1, 0, 1],
         [0, 0, 1]],

        [[0, 0, 0],
         [1, 0, 0],
         [0, 1, 1]],

        [[0, 0, 0],
         [1, 0, 0],
         [1, 1, 1]]]), 1)

Any ideas how this problem can be tackled?

ps. The complete array which I am trying to label consists of 350219 2d arrays.

Aelius
  • 1,029
  • 11
  • 22
Wilmar van Ommeren
  • 7,469
  • 6
  • 34
  • 65
  • 2
    I think you need to specify the `structure` value, giving a 2d array of 1s, rather than a 3d array as would be assumed by default – dan-man Apr 28 '16 at 15:03
  • When I define a 2d structure, like: [[0, 1, 0], [ 1, 1, 1], [0, 1, 0]] I get the error: structure and input must have equal rank – Wilmar van Ommeren Apr 28 '16 at 15:10
  • 2
    you need the 2d structure to be 3d, i.e. one of the dims should a length of 1. – dan-man Apr 28 '16 at 15:17

1 Answers1

2

I answered this question with the help of dan-man.

I had to define a new 3D structure for the label tool:

import numpy as np
from scipy.dimage import label

str_3D = np.array([[[0, 0, 0],
                    [0, 0, 0],
                    [0, 0, 0]],

                   [[0, 1, 0],
                    [1, 1, 1],
                    [0, 1, 0]],

                   [[0, 0, 0],
                    [0, 0, 0],
                    [0, 0, 0]]], dtype='uint8')

Now the label returns the following for my subset:

>>> label(subset, structure=str_3D)
# outputs:
(array([[[1, 0, 0],
         [1, 0, 2],
         [0, 0, 0]],

        [[0, 0, 0],
         [3, 0, 4],
         [0, 0, 4]],

        [[0, 0, 0],
         [5, 0, 0],
         [0, 6, 6]],

        [[0, 0, 0],
         [7, 0, 0],
         [7, 7, 7]]]), 7)
Aelius
  • 1,029
  • 11
  • 22
Wilmar van Ommeren
  • 7,469
  • 6
  • 34
  • 65