处理黑色手写字迹的擦除方法
当试卷上的手写字迹是黑色的,与印刷文字颜色相同时,简单的颜色分割方法就不再适用了。这种情况下,我们需要采用更复杂的技术来区分和擦除手写字迹。以下是几种可行的方法:
1. 基于深度学习的图像修复
这是目前处理黑色手写字迹最有效的方法之一:
import cv2
import numpy as np
import torch
from lama_cleaner.model_manager import ModelManager
from lama_cleaner.schema import Config
def remove_black_handwriting_with_lama(image_path, mask_path, output_path):
"""
使用LaMa深度学习模型擦除黑色手写字迹
参数:
image_path: 原始图像路径
mask_path: 手写字迹掩码路径(需手动或半自动创建)
output_path: 输出图像路径
"""
# 读取图像和掩码
img = cv2.imread(image_path)
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
# 初始化LaMa模型
model = ModelManager(name="lama", device="cuda" if torch.cuda.is_available() else "cpu")
# 设置配置
config = Config(
ldm_steps=25,
ldm_sampler="plms",
hd_strategy="Original",
hd_strategy_crop_margin=32,
hd_strategy_crop_trigger_size=2000,
hd_strategy_resize_limit=2048,
)
# 执行修复
result = model(img, mask, config)
# 保存结果
cv2.imwrite(output_path, result)
print("处理完成,结果已保存到", output_path) 2. 结合OCR的半自动方法
这种方法尝试保护印刷文字,只修复其他区域:
import cv2
import numpy as np
import pytesseract
from PIL import Image
def remove_handwriting_with_ocr(image_path, output_path):
"""
使用OCR识别印刷文字,然后尝试擦除非印刷文字区域
"""
# 读取图像
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化处理
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 使用OCR识别印刷文字区域
# 注意:需要安装Tesseract OCR并设置路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Windows路径示例
# 获取OCR识别结果(包含边界框信息)
ocr_results = pytesseract.image_to_data(Image.fromarray(gray), output_type=pytesseract.Output.DICT)
# 创建掩码,初始全为255(白色)
mask = np.ones_like(gray) * 255
# 在掩码上标记OCR识别的文字区域为0(黑色)
for i in range(len(ocr_results['text'])):
# 只处理置信度高且非空的文本
if int(ocr_results['conf'][i]) > 60 and ocr_results['text'][i].strip() != '':
x = ocr_results['left'][i]
y = ocr_results['top'][i]
w = ocr_results['width'][i]
h = ocr_results['height'][i]
# 在掩码上将印刷文字区域标记为黑色(保护区域)
cv2.rectangle(mask, (x, y), (x + w, y + h), 0, -1)
# 对二值化图像进行形态学操作,提取可能的手写区域
kernel = np.ones((3, 3), np.uint8)
dilated = cv2.dilate(binary, kernel, iterations=2)
# 只保留非印刷文字区域的二值化结果(可能的手写区域)
handwriting_mask = cv2.bitwise_and(dilated, dilated, mask=mask)
# 进行图像修复
result = cv2.inpaint(img, handwriting_mask, 5, cv2.INPAINT_TELEA)
# 保存结果
cv2.imwrite(output_path, result)
print("处理完成,结果已保存到", output_path) 3. 交互式手动标记工具
当自动方法效果不佳时,可以使用交互式工具手动标记字迹区域:
import cv2
import numpy as np
def interactive_handwriting_removal():
"""
交互式手动标记手写字迹并擦除
使用方法:
- 鼠标左键拖动标记手写字迹
- 按'c'清除所有标记
- 按'r'执行修复
- 按's'保存结果
- 按'q'退出
"""
# 全局变量
drawing = False
ix, iy = -1, -1
# 鼠标回调函数
def draw_mask(event, x, y, flags, param):
nonlocal drawing, ix, iy
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.line(mask, (ix, iy), (x, y), 255, 5)
cv2.line(img_display, (ix, iy), (x, y), (0, 0, 255), 5)
ix, iy = x, y
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.line(mask, (ix, iy), (x, y), 255, 5)
cv2.line(img_display, (ix, iy), (x, y), (0, 0, 255), 5)
# 读取图像
img_path = input("请输入图像路径: ")
img = cv2.imread(img_path)
if img is None:
print("无法读取图像")
return
# 创建显示图像和掩码
img_display = img.copy()
mask = np.zeros(img.shape[:2], dtype=np.uint8)
# 创建窗口并设置鼠标回调
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_mask)
# 主循环
while True:
cv2.imshow('image', img_display)
k = cv2.waitKey(1) & 0xFF
if k == ord('c'): # 清除所有标记
mask = np.zeros(img.shape[:2], dtype=np.uint8)
img_display = img.copy()
elif k == ord('r'): # 执行修复
result = cv2.inpaint(img, mask, 5, cv2.INPAINT_TELEA)
cv2.imshow('result', result)
elif k == ord('s'): # 保存结果
if 'result' in locals():
output_path = input("请输入保存路径: ")
cv2.imwrite(output_path, result)
print("结果已保存")
else:
print("请先执行修复")
elif k == ord('q'): # 退出
break
cv2.destroyAllWindows() 4. 基于笔迹特征的区分方法
这种方法尝试利用手写字迹和印刷体在形态特征上的差异:
import cv2
import numpy as np
def remove_handwriting_by_features(image_path, output_path):
"""
尝试通过笔迹特征区分手写和印刷文字
注意:此方法效果有限,需要根据实际图像调整参数
"""
# 读取图像
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 连通域分析
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary, connectivity=8)
# 创建掩码
mask = np.zeros_like(gray)
# 分析每个连通域的特征
for i in range(1, num_labels): # 跳过背景(标签0)
area = stats[i, cv2.CC_STAT_AREA]
width = stats[i, cv2.CC_STAT_WIDTH]
height = stats[i, cv2.CC_STAT_HEIGHT]
# 计算宽高比
aspect_ratio = width / height if height > 0 else 0
# 计算密度(填充率)
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
component = binary[y:y+height, x:x+width]
density = np.sum(component) / (width * height * 255) if width * height > 0 else 0
# 根据特征判断是否为手写字迹
# 这些阈值需要根据实际图像调整
is_handwriting = False
# 手写字迹通常比印刷体更不规则
if (area > 50 and area < 2000 and # 面积范围
(aspect_ratio < 0.5 or aspect_ratio > 2.0) and # 宽高比不均匀
density < 0.5): # 密度较低
is_handwriting = True
# 将可能的手写字迹添加到掩码
if is_handwriting:
component_mask = np.zeros_like(gray)
component_mask[labels == i] = 255
mask = cv2.bitwise_or(mask, component_mask)
# 形态学操作优化掩码
kernel = np.ones((3, 3), np.uint8)
mask = cv2.dilate(mask, kernel, iterations=2)
# 图像修复
result = cv2.inpaint(img, mask, 5, cv2.INPAINT_TELEA)
# 保存结果
cv2.imwrite(output_path, result)
print("处理完成,结果已保存到", output_path) 注意事项
深度学习方法需要安装额外的库(如lama-cleaner)和模型,并且可能需要GPU加速。OCR方法需要安装Tesseract OCR引擎,Windows用户需要设置正确的路径。交互式工具是最可靠的方法,但需要手动操作,适合处理少量图像。基于特征的方法参数需要根据实际图像多次调整,效果因图像而异。对于重要文档,建议先在副本上测试,确保效果满意后再处理原始图像。所有自动方法都可能存在误判,特别是当手写字迹与印刷文字重叠时。
无论采用哪种方法,处理黑色手写字迹都比处理彩色字迹更具挑战性,可能需要结合多种方法才能获得满意的效果。
网友回复


