Navin
Navin

Reputation: 4105

image feature detection with large structuring element

I am trying to extract some features from an image but each of the extracted features are really small. The easiest way to extract larger features seems to be to use a larger structuring element but the following code fails when ITER > 1.

from scipy import ndimage,misc
lena=misc.lena().astype(float64)
lena/=ndimage.maximum(lena)
lena=lena>0.54# convert to binary image
       #   =====================  
ITER=1 # || FAILS WHEN ITER > 1 ||
       #   =====================
struct=ndimage.generate_binary_structure(2,1)
struct=ndimage.iterate_structure(struct,ITER)
lena_label,n =ndimage.label(lena,struct)
slices=ndimage.find_objects(lena_label)
images=[lena[sl] for sl in slices]
imshow(images[0])

.

RuntimeError: structure dimensions must be equal to 3

Upvotes: 2

Views: 854

Answers (1)

mmgp
mmgp

Reputation: 19241

The parameter structure for the ndimage.label function is used to determine the connectivity of the input. When you represent the input as a rectangular matrix, this connectivity commonly regards either the 4 or the 8 neighbors around a point p. Scipy follows this convention and limits the accepted structure to such cases, therefore it raises an error when anything larger than 3x3 is passed to the function.

If you really want to do such thing, first you need to define very clearly the connectivity you are trying to describe. Then you need to implement it. A simpler way is to first dilate the input, and then label it. This will effectively give the larger features that would be labeled with a larger structure parameter.

Upvotes: 3

Related Questions