python-2.7 - 合并重叠的矩形(python)

标签 python-2.7 opencv opencv3.0

经过研究,我遇到了几个类似的问题:OpenCV groupRectangles - getting grouped and ungrouped rectangles (大多数在 C++ 中)。然而,它们都不是坚固的。我想将重叠的矩形组合成一个。 Image

我的进步:

for cnt in large_contours:
    x,y,w,h = cv2.boundingRect(cnt)
    mec=x,y,w,h
    rectVec=cv2.rectangle(img_and_contours,(x,y),(x+w,y+h),(0,255,0),2)
    #cv2.rectangle(img_and_contours, cv2.boundingRect(large_contours[cnt]),(0,255,0));
    rectList, weights = cv2.groupRectangles(mec, 3,0.2)

我只发布了我的一段代码。我希望 groupRectangle 会做我想做的事,但什么也没做,反而给我一个错误

rectList,weights = cv2.groupRectangles(mec,3,0.2) TypeError: rectList Blockquote

最佳答案

这是一段对我有用的代码

def merge_overlapping_zones(zones,delta_overpap = 30):

index = 0

if zones is None: return zones
while index < len(zones):
    no_Over_Lap = False
    while no_Over_Lap == False and len(zones) > 1 and index < len(zones):
        zone1 = zones[index]
        tmpZones = np.delete(zones, index, 0)
        tmpZones = [tImageZone(*a) for a in tmpZones]

        for i in range(0, len(tmpZones)):
            zone2 = tmpZones[i]

            # check left side broken
            if zone2.x >= delta_overpap and zone2.y >= delta_overpap:
                t = tImageZone(zone2.x - delta_overpap, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)
            elif zone2.x >= delta_overpap:
                t = tImageZone(zone2.x - delta_overpap, zone2.y, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)
            else:
                t = tImageZone(zone2.x, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)

            if (is_zone_overlap(zone1, t) or is_zone_overlap(zone1, zone2)):
                tmpZones[i] = merge_zone(zone1, zone2)
                zones = tmpZones
                no_Over_Lap = False
                break

            no_Over_Lap = True
    index += 1

return zones

`

关于python-2.7 - 合并重叠的矩形(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847923/

相关文章:

c++ - OpenCV - 去除凸性缺陷(Python 到 C++)

java - 在 HoughCircles(), OpenCV 给出的一系列点上使用 warpPerspective()

python - 如何从存在轻微背景的图像中提取文本?

video-capture - opencv 3.4.1 readFrame 中错误 : Assertion failed (chunk. m_size <= 0xFFFF)

c++ - 如何将视频流数据转换为 cv::gpumat?

python - 带有 __init__.pyx : Possible? 的 Cython 包

python - 在 Windows 上安装 MayaVi,使用 Python 2.7

python - 使用 csv.reader 以 utf-8 打开文件

c++ - 使用C++流式传输OpenCV mat数据

python-2.7 - cv2.destroyWindow() 没有按预期工作