3

I have divided an image into objects (slices) using the method kindly contributed by unutbu and Joe Kington at this question: Rectangular bounding box around blobs in a monochrome image using python and have a list of these objects which takes the following form:

the_blobs = [(slice(dy.start, dy.stop, None), slice(dx.start, dx.stop, None))]

dy.start gives the starting y-pixel value and dy.stop gives the final y-pixel value, and the same deal for dx.

Within the list there are some objects which overlap i.e. one tiny object (a square) is within a larger object such as a circle. When this occurs I want to remove the "enclosed" object from the list (because the circle already has it included) e.g.

current list

the_blobs = [(slice(100L, 1000L, None), slice(100L, 1000L, None)), 
(slice(150L, 220L, None), slice(150L, 220L, None)), 
(slice(1001L, 2000L, None), slice(1500L, 1700L, None)),
(slice(2001L, 2200L, None), slice(1800L, 1890L, None))] 

ideal list (with object removed)

the_blobs = [(slice(100L, 1000L, None), slice(100L, 1000L, None)), 
(slice(1001L, 2000L, None), slice(1500L, 1700L, None)),
(slice(2001L, 2200L, None), slice(1800L, 1890L, None))] 

I should note that as part of the aforementioned question a suggestion was made to use the following code:

data_slices = ndimage.find_objects(coded_paws)
for s in data_slices:
    filled[s] = True
coded_paws, num_paws = ndimage.label(filled)
data_slices = ndimage.find_objects(coded_paws)  

However this doesn't seem to work 100% of the time, and it really was a contribution slightly beyond the scope of the original question so I'm re-opening this part as a separate, specific question.

Any ideas on how I can achieve this?

objects as shown in list

EDIT: Here's an actual image example which doesn't work with the above code

enter image description here

processing this returns

enter image description here

and

enter image description here

Ideally I'd like to remove the last image from the list of slices

Community
  • 1
  • 1
user714852
  • 2,054
  • 4
  • 30
  • 52

1 Answers1

1

Obviously you can take an O(n^2) approach that checks each blob against all other blobs and determines if it should be removed by checking if blob1.dx.start > blob2.dx.start and blob1.dy.start > blob2.dy.start and blob1.dx.stop < blob2.dx.stop and blob1.dy.stop < blob2.dy.stop (if this condition is true, it is ok to remove blob1 from the list). If your total blob count is pretty low, this should work unless I am missing something.

If you are looking for an optimized solution, it would be helpful to know about how many blobs there are and how common the condition is.

Paul
  • 42,322
  • 15
  • 106
  • 123