Given a contiguous drawing of arbitrary pixels (e.g. on an HTML5 Canvas) is there any algorithm for finding the axis-aligned bounding box that is more efficient than simply looking at every pixel and recording the min/max x/y values?
相关问题
- Direct2D Only Partially Linking in C++ Builder
- Asynchronous threads drawing in Bitmaps Delphi
- Create depth map from 3d points
- OpenCL on Linux with integrated intel graphic chip
- How do I add a virtual GPU into Qemu?
相关文章
- Behavior of uniforms after glUseProgram() and spee
- How to smooth the blocks of a 3D voxel world?
- Calculating Vertex Normals of a mesh [duplicate]
- myBitmap.RawFormat is something different than any
- anyone can explain the “field of view”
- How to dodge pointrange ggplots on two levels?
- Generating a Voronoi Diagram around 2D Polygons
- How to get the transformation matrix of a 3d model
I dislike the current answer. Here's my code that I plugged into OP website. It's much faster in firefox and chrome.
The idea is check all pixels on x axis to see if there's a hit on the Y axis. If so update Y and increase X so we can scan for max X
Just scanline from top left to right and down to get y top,and similar algorithm with different directions for the rest.
Edit by Phrogz:
Here's a pseudo-code implementation. An included optimization ensures that each scan line does not look at pixels covered by an earlier pass:
The result (in practice) performs about the same as the brute-force algorithm for a single pixel, and significantly better as the object gets larger.
Demo: http://phrogz.net/tmp/canvas_bounding_box2.html
For fun, here's a visual representation of how this algorithm works:
It doesn't matter in what order you choose to do the sides, you just have to make sure that you take the previous results into account so that you are not double-scanning the corners.
You might be able to use some kind of binary search, or sample on a coarse grid then a successively finer grid. The correctness of this method depends on if 'holes' are allowed in your drawing.