imagick - how to find coordinates of edges in an image ?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
dave007
Posts: 4
Joined: 2016-01-22T12:20:30-07:00
Authentication code: 1151

imagick - how to find coordinates of edges in an image ?

Post by dave007 »

given an image composed of a plain black field with a single white rectangle inside it somewhere, how can i extract the coordinates of the rectangle ? i've googled and googled and found numerous articles on edge detection using hough lines, convolution, morphology, etc., but all of these actually edge the image or convert it in some way. i don't want to change the image, all i want to do is find where the edges are.

obviously, i could simply iterate over the entire width+height of the image and look at the pixel colours (as some posts suggest), but that seems horribly inefficient. is there no built-in algorithm ? it seems like this must be part of imagick somewhere, otherwise how could it actually find and draw the edges of internal images ? but i'm having trouble finding out how to get at it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: imagick - how to find coordinates of edges in an image ?

Post by fmw42 »

See the string format %@ at http://www.imagemagick.org/script/escape.php.

# create test image

Code: Select all

convert -size 200x200 xc:black -size 100x100 xc:white -geometry +50+50 -compose over -composite test.jpg
# get crop coordinates without cropping (use -fuzz since jpg background and foreground is not a perfectly constant color due to compression)

Code: Select all

convert test.jpg -fuzz 5% -format "%@" info:
100x100+50+50
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: imagick - how to find coordinates of edges in an image ?

Post by Bonzo »

Looks to me the OP wants an Imagick method and not a command line one.

If that is the case dave007 you may have a problem as Imagick does not have all the functionality of Imagemagick.
dave007
Posts: 4
Joined: 2016-01-22T12:20:30-07:00
Authentication code: 1151

Re: imagick - how to find coordinates of edges in an image ?

Post by dave007 »

thanks, mw42.
Bonzo wrote:Looks to me the OP wants an Imagick method and not a command line one.

If that is the case dave007 you may have a problem as Imagick does not have all the functionality of Imagemagick.
that is correct. the app is in php. is there a way i could do this in imagick ? if there's a way to do that, even w/a whole string of commands, i'd prefer to do it that way. i suppose i could always kludge it w/system(), but i'd rather not.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: imagick - how to find coordinates of edges in an image ?

Post by Bonzo »

You do not need to kludge it with system() as fmw42's code will work; although you might need to use exec() rather than system().

Imagick is not written or maintained by the Imagemagick developers so there are not many Imagick experts here.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: imagick - how to find coordinates of edges in an image ?

Post by snibgo »

Are the sides of the rectangle always parallel to the overall images sides (ie x and y axes)? If so, Fred's suggestion of "-format %@" will do the job. If they are not parallel to the overall sides, the job is harder.
snibgo's IM pages: im.snibgo.com
dave007
Posts: 4
Joined: 2016-01-22T12:20:30-07:00
Authentication code: 1151

Re: imagick - how to find coordinates of edges in an image ?

Post by dave007 »

@bonzo - thanks for the heads-up, i didn't know the development teams were different.

@snibgo - yes, always parallel. if i can't find a way to do this w/o resorting to a system call -- whether it's system() or exec() makes little difference -- then i'll use his solution, it seemed to do exactly what i'm looking for.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: imagick - how to find coordinates of edges in an image ?

Post by fmw42 »

Use the Imagick equivalent of

convert test.jpg -fuzz 5% -trim tmp.png

Then use the equivalent of

identify -verbose tmp.png

to get the page geometry and image size by parsing the results.

See http://us3.php.net/manual/en/imagick.identifyimage.php and http://www.imagemagick.org/script/identify.php
dave007
Posts: 4
Joined: 2016-01-22T12:20:30-07:00
Authentication code: 1151

Re: imagick - how to find coordinates of edges in an image ?

Post by dave007 »

i ended up doing what amounts to that, yes.

i posted over on stackexchange and got a similar suggestion. it turns out it is surprisingly simple.

for the image as described,

Code: Select all

$im = new Imagick(realpath('./image.jpg'));
$im->trimImage(0);
$pagedata = $im->getImagePage();
$im->setImagePage(0, 0, 0, 0);
$x = $pagedata['x'];
$y = $pagedata['y'];
$w = $im->width;
$h = $im->height;
a side effect of the trimImage() function is that it will rebase the page data's x and y coordinates. the setImagePage() is necessary because otherwise the whole height and width of the original image will be returned.

it is even easier if the image is not exactly as originally described -- i.e., if the image uses transparency instead of BlackPixel, such as is common with .png :

Code: Select all

$im = new Imagick(realpath('./image.png'));
$pagedata = $im->getImagePage();
$x = $pagedata['x'];
$y = $pagedata['y'];
$w = $pagedata['width'];
$h = $pagedata['height'];
i.e., it turns out all of the information can be retrieved with getImagePage().

thanks much for your help, i'm new to imagemagick and imagick, and the power of what you can do w/them is impressive -- and sometimes a little confusing. :lol:
Post Reply