二维Radon变换定义:
将(x,y)平面空间中的一条直线 p = x cos θ + y sin θ p=x\cos\theta+y\sin\theta p=xcosθ+ysinθ映射成Radon空间的一个点 ( p , θ ) (p,\theta) (p,θ),
连续图像的Radon变换为: R ( p , θ ) = ∬ D f ( x , y ) δ ( p − x cos θ − y sin θ ) d x d y R(p,\theta)=\iint_D f(x,y) \delta(p-x\cos\theta-y\sin\theta) \,dx\,dy R(p,θ)=∬Df(x,y)δ(p−xcosθ−ysinθ)dxdy;
其中 δ ( x ) = { 0 x!=0 1 x=0 \delta(x)_=\begin{cases}0& \text{x!=0}\\1& \text{x=0}\end{cases} δ(x)={01x!=0x=0。
通过randon变换可以计算出该二值图片在每个方向上每条直线的值,由上面的公式可以知道如果在这条直线上的文字越多其值越大,因此最大值所在的角度即是文字的倾斜角度。
import cv2
import sys
import time
import numpy as np
from skimage.transform import radon
img = cv2.imread('C:/Users/64975/Desktop/test_image/tt.png')
# h = 900
# ratio = img.shape[0] / h
# w = img.shape[1] / ratio
# orig = img.copy()
# resize_image = cv2.resize(orig, (int(w), h))
I = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
h, w = I.shape
(cX, cY) = (w // 2, h // 2)
if (w > 240):
I = cv2.resize(I, (240, int((h / w) * 240)))
I = I - np.mean(I)
sinogram = radon(I)
r = np.array([np.sqrt(np.mean(np.abs(line) ** 2)) for line in sinogram.transpose()])
rotation = np.argmax(r)
print('Rotation: {:.2f} degrees'.format(90 - rotation))
# Rotate and save with the original resolution
M = cv2.getRotationMatrix2D((w/2, h/2), 90 - rotation, 1)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
dst = cv2.warpAffine(img, M, (nW, nH))
cv2.imwrite('C:/Users/64975/Desktop/test_image/out_image.jpg', dst)
cv2.imwrite('rotated.jpg', dst)
cv2.imshow('dst', dst)
cv2.waitKey()
经过调研发现,TextIn的切边增强和弯曲矫正技术很强,他的技术参考的论文为DocUNet和DewarpNet。
DocUNet的代码地址:https://github.com/teresasun/docUnet.pytorch。
DewarpNet的代码地址:https://github.com/cvlab-stonybrook/DewarpNet。
DocUNet提供的代码中没有训练好的模型,需要自己手动训练且从论文中可以看出DewarpNet的效果优于DocUNet,所以只对DewarpNet进行了测试。
test1:
test2:
test3:
test4:
test5:
test1:
test2:
test3:
test4:
test5:
论文地址:https://dl.acm.org/doi/abs/10.1145/3528233.3530756
代码地址:https://github.com/cvlab-stonybrook/PaperEdge
该模型分为两部分:大小都为139M。
论文地址:https://arxiv.org/pdf/2210.08161.pdf
代码地址:https://github.com/fh2019ustc/DocGeoNet
该模型分为两部分:预处理模型为4M,另一部分为90M。