从表格列区域删除粗体并扩展表格线

问题描述 投票:0回答:1

我需要有关下图的帮助。图像有 2 个表格,我试图删除列区域中的深色背景,并将其设置为白色背景和黑色文本,这将与其他区域保持一致。即我想要所有区域的白色背景和黑色文本。我还想延长表 2 上的垂直线。请参阅图片以获取详细说明:我下面的解决方案似乎没有做到这一点,而且我是计算机视觉的新手。 请阅读图像以获取预期结果。我们将非常感谢您的帮助。

# Load the image
image = cv2.imread('./help.png')

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Threshold the grayscale image to create a binary image
_, binary_image = cv2.threshold(gray_image, 128, 255, cv2.THRESH_BINARY)

# Invert the binary image
inverted_image = cv2.bitwise_not(binary_image)

# Create an all-white image
white_background = 255 * np.ones_like(image)

# Set the text pixels to black on the white background
result_image = cv2.bitwise_or(white_background, white_background, mask=inverted_image)

# Find contours in the processed image
contours, _ = cv2.findContours(inverted_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours on a copy of the original image (result_image)
contour_image = result_image.copy()
cv2.drawContours(contour_image, contours, -1, (0, 0, 0), 2)

# Display the image with contours
plt.imshow(contour_image)
plt.show()

# Optionally, save the image with contours
cv2.imwrite('./images/contour_image.png', contour_image)
python opencv image-processing computer-vision opencv3.0
1个回答
0
投票

我使用了一些基本的图像处理技术,它确实有效。困难的部分是延长线路。首先,我可以利用以下事实:这些线条实际上存在,但在深色背景中不可见(如第一个表中),但显然情况并非如此。我正在考虑线路检测,但我想不出一种适用于所有情况的算法。

无论如何,这是我不完整的代码,一旦我能够理解扩展线,我就会更新答案。

步骤很简单,

  1. 使用关闭内核删除文本
  2. 使用开口内核去除黑色突出显示中的孔(因为黑色区域中的文本)
  3. 反转 2 结果的颜色。

代码:

import cv2 as cv
import numpy as np

image = cv.imread('3dL39.png', cv.IMREAD_GRAYSCALE)

_, mask = cv.threshold(image, 200, 255, cv.THRESH_BINARY)

kernel = np.ones((5, 5), np.uint8)
closing = cv.dilate(mask, kernel, iterations=1)

kernel = np.ones((9, 9), np.uint8)
openning = cv.morphologyEx(closing, cv.MORPH_OPEN, kernel, iterations=3)

image[openning == 0] = 255 - image[openning == 0]

cv.imwrite('Final.png', image)

结果:

© www.soinside.com 2019 - 2024. All rights reserved.