【问题标题】:Removing horizontal and vertical lines in a form by using morphological operations [duplicate]使用形态学操作去除表单中的水平线和垂直线
【发布时间】:2019-05-05 23:18:39
【问题描述】:

我想从申请表中删除水平线和垂直线,类似于附件。我正在按照 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)

这是我得到的:

原图:

转换为灰色:

二进制图像:

水平线:

垂直线:

边缘:

扩张:

最终结果:

我已尝试回答已经提出的相关问题,但这并不能解决申请表的这种特殊情况。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    好像你把所有的零件都放在那里了。 您可以使用水平和垂直二值图像(黑底白字)作为蒙版,并将匹配该蒙版的所有像素设置为白色。

    或者,仅将erode(根据需要)与结构元素一起使用,这将删除与该元素匹配的所有点。

    【讨论】:

    • 很高兴为您提供帮助 :) 您可以使用您得到的结果更新您的问题,因此对其他人会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2012-11-19
    • 2017-07-16
    • 2017-06-30
    相关资源
    最近更新 更多