我想从申请表中删除水平和垂直线,类似于所附的表格。我正在遵循OpenCV的“使用形态学运算提取水平和垂直线”功能中显示的步骤。
代码:
import numpy as np
import sys
import cv2 as cv
src = cv.imread('image.jpg')
if src is None:
print ('Error opening image: ' + src)
# Transform source image to gray if it is not already
if len(src.shape) != 2:
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
else:
gray = src
# Show gray image
cv.imwrite('image_gray.jpg',gray)
# Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
gray = cv.bitwise_not(gray)
bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 15, -2)
# Show binary image
cv.imwrite('image_binary.jpg',bw)
# Create the images that will use to extract the horizontal and vertical lines
horizontal = np.copy(bw)
vertical = np.copy(bw)
# Specify size on horizontal axis
cols = horizontal.shape[1]
horizontal_size = cols / 20
horizontal_size=int(horizontal_size)
# Create structure element for extracting horizontal lines through morphology operations
horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
# Apply morphology operations
horizontal = cv.erode(horizontal, horizontalStructure)
horizontal = cv.dilate(horizontal, horizontalStructure)
# Show extracted horizontal lines
cv.imwrite('image_horizontal.jpg',horizontal)
# Specify size on vertical axis
rows = vertical.shape[0]
verticalsize = rows / 20
verticalsize = int(verticalsize)
# Create structure element for extracting vertical lines through morphology operations
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
# Apply morphology operations
vertical = cv.erode(vertical, verticalStructure)
vertical = cv.dilate(vertical, verticalStructure)
# Show extracted vertical lines
cv.imwrite('image_vertical.jpg',vertical)
# Inverse vertical image
vertical = cv.bitwise_not(vertical)
cv.imwrite('image_vertInverse.jpg',vertical)
# Step 1
edges = cv.adaptiveThreshold(vertical, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 3, -2)
cv.imwrite('image_edges.jpg',edges)
# Step 2
kernel = np.ones((2, 2), np.uint8)
edges = cv.dilate(edges, kernel)
cv.imwrite('image_dilate.jpg',edges)
# Step 3
smooth = np.copy(vertical)
# Step 4
smooth = cv.blur(smooth, (2, 2))
# Step 5
(rows, cols) = np.where(edges != 0)
vertical[rows, cols] = smooth[rows, cols]
# Show final result
cv.imwrite('image_final.jpg',vertical)
这就是我得到的:
原始图片:
已转换为灰色:
二进制图像:
水平线:
垂直线:
边缘:
扩张:
最终结果:
我已经尝试回答已经提出的相关问题,但是不能解决申请表的这种特殊情况。
请帮忙吗?
答案 0 :(得分:0)
好像所有的零件都在那里。 您可以将水平和垂直二进制图像(黑底白字)用作遮罩,并将与该遮罩匹配的所有像素设置为白色。
或者,对结构元素仅使用erode
(需要多次),这将删除所有与该元素匹配的点。