Page 1 of 1

Find Bounding Box For B/W Image

Posted: 2010-04-26T08:58:48-07:00
by billbell52
I would like to find the bounding box for a B/W image. Example:

Image

What I want is the most left. right, top and bottom black pixel location. I plan to crop the image to that size or 1 or 2 pixels larger.

I am doing an image compare and I want to make it as fast as possible. I read the docs and examples and did not see anything obvious.

The site I used converted it to jpg. It is actually a bmp file.

Re: Find Bounding Box For B/W Image

Posted: 2010-04-26T10:41:48-07:00
by fmw42
this will get to the top left corner and bottom right corner: ulx=left, uly=top, brx=right, bry=bottom

convert 23hu8ic.jpg -trim 23hu8ic_crop.gif
width=`convert 23hu8ic_crop.gif -format "%w" info:`
height=`convert 23hu8ic_crop.gif -format "%h" info:`
ulx=`convert 23hu8ic_crop.gif -format "%X" info:`
uly=`convert 23hu8ic_crop.gif -format "%Y" info:`
brx=`convert 23hu8ic_crop.gif -format "%[fx:$ulx+$width-1]" info:`
bry=`convert 23hu8ic_crop.gif -format "%[fx:$uly+$height-1]" info:`
echo "ulx=$ulx; uly=$uly; brx=$brx; bry=$bry;"
ulx=+24; uly=+16; brx=143; bry=135;

see -trim and string formats at http://www.imagemagick.org/script/escape.php

This is for unix. If you are on Windows, see http://www.imagemagick.org/Usage/windows/

Re: Find Bounding Box For B/W Image

Posted: 2010-04-26T11:08:12-07:00
by billbell52
Thanks for info. Works perfectly. I completely missed the -trim command. Can't say enough for your tremendous support. THANKS!!!