基于python 将图像上同一行距离相近的矩形框融合

发布于:2025-05-30 ⋅ 阅读:(18) ⋅ 点赞:(0)
import os
import cv2
import numpy as np



def get_files(path):
    """ 获取指定路径下所有文件名称 """
    files = []
    for filename in os.listdir(path):
        if os.path.isfile(os.path.join(path, filename)):
            files.append(filename)
    return files


def split_to_name_ext(image_name_ext):
    name, ext = os.path.splitext(image_name_ext)
    return name, ext


def gen_empty_txt(txt_path):
    with open(txt_path, "w") as f:
        pass



def calculate_center(rect):
    """计算矩形的中心点"""
    return ((rect[0] + rect[2]) / 2, (rect[1] + rect[3]) / 2)

def merge_rects(rect1, rect2):
    """合并两个矩形"""
    return [min(rect1[0], rect2[0]), min(rect1[1], rect2[1]), 
            max(rect1[2], rect2[2]), max(rect1[3], rect2[3])]

def distance(x1, x2):
    """计算两个中心点之间的距离"""
    return abs(x1-x2)


def merge_close_rects(rects, threshold):
    """合并距离相近的矩形"""

    """计算矩形的中心点"""
    # centers = [calculate_center(rect) for rect in rects]
    merged = []
    
    used = [False] * len(rects)
    print(used)

    for i in range(len(rects)):
        if not used[i]:#表示没有处理过的矩形框
            new_rect = rects[i]
            print(new_rect)
            for j in range(i + 1, len(rects)):
                compara_rect=rects[j]
                print('目前比对的矩形的坐标为',compara_rect)
                distance_x=distance(new_rect[0], compara_rect[0])
                distance_y=distance(new_rect[1], compara_rect[1])
                
                if (distance_x < threshold and distance_y<10):
                    print('两个矩形框的距离为',distance_x)
                    print('两个矩形框的距离为',distance_y)
                    new_rect = merge_rects(new_rect, compara_rect)
                    used[j] = True
            merged.append(new_rect)
    
    return merged





if __name__ == '__main__':

    small_box=[[712, 824, 718, 837], [621, 991, 629, 1005], [1531, 882, 1541, 896], [1583, 918, 1591, 929], [1553, 931, 1561, 946], [1601, 934, 1610, 948], [1600, 1033, 1608, 1048], [1518, 1079, 1525, 1094], [1547, 1164, 1553, 1177], [1604, 1164, 1610, 1177], [1608, 1162, 1616, 1177], [1620, 1164, 1626, 1177], [1535, 1180, 1541, 1193], [1595, 1197, 1601, 1210], [1603, 1195, 1610, 1210], [1537, 1212, 1545, 1226], [1543, 1211, 1550, 1226], [1604, 1212, 1612, 1226], [1522, 1346, 1528, 1359], [1543, 1476, 1552, 1491], [1623, 1479, 1631, 1491], [1308, 1513, 1317, 1527], [2176, 800, 2183, 815], [2181, 802, 2187, 815], [2203, 803, 2210, 818], [2201, 820, 2208, 831], [2096, 853, 2102, 864], [2106, 853, 2112, 864], [2127, 853, 2133, 864], [2162, 902, 2169, 914], [2175, 902, 2181, 914], [2193, 902, 2200, 914], [2212, 902, 2219, 914], [2179, 1018, 2187, 1029], [2187, 1018, 2203, 1029], [2186, 1034, 2201, 1046], [2199, 1034, 2219, 1046], [2161, 1081, 2168, 1095], [2189, 1308, 2197, 1323], [2205, 1307, 2211, 1320], [2208, 1308, 2216, 1320], [2148, 1371, 2156, 1386], [2178, 1374, 2185, 1386], [2204, 1374, 2211, 1385], [2208, 1374, 2216, 1386], [1995, 1404, 2002, 1419], [2009, 1404, 2020, 1422], [2022, 1407, 2031, 1418], [2079, 1404, 2086, 1419], [2086, 1404, 2093, 1418], [2089, 1422, 2095, 1435], [2136, 1457, 2147, 1471], [2149, 1457, 2160, 1471], [2162, 1457, 2170, 1468], [2066, 1473, 2081, 1484], [2129, 1473, 2140, 1485], [2138, 1473, 2146, 1484], [2155, 1473, 2172, 1485], [2178, 1473, 2185, 1485], [2183, 1473, 2194, 1485], [2088, 1506, 2096, 1520], [2117, 1506, 2124, 1518], [2148, 1506, 2155, 1518], [2158, 1506, 2171, 1520], [2174, 1506, 2181, 1517], [2188, 1506, 2195, 1518], [2197, 1506, 2205, 1517], [2142, 1523, 2159, 1534], [2161, 1523, 2168, 1534], [2168, 1523, 2176, 1536], [2174, 1523, 2181, 1534], [2196, 1523, 2203, 1534], [2201, 1523, 2216, 1534], [1985, 1539, 1992, 1554], [2027, 1539, 2033, 1551], [2051, 1539, 2059, 1551], [2060, 1539, 2068, 1551], [1981, 1552, 2005, 1567], [2005, 1554, 2011, 1567], [2008, 1556, 2024, 1567], [2022, 1556, 2038, 1570], [2035, 1556, 2043, 1567], [2040, 1554, 2046, 1567], [2049, 1556, 2055, 1567], [2067, 1553, 2074, 1567], [2071, 1556, 2095, 1567], [2113, 1569, 2127, 1584], [2151, 1572, 2168, 1584], [2165, 1571, 2171, 1584], [2169, 1572, 2176, 1584], [2176, 1572, 2184, 1583], [2182, 1572, 2189, 1584], [2187, 1572, 2203, 1584], [2203, 1572, 2216, 1584], [2033, 1587, 2039, 1600], [2121, 1605, 2129, 1617], [2136, 1605, 2143, 1616], [2146, 1602, 2156, 1617], [2159, 1605, 2166, 1616], [2169, 1605, 2180, 1620], [2183, 1605, 2189, 1617], [2194, 1605, 2201, 1616], [2198, 1605, 2206, 1617], [2205, 1605, 2216, 1620]]


   
    # 设置距离阈值
    threshold = 40  # 根据需要调整这个值
    # 合并矩形
    print("不一样的小区域的个数为",len(small_box))
    merged_rects = merge_close_rects(small_box, threshold)
    print(merged_rects)

    

绿色是小的矩形框,黄色是同一行融合后的矩形框。


网站公告

今日签到

点亮在社区的每一天
去签到