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.
imagick - how to find coordinates of edges in an image ?
- 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 ?
See the string format %@ at http://www.imagemagick.org/script/escape.php.
# create test image
# get crop coordinates without cropping (use -fuzz since jpg background and foreground is not a perfectly constant color due to compression)
100x100+50+50
# create test image
Code: Select all
convert -size 200x200 xc:black -size 100x100 xc:white -geometry +50+50 -compose over -composite test.jpg
Code: Select all
convert test.jpg -fuzz 5% -format "%@" info:
Re: imagick - how to find coordinates of edges in an image ?
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.
If that is the case dave007 you may have a problem as Imagick does not have all the functionality of Imagemagick.
Re: imagick - how to find coordinates of edges in an image ?
thanks, mw42.
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 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.
Re: imagick - how to find coordinates of edges in an image ?
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.
Imagick is not written or maintained by the Imagemagick developers so there are not many Imagick experts here.
-
- 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 ?
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
Re: imagick - how to find coordinates of edges in an image ?
@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.
@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.
- 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 ?
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
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
Re: imagick - how to find coordinates of edges in an image ?
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,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 :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.
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;
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'];
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.