How to find bounding rectangle?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
txtsd
Posts: 3
Joined: 2019-05-03T11:32:43-07:00
Authentication code: 1152

How to find bounding rectangle?

Post by txtsd »

Hello. I'm trying to extract the topleftmost subimage from an image. Nothing is known about the image at the time of processing, but it is in the same general format and has an alpha layer. The image is a kind of a mosaic, but there is no pattern to where subimages are placed.

In my first attempt, I tried to find X and Y (width and height) by trying looking for X number of fully transparent pixels at Xx1+0+X and then Y number of fully transparent pixels at 1xY+Y+0. If either of these fails, it tries again, but FOUND_Xx1+0+X and 1xFOUND_Y+Y+0. This recalculates with the actual width or height instead of looking with height and width equal.

Now this new case has me stumped. I'm not able to find either width or height with my algorithm.
Here are screenshots of bounding squares for width and height. 1 pixel off from actual content because it is checking for transparent pixels.

Image
Image

How should I proceed?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to find bounding rectangle?

Post by fmw42 »

Threshold your image to keep the white areas and make all else black. Then use -connected-components to get the bounding regions of each of the white regions. Take the largest white region which will be the first white region in the list, since they are listed in order of decreasing area.

So for your image (which I have cropped from your screen snap)

Code: Select all

convert img.png -auto-level -negate -threshold 0% -negate -type  bilevel \
-define connected-components:area-threshold=1000 \
-define connected-components:verbose=true \
-connected-components 4 \
null:

Objects (id: bounding-box centroid area mean-color):
  0: 1174x766+0+0 699.7,392.8 649500 gray(0)
  4: 566x566+0+13 293.5,326.3 230624 gray(255)
  154: 392x89+70+677 280.8,734.5 15560 gray(255)
  152: 112x47+214+582 262.7,599.1 3600 gray(255)


And take the one staring with 4:

Or in Unix code, we can extract that automatically:

Code: Select all

bbox=$(convert img.png -auto-level -negate -threshold 0% -negate -type  bilevel \
-define connected-components:area-threshold=1000 \
-define connected-components:verbose=true \
-connected-components 4 \
null: | grep "gray(255)" | head -n 1)
echo "$bbox"

  4: 566x566+0+13 293.5,326.3 230624 gray(255)

See https://imagemagick.org/script/connected-components.php
txtsd
Posts: 3
Joined: 2019-05-03T11:32:43-07:00
Authentication code: 1152

Re: How to find bounding rectangle?

Post by txtsd »

Thanks. This works for the images in the screenshots I posted, but those are the transfered alpha layers that I'm looking at.
How would I get IM to get that sort of an output first?

The original images look like this:
EDIT: snipped

EDIT: There are a variety of colors at times.
Last edited by txtsd on 2019-05-08T01:40:02-07:00, edited 1 time in total.
txtsd
Posts: 3
Joined: 2019-05-03T11:32:43-07:00
Authentication code: 1152

Re: How to find bounding rectangle?

Post by txtsd »

Figured it out. I just have to add

Code: Select all

-alpha extract
Post Reply