“横平竖直”进行连线+将相邻框进行合并

一.横平竖直”进行连线

解法1.将一些坐标点按照x相等,y相等连起来

解法1.根据 x或y总有一个相等的,用np.sum来找出和为1的点,然后在连起来,存在重复连线的问题.

import numpy as np
    coord = np.array([[10, 60],
                      [10, 20],
                      [20, 20],
                      [40, 40],
                      [40, 60],
                      [20, 40]])
    img = np.zeros((100, 100))
    h, w = coord.shape

    for i in range(h):
        index = np.sum(np.array(coord[i]).reshape(-1, 2) == coord, axis=-1)
        y = np.where(index == 1)

        for point in coord[y]:
            cv2.line(img, (coord[i][0], coord[i][1]), (point[0], point[1]), color=(255,255,255), thickness=3)
    cv2.imshow('img', img)
    cv2.waitKey(0)

解法2.先连x相等,在连y相等的

def coord_sort_ind(x,y):
    #按照x,y来排序
    ind = np.lexsort((y.tolist(), x.tolist())) # Sort by x, then by y
    return ind
def simple_line_():
    import numpy as np
    coord = np.array([[10, 60],
                      [10, 20],
                      [20, 20],
                      [40, 40],
                      [40, 60],
                      [20, 40]])
    # 按照先X后Y排的序
    ind = coord_sort_ind(coord[:, 0], coord[:, 1])
    coord_X=coord[ind]
    print(coord_X)

    # # 按照先Y后X排的序
    ind = coord_sort_ind(coord[:, 1], coord[:, 0])
    print(ind)
    coord_Y = coord[ind]
    print(coord_Y)

    img = np.zeros((100, 100))
    h, w = coord.shape
    # x想等的连接
    for i in range(0, h, 2):
        cv2.line(img, (coord_X[i][0], coord_X[i][1]), (coord_X[i + 1][0], coord_X[i + 1][1]), color=(255, 255, 255),
                 thickness=3)
        # y想等的连接
        cv2.line(img, (coord_Y[i][0], coord_Y[i][1]), (coord_Y[i + 1][0], coord_Y[i + 1][1]), color=(255, 255, 255),
                 thickness=3)

    cv2.imshow('img', img)
    cv2.waitKey(0)

二.将相邻框进行合并

目前解法是将两个相邻的框合并,得到大框在继续合并,算法还需要优化成一次性合并成功

import numpy as np
import cv2
def get_cluster(points,x_dis,y1_dis):
    points = points[points[:, 0].argsort()]
    x1,y1,x2,y2=points[:,0],points[:,1],points[:,-2],points[:,-1]
    #只对竖值框进行合并操作
    points=points[np.where(x2-x1<y2-y1)[0]]
    # points=points[:13]
    # print('points:',points)
    # # debug to show
    # for i,point in enumerate(points):
    #         font = cv2.FONT_HERSHEY_SIMPLEX
    #         cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)
    #         cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)
    # cv2.imwrite('img_rect.jpg', img)

    # print(points.shape[0])
    cluster = []
    while points.shape[0]:
        temp=[]
        # print('points:',points)
        #x1-x2<
        index_x = np.where(abs(points[0][-2]-points[:, 0]) < x_dis)[0]
        # print('index_x:',index_x)
        # if len(index_x):
        #     #y1-y1<
        index_y1 = np.where(abs(points[0][1]-points[:, 1]) < y1_dis)[0]
        # print('index_y: ',index_y1)
        #y2-y2<h
        index_y2 = np.where(abs(points[0][-1]-points[:, -1]) < (points[:, -1]-points[:, 1]))[0]
        # print('index_y2:',index_y2)
        # common_index=[i for i in index_x  if i in index_y]
        common_index = list(set(index_x) & set(index_y1) & set(index_y2))
        # print('common_index:',common_index)
        # print('common_index:',common_index)
        if len(common_index):
            temp.append(points[0])
            temp.extend(points[common_index])
            points = np.delete(points, obj=common_index+[0], axis=0)
        else:
            temp.append(points[0])
            points=points[1:]
        cluster.append(temp)

    # # print(points)
    # print('cluster:',cluster)
    # print(len(cluster))
    # for i in range(len(cluster)):
    #     every_cluster=np.array(cluster[i]).reshape(-1,4)
    #     print('every_cluster:',every_cluster)
    #     for point in every_cluster:
    #         font = cv2.FONT_HERSHEY_SIMPLEX
    #         cv2.putText(img, str(i), (point[0],point[1]), font, 2, (255, 255, 255), 2)
    #         cv2.rectangle(img,(point[0],point[1]),(point[-2],point[-1]),color=(255,255,255),thickness=3)
    # cv2.imwrite('img_rect_clu.jpg',img)
    return cluster
def fix_bbox_point(cluster):
    points=[]
    for i,evry_cluster in enumerate(cluster):
        every_cluster = np.array(cluster[i]).reshape(-1, 4)
        x1,y1,x2,y2=np.min(every_cluster[:,0]),np.min(every_cluster[:,1])\
        ,np.max(every_cluster[:,-2]),np.max(every_cluster[:,-1])
        points.append([x1,y1,x2,y2])
    return np.array(points)

def union_bboxs(points):
    cluster_first = get_cluster(points, x_dis=50, y1_dis=300)
    # print('===cluster=====')
    # print(len(cluster_first))
    first_bboxs = fix_bbox_point(cluster_first)

    cluster_second = get_cluster(first_bboxs, x_dis=50, y1_dis=300)
    # print('===cluster=====')
    # print(len(cluster_second))
    second_bboxs = fix_bbox_point(cluster_second)

    cluster_third = get_cluster(second_bboxs, x_dis=50, y1_dis=400)
    # print('===cluster=====')
    # print(len(cluster_third))
    third_bboxs = fix_bbox_point(cluster_third)
    return third_bboxs

if __name__ == '__main__':
    # points = np.array([[1264, 1480, 1346, 1790],
    #                    [2278, 1873, 2380, 2542],
    #                    [3209, 2279, 3286, 2599],
    #                    [2195, 3468, 2277, 3772],
    #                    [844, 1087, 924, 1399],
    #                    [92, 876, 181, 1327],
    #                    [951, 306, 1033, 596],
    #                    [1652, 299, 1729, 604],
    #                    [2196, 1957, 2270, 2535],
    #                    [916, 4251, 994, 4563],
    #                    [1131, 1915, 1214, 2505],
    #                    [1042, 265, 1116, 532],
    #                    [2124, 2036, 2195, 2603],
    #                    [934, 1054, 1003, 1258],
    #                    [1522, 3561, 1585, 4086],
    #                    [999, 4220, 1068, 4422],
    #                    [2404, 586, 2516, 1209],
    #                    [1452, 3618, 1528, 4151],
    #                    [1354, 1449, 1425, 1655],
    #                    [1583, 3532, 1683, 4109],
    #                    [1207, 1525, 1256, 1792],
    #                    [2144, 3511, 2191, 3779],
    #                    [3162, 2319, 3205, 2549],
    #                    [787, 1124, 837, 1395],
    #                    [896, 339, 944, 606],
    #                    [1741, 260, 1813, 467],
    #                    [219, 3230, 330, 3785],
    #                    [1424, 2675, 1506, 2818],
    #                    [2528, 538, 2714, 1709],
    #                    [2287, 664, 2394, 1596],
    #                    [231, 1703, 428, 2598],
    #                    [287, 248, 473, 1638],
    #                    [186, 280, 265, 1025],
    #                    [1670, 3480, 1756, 3852],
    #                    [1754, 2277, 1836, 2431],
    #                    [3284, 2241, 3360, 2455],
    #                    [2823, 1662, 3163, 1730],
    #                    [2897, 2852, 3285, 2946],
    #                    [2972, 2700, 3049, 2790]])

    points=np.array([[700,1888,  789, 2199],
                     [1918, 1487, 2000, 1798],
                     [1045,  683, 1129,  995],
                     [2867,  279, 2975,  958],
                     [1929, 3088, 2012, 3393],
                     [1006,  272, 1087,  583],
                     [2476, 3089, 2560, 3396],
                     [ 820, 3094,  904, 3406],
                     [2532, 3499, 2616, 3810],
                     [1335, 1080, 1411, 1378],
                     [2204, 2283, 2288, 2587],
                     [1138,  644, 1213,  975],
                     [1611, 1884, 1695, 2196],
                     [2823, 2283, 2906, 2603],
                     [ 773, 2286,  853, 2606],
                     [1562,  262, 1642,  585],
                     [2458, 1883, 2540, 2196],
                     [1749, 3491, 1831, 3803],
                     [ 634, 1266,  715, 1785],
                     [1646,  236, 1722,  515],
                     [ 988, 3889, 1060, 4221],
                     [ 725, 1167,  836, 1743],
                     [2592,  455, 2703, 1365],
                     [2721,  375, 2853, 1289],
                     [ 846, 1104,  938, 1481],
                     [ 857, 2255,  935, 2583],
                     [1419, 1043, 1494, 1316],
                     [1707, 2310, 1814, 3001],
                     [1327, 3046, 1405, 3387],
                     [1244, 3081, 1327, 3404],
                     [1814, 2264, 1903, 2656],
                     [2543, 1844, 2617, 2053],
                     [2845, 3894, 2918, 4215],
                     [2147, 2330, 2197, 2559],
                     [2402, 1922, 2452, 2198],
                     [1058, 3860, 1122, 4065],
                     [3308, 1779, 3396, 2183],
                     [2293, 2248, 2370, 2454],
                     [1857, 1531, 1908, 1760],
                     [1697, 3535, 1745, 3803],
                     [1189, 3133, 1236, 3367],
                     [2419, 3135, 2468, 3360],
                     [1873, 3124, 1923, 3400],
                     [ 988,  719, 1038,  995],
                     [2480, 3539, 2529, 3809],
                     [2017, 3057, 2083, 3278],
                     [2095, 2368, 2138, 2598],
                     [ 766, 3135,  815, 3409],
                     [1559, 1964, 1607, 2160],
                     [1504, 1966, 1550, 2202],
                     [2913, 2248, 2996, 2455],
                     [ 766, 4318,  838, 4580],
                     [1802, 1571, 1848, 1797],
                     [ 374,  801,  457, 1032],
                     [ 637, 1926,  690, 2164],
                     [1636, 2441, 1708, 2998],
                     [ 964,  353, 1012,  549],
                     [3243,  290, 3389, 1431],
                     [1869, 3963, 1952, 4206],
                     [3131, 2120, 3215, 2800],
                     [2858, 4314, 2934, 4593],
                     [2802, 3939, 2844, 4189],
                     [ 484,  716,  570,  955],
                     [2913, 3860, 2988, 4126],
                     [ 330,  645,  566,  717]])


    # print(points[30:33])
    # points = points[12:21]
    img = np.zeros((5000, 4000))
    cluster_first=get_cluster(points,x_dis=50,y1_dis=300)
    print('===first cluster=====')
    print(len(cluster_first))
    first_bboxs=fix_bbox_point(cluster_first)
    print('===second cluster===')
    cluster_second = get_cluster(first_bboxs, x_dis=50, y1_dis=400)
    print(len(cluster_second))
    second_bboxs = fix_bbox_point(cluster_second)
    # #debug
    # for i,point in enumerate(first_bboxs):
    #     font = cv2.FONT_HERSHEY_SIMPLEX
    #     cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)
    #     cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)
    # cv2.imwrite('img_rect_first_bbx.jpg', img)
    #
    #
    # # debug
    # for i, point in enumerate(second_bboxs):
    #     font = cv2.FONT_HERSHEY_SIMPLEX
    #     cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)
    #     cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)
    # cv2.imwrite('img_rect_second_bbx.jpg', img)

    cluster_third = get_cluster(second_bboxs, x_dis=50, y1_dis=400)
    print('===cluster=====')
    print(len(cluster_third))
    third_bboxs = fix_bbox_point(cluster_third)
    # debug
    for i, point in enumerate(third_bboxs):
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)
        cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)
    cv2.imwrite('img_rect_third_bbx.jpg', img)

合并前: 

合并后:

要将手绘的迷宫图变得方方正正线条平竖直,可以通过以下步骤实现: 1. 读取迷宫图像,并将其转换为灰度图像。 2. 对灰度图像进行二值化处理,将其转换为黑白图像。 3. 对黑白图像进行膨胀操作,使得迷宫的边界更加明显。 4. 对膨胀后的图像进行边缘检测,找到迷宫的边界。 5. 对边界进行直线拟合,得到迷宫的直线边界。 6. 根据直线边界将迷宫图像进行扭曲矫正,使其变得方方正正线条平竖直。 代码示例: ```python import cv2 import numpy as np # 读取迷宫图像并转换为灰度图像 img = cv2.imread('maze.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将灰度图像进行二值化处理 thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # 对二值化后的图像进行膨胀操作 kernel = np.ones((5, 5), np.uint8) dilation = cv2.dilate(thresh, kernel, iterations=5) # 对膨胀后的图像进行边缘检测 edges = cv2.Canny(dilation, 100, 200) # 对边缘进行直线拟合 lines = cv2.HoughLines(edges, 1, np.pi/180, 200) for line in lines: rho, theta = line[0] a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2) # 对图像进行扭曲矫正 rows, cols = img.shape[:2] src_pts = np.float32([[0, 0], [cols-1, 0], [0, rows-1], [cols-1, rows-1]]) dst_pts = np.float32([[0, 0], [cols-1, 0], [cols*0.1, rows-1], [cols*0.9, rows-1]]) M = cv2.getPerspectiveTransform(src_pts, dst_pts) warped = cv2.warpPerspective(img, M, (cols, rows)) # 显示结果 cv2.imshow('Original', img) cv2.imshow('Warped', warped) cv2.waitKey(0) cv2.destroyAllWindows() ``` 注:以上代码仅供参考,具体实现应根据实际情况进行调整。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值