python - 如何去除文本图像上的黑噪声?
问题描述
我正在使用此代码对图像进行伽玛校正,当我使文本足够暗时(所有背景都是白色),我如何获得伽玛校正图像附带的黑噪声?
伽玛校正代码:
import cv2
import numpy as np
def adjust_gamma(image, gamma=1.0):
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
x = 'page-1.png' #location of the image
original = cv2.imread(x, 1)
#cv2.imshow('original', original)
print("Choose a value between 0-1 for gamma correction:")
print("[Choose 0.0 to exit]")
def apply_gamma():
while True:
gamma = float(input("\nWhat value would you like the gamma correction to be? "))
if 1 > gamma > 0:
adjusted = adjust_gamma(original, gamma=gamma)
cv2.putText(adjusted, "g={}".format(gamma), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
cv2.imshow('Original', original)
cv2.imshow("Gamma corrected image", adjusted)
cv2.waitKey(0)
cv2.destroyAllWindows()
print('\n[press "y" to continue. Press "n" to readjust"]')
while True:
gc = input('Are you satisfied with the image? ')
if gc == 'n':
apply_gamma()
if gc == 'y':
print('\nDenoise is next')
cv2.imwrite('text1_gc.png', adjusted)
break
else:
print('\nInvalid input')
gc = True
break
apply_gamma()
噪音:
解决方案
您只需要使用一个threshold
值对图像进行二值化。为此,您可以使用cv2.threshold
方法。在这里threshold value = 85
。
使用此代码:
import cv2
img = cv2.imread('test.png', 0)
threshold = 85
cv2.threshold(img,threshold,255,cv2.THRESH_BINARY,img)
cv2.imshow('test',img)
cv2.waitKey(0)
输出:
推荐阅读
- extjs - 动态加载 javascript 文件
- python-3.x - 将 pandas.DataFrame 行中的每个值替换为该值的计数
- angular - 带有服务提供的 Angular 延迟加载模块在此模块中
- postgresql - 在 Kotlin 中翻译 sql 查询不起作用
- python - cx_Oracle 与 Pyinstaller 的连接导出到 exe
- c# - 我应该将此方法的查询转换为存储过程吗?
- vim - 如何将点运算符的内容复制到寄存器中?
- r - 创建具有固定增量的可变长度的多个序列?
- themes - 更改 Tumblr 照片集中的 onclick URL
- regex - Logstash Grok Regex:获取每个块中的每一行