我想对该图像进行某种预处理,以便使文本更具可读性,以便以后可以从图像中读取文本。我对此并不陌生,所以我不知道该怎么办,应该增加对比度或降低噪点等等。基本上,我想删除图像上的这些灰色区域,只保留黑色字母(尽可能清晰)和白色背景。
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种不同的阈值,直到找到能给出结果的阈值。有没有更好的方法来解决这样的问题?
问题是我可以得到不同大小的灰色区域的不同图片,因此有时我不需要应用任何类型的阈值,有时我需要这样做,因为我认为阈值的解决方案不是那么好。
对于此图像,我的代码运行良好:
但是为此,它给出了可怕的结果:
最佳答案
尝试在Python / OpenCV中进行除法归一化。将输入除以其模糊副本。然后锐化。您可能希望更好地裁剪收据或先遮掩背景。
输入:
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()
除法结果:
锐化结果:
关于python - 使用Python中的OpenCV处理图像以减少噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64037129/