Page 1 of 1

extracting a shape from a transparent background efficiently

Posted: 2014-12-20T04:21:18-07:00
by gubach
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!

Re: extracting a shape from a transparent background efficiently

Posted: 2014-12-20T05:10:45-07:00
by snibgo
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.

Re: extracting a shape from a transparent background efficiently

Posted: 2014-12-20T11:27:44-07:00
by fmw42
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.

Re: extracting a shape from a transparent background efficiently

Posted: 2014-12-21T08:23:02-07:00
by gubach
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!