I suspect this is one of those cases where vector operations don't buy you anything; indeed, they probably force a lot of extra work.
Once you have your "segmented image" -- ie. each object colored differently -- then a single left to right, top-to bottom pixel scan will discover the bounding boxes of all the objects.
In pseudo code:
use constant { LEFT=>0, RIGHT=>1, TOP=>2, BOTTOM=>3 }; my @bounds; for my $y ( 0 .. $height -1 ){ for my $x ( 0 .. $width - 1 ) { my $pixel = $image->pixel( $x, $y ); $bounds[ $pixel ][ LEFT ] = $x if $x < ( $bounds[ $pixel ][ +LEFT ] // 1e99 ); $bounds[ $pixel ][ RIGHT ] = $x if $x > ( $bounds[ $pixel ][ +RIGHT ] // 0 ); $bounds[ $pixel ][ TOP ] = $y if $y < ( $bounds[ $pixel ][ +TOP ] // 1e99 ); $bounds[ $pixel ][ BOTTOM ] = $y if $y > ( $bounds[ $pixel ][ +BOTTOM ] // 0 ); } }
Unfortunately I cannot help you with applying that to PDL because I gave up on trying to find an efficient way to apply a piece of perl code to all the elements of a piddle.
In reply to Re: PDL: Looking for efficient way to extract sub-images, by finding bounding boxes of "objects"
by BrowserUk
in thread PDL: Looking for efficient way to extract sub-images, by finding bounding boxes of "objects"
by vr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |