Deskewing document is one of the essential preprcessing step before OCR and HTR. This repo contain proposed method for document deskewing mainly via Fourier Transform.
- Fourier Transform
- Non Local Means Denoising
- OpenCV
- Scikit-image
- Numpy
Change image to gray format
Morphological operations
Fast Fourier Transform
FNlM Denoising and thresholding of FFT output
Determines the peaks
Calculate angle using slope of drawing line
This method works well if angle of skew is between -60 and 60 degrees*
from deskew_fft import RotateDoc
from skimage.transform import rotate
import skimage.io as io
import matplotlib.pyplot as plt
path = 'test_image_1.jpg'
rotate_doc = RotateDoc(
img_path=path,
visualize=1,
resize_ratio=2,
peak_top_bottom=True,
synth=False
)
image = io.imread(path)
ag = rotate_doc.deskewImage()
rotated_image = rotate(image,ag)
if rotate_doc.visualize:
plt.imshow(rotated_image)
plt.show()
In this test document manually rotated from -60 to 60 degree and check performance of methdod.
Rotate -60 degree | Deskew 63 degree |
---|---|
Rotate -50 degree | Deskew 53 degree |
---|---|
Rotate -40 degree | Deskew 46 degree |
---|---|
Rotate -30 degree | Deskew 35 degree |
---|---|
Rotate -20 degree | Deskew 25 degree |
---|---|
Rotate -10 degree | Deskew 12 degree |
---|---|
Rotate 10 degree | Deskew -12 degree |
---|---|
Rotate 20 degree | Deskew -24 degree |
---|---|
Rotate 30 degree | Deskew -35 degree |
---|---|
Rotate 40 degree | Deskew -47 degree |
---|---|
Rotate 50 degree | Deskew -55 degree |
---|---|
Rotate 60 degree | Deskew -65 degree |
---|---|
from deskew_fft import RotateDoc
from skimage.transform import rotate
import skimage.io as io
import matplotlib.pyplot as plt
path = 'test_image_1.jpg'
for angle in range(-60,70,10):
rotated_image = rotate(io.imread(path),angle)
rotate_doc = RotateDoc(
img_path=path,
visualize=1,
resize_ratio=2,
peak_top_bottom=True,
synth=True
)
ag = rotate_doc.deskewImage(angle=angle)
rotated_image = rotate(rotated_image,ag)
if rotate_doc.visualize:
plt.imshow(rotated_image)
plt.show()
print('-'*100)