试图为扫描的文档制作神奇的滤色器,但没有得到完美的输出

2024-04-29 10:48:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为扫描的文档制作一个神奇的滤色器,但没有得到想要的输出。在某些文档中,文本在某些文档中失真,图像颜色丢失。在某些文档中,文本变得太厚。 我使用拉普拉斯高斯边缘增强,然后阈值,最后模糊图像,以获得自然的外观

import cv2
import numpy as np

img = cv2.imread('example_output/sample16.jpeg')
original = img.copy()
kernel_sharpen_3 = np.array([[-1, -1, -1, -1, -1],
                         [-1, 2, 2, 2, -1],
                         [-1, 2, 8, 2, -1],
                         [-1, 2, 2, 2, -1],
                         [-1, -1, -1, -1, -1]]) / 8
img = cv2.filter2D(img, -1, kernel_sharpen_3)
ret, threshold = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
blur = cv2.GaussianBlur(threshold, (5, 5), 0)
stack_horizontal = np.concatenate((original, img), axis=1)
stack_horizontal2 = np.concatenate((threshold, blur), axis=1)
stack_vertical = np.concatenate((stack_horizontal, stack_horizontal2), axis=0)

cv2.imshow('Images', cv2.resize(stack_vertical, (700, 1000)))
cv2.imwrite("magic.jpg", threshold)
cv2.waitKey()
cv2.destroyAllWindows()

输入图像:

Input Image

我的结果:

My Result

期望输出:

Desired Output


Tags: 文档图像文本importimgthresholdstacknp