如何将垂直线的表格图像拆分为三个图像? [英] How to split image of table at vertical lines into three images?

查看:88
本文介绍了如何将垂直线的表格图像拆分为三个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一张桌子的垂直线图像分割成三个图像,如下所示.是否可以?每列的宽度是可变的.可悲的是,如您所见,左侧的垂直线是从标题向下绘制的.

  • 输入图像(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 文件夹中.但到目前为止没有运气.我正在研究它.

您需要清理峰部,因为较粗的线条可能会出现平台期.

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

导入 cv2将 numpy 导入为 npfrom skimage import io # 仅用于网络读取图像# 通过 scikit-image 网络读取图片;转换为 OpenCV 的 BGR 颜色排序img = cv2.cvtColor(io.imread('https://i.stack.imgur.com/BTqBs.png'), cv2.COLOR_RGB2BGR)# 图像的逆二进制阈值灰度版本img_thr = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 128, 255, cv2.THRESH_BINARY_INV)[1]# 沿 y 轴计数像素,找到峰值thr_y = 200y_sum = np.count_nonzero(img_thr,axis=0)峰值 = np.where(y_sum > thr_y)[0]# 清除峰thr_x = 50temp = np.diff(peaks).squeeze()idx = np.where(temp > thr_x)[0]峰值 = np.concatenate(([0], peaks[idx+1]), axis=0) + 1# 保存子图像对于我在 np.arange(peaks.shape[0] - 1):cv2.imwrite('sub_image_' + str(i) + '.png', img[:, peaks[i]:peaks[i+1]])

我得到以下三张图片:

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

希望有帮助!

---------------------------------------系统信息---------------------------平台:Windows-10-10.0.16299-SP0蟒蛇:3.8.1NumPy:1.18.1OpenCV:4.2.0---------------------------

I want to split an image of a table at the vertical lines into three images as shown below. Is it possible? The width of each column is variable. And the sad thing is that the left vertical line is drawn down from the header as you can see.

  • Input image (input.png)

  • Output image (output1.png)

  • Output image (output2.png)

  • Output image (output3.png)


Update 1

And the sad thing is that the left vertical line is drawn down from the header as you can see.

It means I guess the following image B is easier to split. But my case is A.


Update 2

I am trying to do the way @HansHirse gave me. My expectation is sub_image_1.png, sub_image_2.png and sub_image_3.png are stored in the out folder. But no luck so far. I'm looking into it.

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

解决方案

Since your table is perfectly aligned, you can inverse binary threshold your image, and count (white) pixels along the y-axis to detect the vertical lines:

You'll need to clean the peaks, since you might get plateaus for the thicker lines.

That'd be my idea in 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]])

I get the following three images:

As you can see, you might want to modify the selection by +/- 1 pixel, if an actual line is only 1 pixel wide.

Hope that helps!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
NumPy:       1.18.1
OpenCV:      4.2.0
----------------------------------------

这篇关于如何将垂直线的表格图像拆分为三个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆