I think something like this might work.
type Point
{
int x,y
}
type Rectangle
{
Point upperLeft, lowerRight;
}
Let S = the set of rectangles found in source array
Let Result = Empty list of rectangles
For every rectangle A in S
For every rectangle B in S that is Not A
Let Tr = new Rectangle(
new Point(Min(A.upperleft.x, B.upperleft.X), Min(A.upperleft.Y, B.upperleft.Y)),
new Point(Min(A.Lowerright.x, B.Lowerright.X), Min(A.Lowerright.Y, B.Lowerright.Y));
If (A.Area + B.Area) / Tr.Area > SomeThreshold Then
S.Remove(A)
S.Remove(B)
Result.Add(Tr)
For every rectangle I in S (that is not A or B)
If I is completely inside Tr Then S.Remove(I)
End
End
End
End
Sorry about the pseudo code, I might try to know this up in C# when I get home from work.
Hope this helps,
Fredrik