Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Detect boundaries within .png's - and cropping

by jcb (Parson)
on Jul 09, 2020 at 02:25 UTC ( [id://11119059]=note: print w/replies, xml ) Need Help??


in reply to Detect boundaries within .png's - and cropping

PDL might be a faster way to implement the approach Fletch suggested.

Replies are listed 'Best First'.
Re^2: Detect boundaries within .png's - and cropping
by etj (Deacon) on Apr 21, 2022 at 07:58 UTC
    Intriguingly, there's no cropping type functionality in core PDL. A bit of a ponder suggests a good approach would be to (for rows), assuming an image of (rgba,x,y):
    $non_blank_rows = ($img->slice('(3)') != 0)->orover; ($low, $high) = $non_blank_rows->which->minmaximum;
    Then do the same for $img->xchg(1,2) for the columns, and hey presto, there's your bounding box!
      It turns out that making this broadcast properly over multiple images was impressively hard.

      which had a hardcoded limitation that it would only work on a 1-dim ndarray (fixed by imitating PDL::VectorValued's strategy of having a "how many set for each vector" output ndarray).

      Then actually using that with minmaximum also posed a problem, since the number of values per image might vary (in other words, the outputs might be ragged) - fixed that by having which fill the unset index-values with -1, and then setting that as the badvalue for the output ndarray, so minmaximum would ignore them.

      After that, the rest was easy! The new PDL::Image2D::crop works on a mask, so the above snippet would call

      $x1x2y1y2 = ($img->slice('(3)') != 0)->crop;
        This has now been released with PDL 2.079! See separate announcement for more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11119059]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found