6

我想将垂直线上的表格图像拆分为三个图像,如下所示。是否可以?每列的宽度是可变的。可悲的是,如您所见,左侧垂直线是从标题向下绘制的。

  • 输入图像 (input.png)

在此处输入图像描述

  • 输出图像 (output1.png)

在此处输入图像描述

  • 输出图像 (output2.png)

在此处输入图像描述

  • 输出图像 (output3.png)

在此处输入图像描述


更新 1

可悲的是,如您所见,左侧垂直线是从标题向下绘制的。

这意味着我猜下面的图像 B 更容易分割。但我的情况是A。

在此处输入图像描述


更新 2

我正在尝试按照@HansHirse 给我的方式去做。我的期望是 sub_image_1.png、sub_image_2.png 和 sub_image_3.png 存储在 out 文件夹中。但到目前为止还没有运气。我正在调查它。

https://github.com/zono/ocr/blob/16fd0ec9a2c7d2e26279ec53947fe7fbab9f526d/src/opencv.py

$ git clone https://github.com/zono/ocr.git
$ cd ocr
$ git checkout 16fd0ec9a2c7d2e26279ec53947fe7fbab9f526d
$ docker-compose up -d
$ docker exec -it ocr /bin/bash
$ python3 opencv.py
4

2 回答 2

10

由于您的表格完全对齐,您可以反转图像的二进制阈值,并沿 y 轴计数(白色)像素以检测垂直线:

沿 y 轴计算白色像素

您需要清理山峰,因为较粗的线条可能会出现高原。

这就是我在 Python OpenCV 中的想法:

import cv2
import numpy as np
from skimage import io              # Only needed for web reading images

# Web read image via scikit-image; convert to OpenCV's BGR color ordering
img = cv2.cvtColor(io.imread('https://i.stack.imgur.com/BTqBs.png'), cv2.COLOR_RGB2BGR)

# Inverse binary threshold grayscale version of image
img_thr = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 128, 255, cv2.THRESH_BINARY_INV)[1]

# Count pixels along the y-axis, find peaks
thr_y = 200
y_sum = np.count_nonzero(img_thr, axis=0)
peaks = np.where(y_sum > thr_y)[0]

# Clean peaks
thr_x = 50
temp = np.diff(peaks).squeeze()
idx = np.where(temp > thr_x)[0]
peaks = np.concatenate(([0], peaks[idx+1]), axis=0) + 1

# Save sub-images
for i in np.arange(peaks.shape[0] - 1):
    cv2.imwrite('sub_image_' + str(i) + '.png', img[:, peaks[i]:peaks[i+1]])

我得到以下三个图像:

子图 1

子图 2

子图 3

如您所见,如果实际线条只有 1 像素宽,您可能希望将选择修改 +/- 1 像素。

希望有帮助!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
NumPy:       1.18.1
OpenCV:      4.2.0
----------------------------------------
于 2020-02-11T07:56:29.793 回答
2

OpenCV有一个线检测功能:

您可以过滤通过传递min_theta和返回的行max_theta。对于垂直线,您可以指定可能 :8892分别为边距。

这是从 openCV 文档中获取的经过编辑的示例:

import sys
import math
import cv2 as cv
import numpy as np
def main(argv):

    default_file = 'img.png'
    filename = argv[0] if len(argv) > 0 else default_file
    # Loads an image
    src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE)

    #some preparation of the photo
    dst = cv.Canny(src, 50, 200, None, 3)

    # Copy edges to the images that will display the results in BGR
    cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
    cdstP = np.copy(cdst)

    lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 88, 92) #min and max theta

您可以使用以下代码获取线条的 x、y 坐标并绘制它们。

    if lines is not None:
        for i in range(0, len(lines)):
            rho = lines[i][0][0]
            theta = lines[i][0][2]
            a = math.cos(theta)
            b = math.sin(theta)
            x0 = a * rho
            y0 = b * rho
            pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
            pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
            cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)

或者,您也可以使用HoughLinesP它,因为这允许您指定最小长度,这将有助于您的过滤。此外,这些行作为每端的 x,y 对返回,使其更易于使用。

    linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)

    if linesP is not None:
        for i in range(0, len(linesP)):
            l = linesP[i][0]
            cv.line(cdstP, (l[0], l[2]), (l[2], l[3]), (0,0,255), 3, cv.LINE_AA)

    cv.imshow("Source", src)
    cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
    cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)

    cv.waitKey()
    return 0

文档

要裁剪图像,您可以获取检测到的线的 x 坐标并使用 numpy 切片。

for i in range(0, len(linesP) - 1):
            l = linesP[i][0]
            xcoords = l[0], linesP[i+1][0][0]
            slice = img[:xcoords[0],xcoords[1]]
            cv.imshow('slice', slice)
            cv.waitKey(0)
于 2020-02-11T08:07:06.020 回答