rektdect.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:kami-solver 作者: erasche 项目源码 文件源码
def griddect(img, debug=False):
    gray = cv2.cvtColor(img,  cv2.COLOR_RGB2GRAY)
    edges = cv2.Canny(gray, 50, 150, apertureSize = 3)

    lines = cv2.HoughLines(edges, 2, np.pi/100, 320)
    v = []
    h = []
    for rho, theta in lines[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))

        if int(a) == 0:
            h.append(y1)
        else:
            v.append(x1)

        if debug:
            cv2.line(img, (x1, y1), (x2, y2), (50, 50, 255), 2)

    height, width, channels = img.shape
    DEC = 0

    def dist(numArr):
        for i in range(len(numArr) - 1):
            x = numArr[i + 1] - numArr[i]
            if x > 5:
                yield x

    v_points = np.unique(np.round(np.array(sorted(v)), decimals=DEC))
    v_dist = list(dist(np.sort(v_points)))
    v_point_median = np.median(v_dist)

    h_points = np.unique(np.round(np.array(sorted(h)), decimals=DEC))
    h_dist = list(dist(np.sort(h_points)))
    h_point_median = np.median(h_dist)

    return v_point_median, h_point_median
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号