An octree is a tree data structure in which each internal node has exactly eight children. Octrees are most often used to partition a three-dimensional space by recursively subdividing it into eight octants. Octrees are the three-dimensional analog of quadtrees. The name is formed from oct + tree, but note that it is normally written "octree" with only one "t". Octrees are often used in 3D graphics and 3D game engines.
For spatial representation
Each node in an octree subdivides the space it represents into eight octants. In a point region octree, the node stores an explicit three-dimensional point, which is the "center" of the subdivision for that node; the point defines one of the corners for each of the eight children. In a matrix based octree, the subdivision point is implicitly the center of the space the node represents. The root node of a PR octree can represent infinite space; the root node of an MX octree must represent a finite bounded space so that the implicit centers are well-defined. Note that Octrees are not the same as k-d trees: k-d trees split along a dimension and octrees split around a point. Also k-d trees are always binary, which is not the case for octrees. By using a depth-first search the nodes are to be traversed and only required surfaces are to be viewed.
History
The use of octrees for 3D computer graphics was pioneered by Donald Meagher at Rensselaer Polytechnic Institute, described in a 1980 report "Octree Encoding: A New Technique for the Representation, Manipulation and Display of Arbitrary 3-D Objects by Computer", for which he holds a 1995 patent "High-speed image generation of complex solid objects using octree encoding"
The octree color quantization algorithm, invented by Gervautz and Purgathofer in 1988, encodes image color data as an octree up to nine levels deep. Octrees are used because and there are three color components in the RGB system. The node index to branch out from at the top level is determined by a formula that uses the most significant bits of the red, green, and blue color components, e.g. 4r + 2g + b. The next lower level uses the next bit significance, and so on. Less significant bits are sometimes ignored to reduce the tree size. The algorithm is highly memory efficient because the tree's size can be limited. The bottom level of the octree consists of leaf nodes that accrue color data not represented in the tree; these nodes initially contain single bits. If much more than the desired number of palette colors are entered into the octree, its size can be continually reduced by seeking out a bottom-level node and averaging its bit data up into a leaf node, pruning part of the tree. Once sampling is complete, exploring all routes in the tree down to the leaf nodes, taking note of the bits along the way, will yield approximately the required number of colors.
Implementation for point decomposition
The example recursive algorithm outline below decomposes an array of 3-dimensional points into octree style bins. The implementation begins with a single bin surrounding all given points, which then recursively subdivides into its 8 octree regions. Recursion is stopped when a given exit condition is met. Examples of such exit conditions are:
When a bin contains fewer than a given number of points
When a bin reaches a minimum size or volume based on the length of its edges
When recursion has reached a maximum number of subdivisions
function = OcTree binDepths = % Initialize an array of bin depths with this single base-level bin binParents = % This base level bin is not a child of other bins binCorners = % It surrounds all points in XYZ space pointBins = 1 % Initially, all points are assigned to this first bin divide % Begin dividing this first bin function divide
% If this bin meets any exit conditions, do not divide it any further. binPointCount = nnz binEdgeLengths = binCorners - binCorners binDepth = binDepths exitConditionsMet = binPointCountvalue if exitConditionsMet return; % Exit recursive function end % Otherwise, split this bin into 8 new sub-bins with a new division point newDiv = + binCorners) / 2 for i = 1:8 newBinNo = length + 1 binDepths = binDepths + 1 binParents = binNo binCorners = oldBinMask = pointBinsbinNo % Calculate which points in pointBinsbinNo now belong in newBinNo pointBins = newBinNo % Recursively divide this newly created bin divide end
Example color quantization
Taking the full list of colors of a 24-bit RGB image as point input to the Octree point decomposition implementation outlined above, the following example show the results of octree color quantization. The first image is the original, while the second is the quantized image using octree decomposition, with each pixel assigned the color at the center of the octree bin in which it falls. Alternatively, final colors could be chosen at the centroid of all colors in each octree bin, however this added computation has very little effect on the visual result. % Read the original RGB image Img = imread; % Extract pixels as RGB point triplets pts = reshape; % Create OcTree decomposition object using a target bin capacity OT = OcTree); % Find which bins are "leaf nodes" on the octree object leafs = find &... ismember); % Find the central RGB location of each leaf bin binCents = mean; % Make a new "indexed" image with a color map ImgIdx = zeros, size); for i = 1:length pxNos = find; ImgIdx = i; end ImgMap = binCents / 255; % Convert 8-bit color to MATLAB rgb values % Display the original 532818-color image and resulting 184-color image figure subplot, imshow title) subplot, imshow title