gpt4 book ai didi

python - 将矩形/正方形区域组合成更大的区域 - imshow/python

转载 作者:行者123 更新时间:2023-11-30 23:03:46 24 4
gpt4 key购买 nike

过去两周我一直在解决这个问题,试图找到一种方法将一组矩形/正方形组合成一个包含感兴趣区域的更大矩形/正方形。这项工作是用 Python 完成的。

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

enter image description here

除了黄色/橙色 Blob 之外,我对任何东西都不特别感兴趣。因此,我正在提取与我的最小阈值相对应的特定强度的区域。我最终得到了 XY 中心,并将其扩展为矩形/正方形大小。我希望下图有 5 个感兴趣的区域,其中包含热点周围的最小区域。

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

问题在于矩形的数量 - 它纯粹基于峰值的强度。我想通过以下任一方法将其从 53 个减少到 5 个:1)将重叠的矩形合并为一个大矩形,或者 2)仅保留一个代表其他矩形的矩形。

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

我故意没有发布任何代码,因为它们都不能很好地工作......

如果你们能帮忙解决这个问题,我将不胜感激!

更新1感谢肯尼,我有了一个可以完成我想要的工作的代码。到目前为止,我只对一组结果进行了测试,但一旦我分析更多结果,我将再次更新。看看结果图像。

enter image description here

最佳答案

下面是一个对矩形进行后处理以对其进行聚类的示例。我从 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

关于python - 将矩形/正方形区域组合成更大的区域 - imshow/python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33984151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com