虚线检测CV2 Python
我正在尝试使用一些变换来去除图像中的虚线和实线,但我只能使用变形变换和hough线检测来去除其中的一些 下面是一个例子:我需要删除虚线和长垂直线,同时不影响其他任何内容 灰度图像输入: 以下是我目前的代码:虚线检测CV2 Python,python,line,cv2,Python,Line,Cv2,我正在尝试使用一些变换来去除图像中的虚线和实线,但我只能使用变形变换和hough线检测来去除其中的一些 下面是一个例子:我需要删除虚线和长垂直线,同时不影响其他任何内容 灰度图像输入: 以下是我目前的代码: thresh = cv2.threshold(num_bloc, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Remove vertical vertical_kernel = cv2.getStructuringEleme
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))
以及我得到的输出(在输入图像的子集上):
如你所见,垂直虚线仍在这里
关于如何删除所有线(虚线和全线)、垂直线、水平线或任何角度的建议?您可以使用形态闭合操作来闭合虚线 试试这个:
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)
输出