Skip to content

Instantly share code, notes, and snippets.

@zabir-nabil
Created March 1, 2020 06:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabir-nabil/dfb78f584947ebdb9f29d39c9737b5c6 to your computer and use it in GitHub Desktop.
Save zabir-nabil/dfb78f584947ebdb9f29d39c9737b5c6 to your computer and use it in GitHub Desktop.
compute skew angle and deskew in python (opencv)
import numpy as np
import math
import cv2
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
def compute_skew(src_img):
if len(src_img.shape) == 3:
h, w, _ = src_img.shape
elif len(src_img.shape) == 2:
h, w = src_img.shape
else:
print('upsupported image type')
img = cv2.medianBlur(src_img, 3)
edges = cv2.Canny(img, threshold1 = 30, threshold2 = 100, apertureSize = 3, L2gradient = True)
lines = cv2.HoughLinesP(edges, 1, math.pi/180, 30, minLineLength=w / 4.0, maxLineGap=h/4.0)
angle = 0.0
nlines = lines.size
#print(nlines)
cnt = 0
for x1, y1, x2, y2 in lines[0]:
ang = np.arctan2(y2 - y1, x2 - x1)
#print(ang)
if math.fabs(ang) <= 30: # excluding extreme rotations
angle += ang
cnt += 1
if cnt == 0:
return 0.0
return (angle / cnt)*180/math.pi
def deskew(src_img):
return rotate_image(src_img, compute_skew(src_img))
if __name__ == '__main__':
import cv2
img = cv2.imread('test.png')
corrected_img = deskew(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment