虚线检测CV2 Python

问题描述

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

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

灰色图像输入:

Gray image input

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

thresh = cv2.threshold(num_bloc,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),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,y2],[x1,y2]])

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

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

Output

如您所见,垂直虚线仍在这里。

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

解决方法

您可以使用形态学闭合操作来闭合虚线。

尝试一下:

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,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,y2],[x1,y2]])

        #         cv2.line(table,(lines[i][0][0],lines[i][0][1]),(lines[i][0][2],lines[i][0][3]),(0,255),3,cv2.LINE_AA)
        #         cv2.rectangle(table,(x1,y1-10 ),(x2,y2+10),(36,12),2)
            cv2.fillPoly(num_bloc,[area],color=(33,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,interpolation = cv2.INTER_AREA)
cv2.imshow('threshold',resizedthres)
cv2.imshow('num_bloc',resized)

输出

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...