使用 Julia 进行验证码图像扭曲校正与识别
为了防止机器识别,很多验证码采用波形、弯曲或仿射扭曲的图像变换技术。字符看似完整但被“拧”成不同的形状,造成 OCR 识别失败。本文将介绍如何通过 图像形态重建 + 几何逆变换 技术,在 Julia 中对这类验证码进行校正并完成字符识别。
一、环境准备
using Pkg
Pkg.add(["Images", "ImageIO", "ImageTransformations", "ImageFiltering", "Tesseract"])
二、加载并预处理图像
更多内容访问ttocr.com或联系1436423940
using Images, ImageIO
img = load("captcha_distorted.png")
gray = Gray.(img)
binary = map(x -> x > 0.6 ? 0.0 : 1.0, gray)
save("binary.png", binary)
三、使用中值滤波平滑波动(可缓解轻微扭曲)
using ImageFiltering
smoothed = imfilter(binary, Kernel.mean((3, 3)))
save("smoothed.png", smoothed)
四、仿射校正示例(手动校准坐标点)
这里我们假设已知原图和目标图四个角的对应关系:
using ImageTransformations
原图中扭曲区域的四个顶点
src_pts = [(30, 10), (120, 5), (25, 50), (125, 55)]
目标图中字符应位于的正方形区域
dst_pts = [(0, 0), (100, 0), (0, 40), (100, 40)]
计算仿射矩阵
tform = mapslices(x -> inv(hcat(src_pts...)) * x, hcat(dst_pts...) )
warp_mat = hcat(tform[:, 1:2], [0.0, 0.0]) # 添加偏移
应用逆仿射变换
corrected = warp_affine(smoothed, warp_mat, axes=(0:39, 0:99))
save("corrected.png", corrected)
五、OCR 识别校正后的图像
using Tesseract
ocr = TesseractOcr("eng")
set_image(ocr, "corrected.png")
text = strip(get_text(ocr))
println("识别出的验证码:", text)
浙公网安备 33010602011771号