图片方向矫正

本文探讨了利用Radon变换进行文本行倾斜角检测的技术,通过实例展示了如何使用Python库实现图像的几何校正,并对比了DocUNet和DewarpNet在文档扭曲矫正方面的效果。同时,文章介绍了Learning from Documents in the Wild提升文档校正的方法,以及Geometric Representation Learning for Document Image Rectification的模型应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 基于Radon变换

1.1 radon原理

二维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)δ(pxcosθ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变换可以计算出该二值图片在每个方向上每条直线的值,由上面的公式可以知道如果在这条直线上的文字越多其值越大,因此最大值所在的角度即是文字的倾斜角度。

1.2 代码实现

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()

1.3 效果

2 基于DewarpNet和DocUNet

2.1 论文调研

经过调研发现,TextIn的切边增强和弯曲矫正技术很强,他的技术参考的论文为DocUNetDewarpNet
DocUNet的代码地址:https://github.com/teresasun/docUnet.pytorch。
DewarpNet的代码地址:https://github.com/cvlab-stonybrook/DewarpNet。

2.2 论文效果

DocUNet:
在这里插入图片描述
DewarpNet:
在这里插入图片描述

2.3 测试效果

DocUNet提供的代码中没有训练好的模型,需要自己手动训练且从论文中可以看出DewarpNet的效果优于DocUNet,所以只对DewarpNet进行了测试。

test1:

test2:

test3:

test4:

test5:

2.4 OCR识别效果

test1:

test2:

test3:

test4:

test5:

3 Learning From Documents in the Wild to Improve Document Unwarping

论文地址:https://dl.acm.org/doi/abs/10.1145/3528233.3530756
代码地址:https://github.com/cvlab-stonybrook/PaperEdge
该模型分为两部分:大小都为139M。

3.1 模型效果

4 Geometric Representation Learning for Document Image Rectification

论文地址:https://arxiv.org/pdf/2210.08161.pdf
代码地址:https://github.com/fh2019ustc/DocGeoNet
该模型分为两部分:预处理模型为4M,另一部分为90M。

4.1 模型效果

5 opencv边缘检测

  1. 利用opencv提取文档轮廓
  2. 画出最小外接矩阵

5.1 效果测试

test1:图片有明显边界
在这里插入图片描述
test2:图片没有明显边界
在这里插入图片描述

6 Document_Scanner

6.1 基于几何矫正

在这里插入图片描述
在这里插入图片描述

6.2 基于深度学习:Semantic Segmentation using PyTorch-DeepLabV3

6.2.1 矫正效果

test1:文档有完整边界
在这里插入图片描述

test1:文档没有完整边界
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值