簡體   English   中英

使用 cv2 Python 從圖像中刪除不均勻的垂直和水平線

[英]Remove uneven vertical and horizontal lines from an image using cv2 Python

目標是使用cv2 Python從圖像中去除不均勻的垂直和水平線。

目前,我正在使用這兩個代碼塊來刪除水平線和垂直線。

刪除垂直和水平線

nimg_v=gray.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 40))
detLines = cv2.morphologyEx(nimg_v, cv2.MORPH_OPEN, kernel, iterations=2) #
nimg_v[(detLines !=0)]=0

# Remove horizontal lines
nimg_h=nimg_v.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,30))
detLines = cv2.morphologyEx(nimg_h, cv2.MORPH_OPEN, kernel, iterations=1)
nimg_h[(detLines !=0)]=0

盡管調整Size of the structuring element ,但我仍然無法在保留文本的同時刪除大部分行。

完整的代碼是

import cv2
import numpy as np
from matplotlib import pyplot as plt

dpath='so_images/dummy_image.jpg'

im = cv2.imread(dpath)
# Rough estimation the starting point of text region
y,x=50,700

# Rough estimation where the end of text region
y_end, x_end=1500,1350
white_bg = 255*np.ones_like(im)
white_bg[y:y+(y_end-y), x:x+(x_end-x)] =im[y:y+(y_end-y), x:x+(x_end-x)]
gray=cv2.cvtColor(white_bg, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
dilate = cv2.dilate(gray, kernel, iterations = 2)
idx = (dilate==255)
gray[idx]=0

## Remove vertical and horizontal line

nimg_v=gray.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 40))
detLines = cv2.morphologyEx(nimg_v, cv2.MORPH_OPEN, kernel, iterations=2) #
nimg_v[(detLines !=0)]=0

# Remove horizontal lines
nimg_h=nimg_v.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,30))
detLines = cv2.morphologyEx(nimg_h, cv2.MORPH_OPEN, kernel, iterations=1)
nimg_h[(detLines !=0)]=0



img_sm=nimg_h.copy()
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
dilate = cv2.dilate(img_sm, kernel, iterations = 4)

img_sm[(dilate !=0)]=255


img_cont=img_sm.copy()
schunk_small=800
schunk_big=50000
cnts = cv2.findContours(img_cont, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if (area < schunk_small) | (area>schunk_big):
        cv2.drawContours(img_cont, [c], -1, (0, 0, 0), -1)

plt.imshow(img_cont)
plt.show()

此外,我還嘗試使用HoughLinesP並根據slope <1進行過濾,如下所示。 但是,我仍然無法刪除這些線條。

edges = cv2.Laplacian(img_cont,cv2.CV_8UC1) # Laplacian Edge Detection

lines = cv2.HoughLinesP(
    edges, # Input edge image
    1, # Distance resolution in pixels
    np.pi/180, # Angle resolution in radians
    threshold=100, # Min number of votes for valid line
    minLineLength=5, # Min allowed length of line
    maxLineGap=10 # Max allowed gap between line for joining them
)

lines_list = []

for points in lines:
    x1,y1,x2,y2=points[0]
    slope = ((y2-y1) / (x2-x1)) if (x2-x1) != 0 else np.inf
    if slope <= 1:
        cv2.line(img_cont,(x1,y1),(x2,y2),(255,255,255),1)

通過使用自定義內核對帶有低通濾波器的圖像進行卷積,可以去除圖像中的細垂直和水平線。 由於字母數字字符的粗細遠高於那些行,它可以過濾這些行。 通過添加下面的代碼給了我這些結果 -

kernel = np.full(fill_value=1/(18*18*250), shape=(18,18), dtype=float)
img_rst = cv2.filter2D(img_cont,-1,kernel)
plt.imshow(img_rst)
plt.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM