1

I have a 2D array in Python containing values of either 0 or 1, arranged to form various shapes. For my current project I need a method to distinguish between the shapes in the image.

I am currently attempting to do this by setting the values of the first shape to 1, the second shape values to 2 and so on. However, this is proving too difficult for me to code, so I was wondering if anyone knew a good algorithm to do this, or knew of another method. Thanks.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
taylor993
  • 49
  • 1
  • 3
  • As with every question like this: What have you tried? Do you have some example code, what's not working about what you've tried. Do you have an algorithm to do this written down on paper? – will Feb 23 '15 at 09:51
  • Based on the question I do not understand what you try to accomplish or what the shapes and datastructures are (are the 2D array an binary bitmap which as a whole is the 'shape' you refer to?). Please describe the problem you try to solve, what the shapes are, what you have tries in term of algorithm and give code examples – Johan Feb 23 '15 at 09:53
  • It would helpful if you could add your existing code to clarify what you are trying to achieve, and where the problems are. – mfitzp Feb 23 '15 at 10:08
  • related: [Finding number of colored shapes from picture using Python](http://stackoverflow.com/q/5298884/4279) – jfs Feb 23 '15 at 12:01

1 Answers1

2

I suspect you are looking for label features which is available in scipy.ndimage. Despite the name, it is not neccessarily specific to image analysis. Quoting from the documentation "Any non-zero values in input are counted as features and zero values are considered the background."

The result will be an array, of the same size, with features numbered. For example, the following code:

import numpy as np
from scipy.ndimage import measurements

arr = np.array([
[0,0,0,0,0,0],
[0,1,1,0,0,0],
[0,1,1,0,0,1],
[0,0,0,0,1,1],
[0,0,0,0,1,1]
])

labeled_array, number_of_features = measurements.label(arr)
print(labeled_array)

...will produce the following output, with the two features numbered 1 and 2 respectively:

array([[0, 0, 0, 0, 0, 0],
       [0, 1, 1, 0, 0, 0],
       [0, 1, 1, 0, 0, 2],
       [0, 0, 0, 0, 2, 2],
       [0, 0, 0, 0, 2, 2]], dtype=int32)

The second return parameter contains the number_of_features in the array (here 2).

mfitzp
  • 15,275
  • 7
  • 50
  • 70
  • Thank you! This is just what i was looking for and worked perfectly. cheers – taylor993 Feb 23 '15 at 14:35
  • Great, happy to help! If the answer solved your issue, please consider marking it as accepted (green tick on the top left). Cheers. – mfitzp Feb 23 '15 at 14:39