将 rectangle/square 个区域合并成更大的区域 - imshow/python

Combining rectangle/square areas into bigger ones - imshow/python

过去两周我一直被这个问题困扰,试图找到一种方法将一组 rectangles/squares 组合成一个包含感兴趣领域的更大的问题。这项工作在 Python.

中完成

我正在研究一种仪器,它可以获取任何表面的 X 和 Y 方向的强度信息。我可以在分辨率阵列中进行实验,但正如预期的那样,更高的分辨率会显着增加实验的时间范围。我的目标是以低分辨率进行实验,然后进行更高分辨率的实验,但仅限于有一些有趣的地方。我附上了一张图片来向您展示我的意思。

除了 yellow/orange 点之外,我对其他任何东西都不特别感兴趣。所以我正在提取与我的最小阈值相对应的特定强度区域。我最终得到了扩展到 rectangle/square 大小的 XY 中心。我希望下图有 5 个感兴趣的区域,它们将包含热点周围的最小区域。

不幸的是,我最终得到的是这个(总共 53 个矩形代表 5 个区域):

问题出在矩形的数量上——它完全基于峰的强度。我想通过以下任一方式将它从 53 减少到 5:1)将重叠的矩形组合成一个大矩形,或者 2)只保留一个代表其他矩形的矩形。

到目前为止,我已经尝试了很多不同的方法来查看它,但我无法让它发挥作用。我探索了 wx 模块,我可以在其中检查矩形是否重叠,检查是否发现点彼此靠近,中心点是否在其他矩形内,等等...

我故意没有 post 任何代码,因为 none 它的效果特别好...

如果你们中的任何人可以帮助解决这个问题,我将永远感激不已!

更新 1 多亏了 Kenney,我得到了一个可以执行我希望它执行的操作的工作代码。到目前为止,我只在一组结果上测试过它,但一旦我分析得更多,我会再次更新。看看结果图像。

这是一个 post 处理矩形以将其聚类的示例。 我从 codereview 中获取了一些实用程序代码,因为我对 python:

完全陌生
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Rect(object):
    def __init__(self, p1, p2):
        '''Store the top, bottom, left and right values for points 
               p1 and p2 are the (corners) in either order
        '''
        self.left   = min(p1.x, p2.x)
        self.right  = max(p1.x, p2.x)
        self.bottom = min(p1.y, p2.y)
        self.top    = max(p1.y, p2.y)

    def __str__(self):
         return "Rect[%d, %d, %d, %d]" % ( self.left, self.top, self.right, self.bottom )

def range_overlap(a_min, a_max, b_min, b_max):
    '''Neither range is completely greater than the other
    '''
    return (a_min <= b_max) and (b_min <= a_max)

def rect_overlaps(r1,r2):
    return range_overlap(r1.left, r1.right, r2.left, r2.right) and range_overlap(r1.bottom, r1.top, r2.bottom, r2.top)

算法如下:

rectangles = [

    # A______
    # |      |
    # -------B

    #     A             B
    Rect( Point(10,10), Point(50,70)),
    Rect( Point( 8,10), Point(30,20)),
    Rect( Point(90,90), Point(99,99)),
];


clusters = [];

for rect in rectangles:
    matched = 0;
    for cluster in clusters:
        if ( rect_overlaps( rect, cluster ) ):
            matched=1
            cluster.left   = min( cluster.left,   rect.left   );
            cluster.right  = max( cluster.right,  rect.right  );
            cluster.top    = min( cluster.top,    rect.top    );
            cluster.bottom = max( cluster.bottom, rect.bottom );

    if ( not matched ):
        clusters.append( rect );

print "Clusters:"
for c in clusters:
    print c