程序
pythonimport numpy as np
import cv2
def replaceZeroes(data):
min_nonzero = min(data[np.nonzero(data)])
data[data == 0] = min_nonzero
return data
def SSR(src_img, size):
L_blur = cv2.GaussianBlur(src_img, (size, size), 0)
img = replaceZeroes(src_img)
L_blur = replaceZeroes(L_blur)
dst_Img = cv2.log(img / 255.0)
dst_Lblur = cv2.log(L_blur / 255.0)
dst_IxL = cv2.multiply(dst_Img, dst_Lblur)
log_R = cv2.subtract(dst_Img, dst_IxL)
dst_R = cv2.normalize(log_R, None, 0, 255, cv2.NORM_MINMAX)
log_uint8 = cv2.convertScaleAbs(dst_R)
return log_uint8
# 使用照片修复的办法来去掉图片中的文字
def remove_text1(path):
size = 3
src_img = cv2.imread(path)
# # 膨胀图片
# src_img = cv2.dilate(src_img, kernel=np.ones((3, 3), dtype=np.uint8), iterations=2)
# 腐蚀图片
# src_img = cv2.erode(src_img, kernel=np.ones((3, 3), dtype=np.uint8), iterations=2)
b_gray, g_gray, r_gray = cv2.split(src_img)
b_gray = SSR(b_gray, size)
g_gray = SSR(g_gray, size)
r_gray = SSR(r_gray, size)
ssrimg = cv2.merge([b_gray, g_gray, r_gray])
canny_img = cv2.Canny(ssrimg, 200, 150)
k = np.ones((3, 3), np.uint8)
mask = cv2.morphologyEx(canny_img, cv2.MORPH_CLOSE, k) # 闭运算
# 对mask进行膨胀
# mask = cv2.dilate(mask, kernel=np.ones((3, 3), dtype=np.uint8), iterations=6)
dst = cv2.inpaint(src_img, mask, 3, cv2.INPAINT_TELEA)
# dst = cv2.inpaint(dst, mask, 3, cv2.INPAINT_TELEA)
# 膨胀
dst = cv2.dilate(dst, kernel=np.ones((3, 3), dtype=np.uint8), iterations=5)
return dst
# 使用mask直接填充颜色的办法来去掉图片中的文字
def remove_text2(path):
size = 3
src_img = cv2.imread(path)
# 腐蚀图片
# src_img = cv2.erode(src_img, kernel=np.ones((3, 3), dtype=np.uint8), iterations=2)
b_gray, g_gray, r_gray = cv2.split(src_img)
b_gray = SSR(b_gray, size)
g_gray = SSR(g_gray, size)
r_gray = SSR(r_gray, size)
ssrimg = cv2.merge([b_gray, g_gray, r_gray])
canny_img = cv2.Canny(ssrimg, 200, 150)
k = np.ones((3, 3), np.uint8)
mask = cv2.morphologyEx(canny_img, cv2.MORPH_CLOSE, k) # 闭运算
# 保存mask为图片
cv2.imwrite("mask.png", mask)
# mask等于1的区域,用白色填充
# dst = cv2.bitwise_and(src_img, src_img, mask=mask)
src_img[mask == 255] = (255, 255, 255)
# 膨胀
src_img = cv2.dilate(src_img, kernel=np.ones((3, 3), dtype=np.uint8), iterations=5)
return src_img
# 移除背景色,增强文字
def remove_ground(path):
size = 3
src_img = cv2.imread(path)
b_gray, g_gray, r_gray = cv2.split(src_img)
b_gray = SSR(b_gray, size)
g_gray = SSR(g_gray, size)
r_gray = SSR(r_gray, size)
ssrimg = cv2.merge([b_gray, g_gray, r_gray])
canny_img = cv2.Canny(ssrimg, 200, 150)
k = np.ones((3, 3), np.uint8)
mask = cv2.morphologyEx(canny_img, cv2.MORPH_CLOSE, k) # 闭运算
# mask等于1的区域,用白色填充
dst = cv2.bitwise_and(src_img, src_img, mask=mask)
dst[mask == 0] = (255, 255, 255)
return dst
if __name__ == '__main__':
imgPath = "papertext.jpg"
dst = remove_text2(imgPath)
cv2.imwrite("result.jpg", dst)
原图:
找到文字部分:
去除文字:
保留清晰文字:
1、
Self-Supervised Text Erasing with Controllable Image Synthesis 即STE : 论文https://arxiv.org/abs/2204.12743。
2、
EnsNet : https://blog.csdn.net/u013554402/article/details/104494261。
3、
https://paperswithcode.com/paper/mtrnet-one-stage-mask-based-scene-text-eraser。
4、
http://www.cjig.cn/html/jig/2022/4/20220420.htm
SickZil-Machine用于去除文本: https://github.com/KUR-creative/SickZil-Machine
lama-cleaner技术: https://github.com/Sanster/lama-cleaner
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!