Professional Documents
Culture Documents
Lec 38
Lec 38
• Binary images.
Image region is divided into cells called pixels.
Each pixel is either black (0, background) or white
(1).
Example Binary Image
1
1 1
1 1
1 1 1 1
1
1
1
1
• Rotation.
• Expansion.
Each pixel is replaced by a 2k x 2k window of
pixels of the same intensity.
• Shrinking.
Each 2k x 2k window is replaced by a pixel
whose intensity is some function of that of the
window pixel intensities.
Shrinking Example
• k = 1.
• >= 2 white pixels in 21 x 21 window =>
white pixel.
Union & Intersection
• Image1 = roads.
• Image2 = rivers.
• Union(Image1, Image2) = roads and rivers.
• Intersection(Image1,Image2) = places
where a road crosses a river => bridge or
tunnel.
Image Representation
• n x n matrix.
Theta(n2) space.
Theta(n2) time for rotation, scaling, union,
intersection, and so on.
• Quad tree.
O(n2) space.
O(n2) time for rotation, scaling, union, intersection,
and so on.
For some images, the space and time could be as
little as O(1) (e.g., all white or all black images).
Quad Tree
NW NE
NW NE SE SW
SW SE
Quad Tree Example
NW NE
SW SE
Quad Tree Example
NW NE
SW SE
Quad Tree Example
NW NE
SW SE
Oct Tree
• 2k x 2k binary matrix.
• If k = 0, return a single-node quad tree.
Root is white if pixel is 1.
Root is black if pixel is 0.
From Matrix To Quad Tree
NW NE
SW SE
• If k > 0, recursively construct quad trees for
the four quadrants.
From Matrix To Quad Tree
NW NE
SW SE
• If the 4 quad tree roots are all black (white), return a
single node quad tree with a black (white) root.
• Otherwise, return a quad tree with a gray root whose
subtrees are the quadrant quad trees.
Complexity
SW NW
Clockwise Rotation By 90 Degrees
NW NE
SW NW
Recursive Algorithm
• If root is a leaf, return.
• Rotate the children of the root.
(NW, NE, SE, SW) (SW, NW, NE, SE)
• Recursively perform the rotation in the
subtrees of the root.
• Complexity is O(size of quad tree).
Quad tree size = # nodes.
• Quad tree size is usually substantially less
than matrix size.
Other Rotations
SW SE
Shrinking
• k = 1.
• >= 2 white pixels in 21 x 21 window =>
white pixel.
NW NE
SW SE
Shrinking
• k = 1.
• >= 2 white pixels in 21 x 21 window =>
white pixel.
NW NE
SW SE
Shrinking
• k = 1.
• >= 2 white pixels in 21 x 21 window =>
white pixel.
NW NE
SW SE
Algorithm To Shrink (k = 1)
• Root represents a 1 x 1 region => return.
• Root represents a 2 x 2 region.
Color the root white if it has >= 2 white children.
Color the root black if it has < 2 white children.
Otherwise, root is a leaf. Don’t change its color.
Make root a leaf (may already be a leaf) and return.
• Root represents a 2k x 2k, k > 1, region.
Shrink subtrees (if any) of root.
Recolor root if all subtrees now have same color.
• Complexity is O(size of quad tree).
Union
• Rules.
Do a pairwise union of corresponding pixels in
the two images.
Union is white iff at least one pixel of the pair
is white.
Recursive Union(A,B)
A B
• root(A) (root(B)) is white => return A (B).
• root(A) (root(B)) is black => return B (A).
• Both have a gray root.
Pairwise union the subtrees of A and B.
If all four of the returned subtrees have a white root,
return a single node tree with a white root.
Otherwise return a tree with a gray root and the four
returned subtrees as its subtrees.
• Complexity is O(|A| + |B|).
Intersection
• Rules.
Do a pairwise intersection of corresponding
pixels in the two images.
Intersection is white iff both pixels of the pair
are white.
Recursive Intersection(A,B)
A B
• root(A) (root(B)) is black => return A (B).
• root(A) (root(B)) is white => return B (A).
• Both have a gray root.
Pairwise intersect the subtrees of A and B.
If all four of the returned subtrees have a black root,
return a single node tree with a black root.
Otherwise return a tree with a gray root and the four
returned subtrees as its subtrees.
• Complexity is O(|A| + |B|).