python - 使用Python中的OpenCV处理图像以减少噪声

标签 python opencv computer-vision

我想对该图像进行某种预处理,以便使文本更具可读性,以便以后可以从图像中读取文本。我对此并不陌生,所以我不知道该怎么办,应该增加对比度或降低噪点等等。基本上,我想删除图像上的这些灰色区域,只保留黑色字母(尽可能清晰)和白色背景。

import cv2

img = cv2.imread('slika1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', img)
cv2.waitKey(0)  


thresh = 200
img = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('filter',img)
cv2.waitKey(0)  
我读取了图像并将阈值应用于图像,但是我需要尝试20种不同的阈值,直到找到能给出结果的阈值。
有没有更好的方法来解决这样的问题?
问题是我可以得到不同大小的灰色区域的不同图片,因此有时我不需要应用任何类型的阈值,有时我需要这样做,因为我认为阈值的解决方案不是那么好。
对于此图像,我的代码运行良好:
enter image description here
但是为此,它给出了可怕的结果:
enter image description here

最佳答案

尝试在Python / OpenCV中进行除法归一化。将输入除以其模糊副本。然后锐化。您可能希望更好地裁剪收据或先遮掩背景。
输入:
enter image description here

import cv2
import numpy as np
import skimage.filters as filters

# read the image
img = cv2.imread('receipt2.jpg')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# blur
smooth = cv2.GaussianBlur(gray, (95,95), 0)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)


# sharpen using unsharp masking
sharp = filters.unsharp_mask(division, radius=1.5, amount=1.5, multichannel=False, preserve_range=False)
sharp = (255*sharp).clip(0,255).astype(np.uint8)


# save results
cv2.imwrite('receipt2_division.png',division)
cv2.imwrite('receipt2_division_sharp.png',sharp)


# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.imshow('sharp', sharp)  
cv2.waitKey(0)
cv2.destroyAllWindows()

除法结果:
enter image description here
锐化结果:
enter image description here

关于python - 使用Python中的OpenCV处理图像以减少噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64037129/

相关文章:

opencv Mat_.at<_tp>。这是什么意思?

c++ - 检查 OpenCV 矩阵是否为浮点分量类型

python - 属性错误 : module 'cv2.cv2' has no attribute 'xfeatures2d' [Opencv 3. 4.3]

c++ - 创建轮廓的边界框

artificial-intelligence - 帮助--LibSVM 的准确率达到 100%?

python - (Python) 如何根据多个正则表达式检查长字符串?

python - 在 Python 2.7 上使用 xpath 提取 href 值

python - 使用多处理查找网络路径

python - 关于如何在单词列表上创建随机森林分类器有什么建议吗?

python - 在 Python 中从文件加载参数