# Detect image orientation angle based on text direction

Here’s an approach based on the assumption that the majority of the text is skewed onto one side. The idea is that we can determine the angle based on the where the major text region is located

• Load the image, convert to
grayscale, and Gaussian blur
• Adaptive threshold to get a binary image
• Find contours and filter using contour area
• Draw filtered contours onto mask
• Split image horizontally or vertically based on orientation
• Count number of pixels in each half

After converting to grayscale and Gaussian blurring, we adaptive threshold to obtain a binary image

From here we find contours and filter using contour area to remove the small noise particles and the large border. We draw any contours that pass this filter onto a mask

To determine the angle, we split the image in half based on the image’s dimension. If `width > height` then it must be a horizontal image so we split in half vertically. if `height > width` then it must be a vertical image so we split in half horizontally

Now that we have two halves, we can use `cv2.countNonZero()` to determine the amount of white pixels on each half. Here’s the logic to determine angle:

``````if horizontal
if left >= right
degree -> 0
else
degree -> 180
if vertical
if top >= bottom
degree -> 270
else
degree -> 90
``````

left 9703

right 3975

Therefore the image is 0 degrees. Here’s the results from other orientations

left 3975

right 9703

We can conclude that the image is flipped 180 degrees

Here’s results for vertical image. Note since its a vertical image, we split horizontally

top 3947

bottom 9550

Therefore the result is 90 degrees

``````import cv2
import numpy as np

def detect_angle(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
area = cv2.contourArea(c)
if area < 45000 and area > 20:

# Horizontal
if w > h:
left_pixels = cv2.countNonZero(left)
right_pixels = cv2.countNonZero(right)
return 0 if left_pixels >= right_pixels else 180
# Vertical
else: