Finding Contiguous Areas of Bits in 2D Bit Array

One approach would be a perimeter walk.
Given a starting point anywhere along the edge of the shape, remember that point.

Start the bounding box as just that point.

Walk the perimeter using a clockwise rule set – if the point used to get to the current point was above, then first look right, then down, then left to find the next point on the shape perimeter. This is kind of like the simple strategy of solving a maze where you continuously follow a wall and always bear to the right.

Each time you visit a new perimeter point, expand the bounding box if the new point is outside it (i.e. keep track of the min and max x and y.

Continue until the starting point is reached.

Cons: if the shape has lots of single pixel ‘filaments’, you’ll be revisiting them as the walk comes back.

Pros: if the shape has large expanses of internal occupied space, you never have to visit them or record them like you would if you were recording visited pixels in a flood fill.

So, conserves space, but in some cases at the expense of time.

Edit

As is so often the case, this problem is known, named, and has multiple algorithmic solutions. The problem you described is called Minimum Bounding Rectangle. One way to solve this is using Contour Tracing. The method I described above is in that class, and is called Moore-Neighbor Tracing or Radial Sweep. The links I’ve included for them discuss them in detail and point out a problem I hadn’t caught. Sometimes you’ll revisit the start point before you have traversed the entire perimeter. If your start point is for instance somewhere along a single pixel ‘filament’, you will revisit it before you’re done, and unless you consider this possibility, you’ll stop prematurely. The website I linked to talks about ways to address this stopping problem. Other pages at that website also talk about two other algorithms: Square Tracing, and Theo Pavlidis’s Algorithm. One thing to note is that these consider diagonals as contiguous, whereas you don’t, but that should just something that can be handled with minor modifications to the basic algorithms.

An alternative approach to your problem is Connected-component labeling. For your needs though, this may be a more time expensive solution than you require.

Additional resource:

Moore Neighbor Contour Tracing Algorithm in C++

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)