extracting a shape from a transparent background efficiently

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
gubach
Posts: 37
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

extracting a shape from a transparent background efficiently

Post by gubach » 2014-12-20T04:21:18-07:00

Suppose an image is given consisting of a monochrome shape (polygon) and a transparent background; there is no transparent border around the shape; points or edges touching the image border. If such images are rotated the ratio of the image may increase and the shape is not any more touching the image borders.

Has IM a function that can directly and efficiently extract a shape from a transparent background (the method must be available in PerlMagick)?

Until now I use four subroutines that determine the distance from left, right, top, bottom to the rotated shape and use those values to compute the parameters for a crop-command but this is expensive because in principle row by row and column by column must be checked for non transparent pixels.

Or perhaps there is a special kind of rotation available: In http://www.imagemagick.org/Usage/distorts/#srt one can read: „One argument is just a simple rotation about the images center, basically producing a similar result to the older Rotate Operator, but without any image size increase.“ But this will cause problems by potentially cutting of pieces of the shape.

Thanks for any help!

snibgo
Posts: 11596
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: extracting a shape from a transparent background efficiently

Post by snibgo » 2014-12-20T05:10:45-07:00

I don't understand what you want. Does "-trim" or "-format %@" provide it? This tells you the four sides of the rectangle that encloses the shape.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 24928
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: extracting a shape from a transparent background efficiently

Post by fmw42 » 2014-12-20T11:27:44-07:00

If you rotate the image and give it a transparent background for the rotation, then as user snibgo said, you can use (the Perlmagick equivalent of ) -trim to remove all extra background to get the rotated shape so that it again touches the sides of the image. But are you also asking to unrotate the shape back to its original orientation? Can you post the before and after images to dropbox.com and put the URL here so we can see what you are trying to do?

Also please always provide your IM version and platform.

gubach
Posts: 37
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: extracting a shape from a transparent background efficiently

Post by gubach » 2014-12-21T08:23:02-07:00

Thaks for the answers. I only want to remove all extra transparent background after rotation. Version is: ImageMagick 6.7.8-7 2013-07-10 Q8

I looked in the description of the (parameterless) method:
Trim remove edges that are the background color from the image
and this means a usage like $image=>Trim();

I have not a dropbox account but I uploaded a "before" and "after" shape example here:
http://vi-anec.de/Alben/shape_before_rotation.png
http://vi-anec.de/Alben/shape_after_rotation.png
http://vi-anec.de/Alben/shape_after_trim.png

and this is exactly what I wanted!! Very helpfull! Thank you very much!

Post Reply