【问题标题】:Dotted Line Detection CV2 Python虚线检测 CV2 Python
【发布时间】:2020-10-18 17:42:45
【问题描述】:

我正在尝试使用一些变换来去除图像中的虚线和实线,但我只能使用变形变换和霍夫线检测来去除其中的一些。

这里是一个例子:我需要删除虚线和长垂直线,同时不影响其他任何东西。

灰度图像输入:

到目前为止,这是我的代码:

thresh = cv2.threshold(num_bloc, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(num_bloc, [c], -1, (255,255,255), 2)

edges = cv2.Canny(num_bloc, 75, 150)

rho = 1              #Distance resolution of the accumulator in pixels.
theta = np.pi/180    #Angle resolution of the accumulator in radians.
threshold = 300       #Only lines that are greater than threshold will be returned.
minLineLength = 50   #Line segments shorter than that are rejected.
maxLineGap = 10     #Maximum allowed gap between points on the same line to link them
lines = cv2.HoughLinesP(edges, rho = rho, theta = theta, threshold = threshold,
                       minLineLength = minLineLength, maxLineGap = maxLineGap)

if lines is not None:
    if lines.size>0 : 
        a,b,c = lines.shape
        for i in range(a):

            x1=lines[i][0][0]
            y1=lines[i][0][1]-5
            x2=lines[i][0][2]
            y2=lines[i][0][3]+5


            area = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])

        #         cv2.line(table, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
        #         cv2.rectangle(table, (x1,y1-10 ), (x2,y2+10), (36,255,12), 2)
            cv2.fillPoly(num_bloc, [area], color=(255,255,255))

我得到的输出(在输入图像的一个子集上):

如您所见,垂直虚线仍在此处。

关于如何删除所有线条(虚线和实线)、垂直、水平或任何角度的任何建议?

【问题讨论】:

    标签: python line cv2


    【解决方案1】:

    可以使用形态闭合运算来闭合虚线。

    试试这个:

    import cv2
    import numpy as np
    
    num_bloc = cv2.imread('KZpu3.png',1)
    gray = cv2.cvtColor(num_bloc, cv2.COLOR_BGR2GRAY)
    
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    kernel = np.ones((5,5),np.uint8)
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    
    # Remove vertical
    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
    detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
    cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        cv2.drawContours(num_bloc, [c], -1, (33, 227, 253), 2)
    
    edges = cv2.Canny(num_bloc, 75, 150)
    
    rho = 1              #Distance resolution of the accumulator in pixels.
    theta = np.pi/180    #Angle resolution of the accumulator in radians.
    threshold = 300       #Only lines that are greater than threshold will be returned.
    minLineLength = 800   #Line segments shorter than that are rejected.
    maxLineGap = 7    #Maximum allowed gap between points on the same line to link them
    lines = cv2.HoughLinesP(edges, rho = rho, theta = theta, threshold = threshold,
                           minLineLength = minLineLength, maxLineGap = maxLineGap)
    
    if lines is not None:
        if lines.size>0 : 
            a,b,c = lines.shape
            for i in range(a):
    
                x1=lines[i][0][0]
                y1=lines[i][0][1]-5
                x2=lines[i][0][2]
                y2=lines[i][0][3]+5
    
    
                area = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
    
            #         cv2.line(table, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
            #         cv2.rectangle(table, (x1,y1-10 ), (x2,y2+10), (36,255,12), 2)
                cv2.fillPoly(num_bloc, [area], color=(33, 227, 253))
    
    width = int(num_bloc.shape[1] * 0.5)
    height = int(num_bloc.shape[0] * 0.5)
    dim = (width, height)
    # resize image
    resized = cv2.resize(num_bloc, dim, interpolation = cv2.INTER_AREA)
    resizedthres = cv2.resize(thresh, dim, interpolation = cv2.INTER_AREA)
    cv2.imshow('threshold',resizedthres)
    cv2.imshow('num_bloc',resized)
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多