http://qs1969.pair.com?node_id=1176081

vr has asked for the wisdom of the Perl Monks concerning the following question:

For a kind of OCR-ish task, I need to find bboxes of all "objects" in 2D image. Then I can use slicing to extract sub-images as virtual piddles.

Suppose there's an image with 3 "objects":

pdl> p $x [ [0 0 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 1 0] [1 0 1 1 1 0] [1 0 0 1 0 0] [1 0 0 0 0 1] [0 0 0 0 1 1] ]

And segmented:

pdl> $s = cc8compt $x pdl> p $s [ [0 0 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 2 0] [1 0 2 2 2 0] [1 0 0 2 0 0] [1 0 0 0 0 3] [0 0 0 0 3 3] ]

That's quite fast. And then:

p Dumper map { $obj = whichND( $s == $_ ); [ [ $obj-> slice( 0 )-> minmax ], [ $obj-> slice( 1 )-> minmax ], ] } 1 .. $s-> max; $VAR1 = [ [ 0, 0 ], [ 1, 5 ] ]; $VAR2 = [ [ 2, 4 ], [ 2, 4 ] ]; $VAR3 = [ [ 4, 5 ], [ 5, 6 ] ];

That gives bboxes in data structure I want. But it's too slow - tens of seconds for some-megapixel image and hundred of "objects", before I even began to use these data.

I wonder if it can be faster. Maybe to skip creating of a mask ($s == $_), i.e. hundred of them. Plus, the whichND gives a piddle of _all_ indexes - when all I want is a bounding box. Maybe there's a way to e.g. efficiently collapse a dimension looking for required index in segmented image ($s, above). I feel I'm missing something obvious. Any ideas?