Ejemplo n.º 1
0
def lineDetect(img, mode=0, threshold=-1, minLineLength=0, maxLineGap=0):
    img = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
    lines = []
    if mode == 0:  # 霍夫直线检测
        # 参数 rho:累加器距离分辨率,  theta:累加器角度分辨率   threshold:投票阈值,  minLineLength:直线的最小长度  maxLineGap:直线合并最大断开距离
        datas = cv2.HoughLinesP(img, rho=1, theta=np.pi / 360., threshold=int(threshold), minLineLength=minLineLength,
                                maxLineGap=maxLineGap)
        if datas is None:
            return lines
        for d in datas.reshape(-1, 4):
            line = GLine().initXY(*d)
            if line.getLen() <= 0: continue
            lines.append(line)
    elif mode == 1:  # 直线检测器检测
        # 直线检测器检测 cv2.createLineSegmentDetector
        # 参数1 refine(LSD_REFINE_STD,): 调优策略, cv2.LSD_REFINE_NONE:无调优策略, cv2.LSD_REFINE_STD:标准调优策略,将弧分解成多个小线段, cv2.LSD_REFINE_ADV:使用NFA指标评价检测直线,调整参数近一步调优检测直线
        # 参数2 scale(0.8): 缩放图像比例   参数3 sigma_scale(0.6): 高斯模糊核sigma=sigma_scale/scale   参数4 quant(2.0):梯度量化误差
        # 参数5 ang_th(22.5):直线段角度容差  参数6 log_eps(0): NFA检测阈值  参数7 density_th(0.7):直线段密度阈值   参数8 n_bins(1024):梯度归一化数量
        lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD)
        datas, width, prec, nfa = lsd.detect(img)
        if datas is None and width is None and prec is None and nfa is None: print("检测失败,未能检测到直线");return lines
        datas = datas.reshape(-1, 4)
        width = width.reshape(-1).tolist()
        prec = prec.reshape(-1).tolist()
        for i in range(len(datas)):
            line = GLine().initXY(*datas[i])
            if line.getLen() <= 0: continue
            line.width, line.prec = width[i], prec[i]
            lines.append(line)
    else:
        pass
    return lines
Ejemplo n.º 2
0
 def disLine0(a, b):
     if (a == b).all(): return 0
     linea = GLine().initXY(a[0], a[1], a[2], a[3])
     lineb = GLine().initXY(b[0], b[1], b[2], b[3])
     longline, shortline = (linea, lineb) if linea.getLen() > lineb.getLen() else (lineb, linea)
     pts = [shortline.pt1, shortline.pt2]
     dis = min([longline.distance(pt, mode=1) for pt in pts])
     angle = longline.angle(shortline)
     if angle > minAngle and dis < minDis: dis = minDis  # 当超过角度, 距离至少位mindis(使其不符号条件)
     return dis * (math.sin(math.radians(angle)) + 1)