In this tutorial, I will explain the different text skew correction methods. Text skew correction is the basic image pre-processing step of Optical Character Recognition(OCR) and is mostly used for text-based image Alignment. In most cases, the text is not aligned horizontally in the image, so it needs to be aligned properly to get accurate text output from the OCR engine. A few skew correction strategies are:
- Calculating the skewed angle of text lines using the Probabilistic Houghline transform, which I have explained here.
- Finding a minimum rotated rectangle that contains the entire text region and rotating inversely.
In this tutorial, I will explain the second method to align the image using text skewness correction.
Let’s read the image, convert the image foreground to white and background to black, and apply otsu thresholding.
image = cv2.imread("image.jpeg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray) # flip image background
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
Now, we need to grab the (x, y) coordinates of all pixel values greater than zero, and then use these coordinates to compute a rotated bounding box containing all coordinates.
coords = np.column_stack(np.where(thresh > 0)) # (x,y) coordinates of pixels > 0
angle = cv2.minAreaRect(coords)[-1] # angle of minimum rotated bounding box
cv2.minAreaRectfunction returns values in the range [-90, 0). We need to add 90 degrees to the angle because as the rectangle rotates clockwise the returned angle tends to 0.
if angle < -45:
angle = -(90 + angle)
# otherwise, just take the inverse of the angle to make
angle = -angle
Now rotate the image by calculating RotationMatrix and applying warp affine transform to correct the skewness of the image.
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0) # get rotation matrix
rotated_image = cv2.warpAffine(image, M, (w, h),
This is our final
I hope that this will help you to correct the skewness of your document. Thank you!