Detect angle and rotate an image in Python [closed]

You could use OpenCV with HoughLines to detect lines in the image. The angle of each of the lines can be found from this:

import numpy as np
import cv2
import math
from scipy import ndimage

img_before = cv2.imread('rotate_me.png')

cv2.imshow("Before", img_before)    
key = cv2.waitKey(0)

img_gray = cv2.cvtColor(img_before, cv2.COLOR_BGR2GRAY)
img_edges = cv2.Canny(img_gray, 100, 100, apertureSize=3)
lines = cv2.HoughLinesP(img_edges, 1, math.pi / 180.0, 100, minLineLength=100, maxLineGap=5)

angles = []

for [[x1, y1, x2, y2]] in lines:
    cv2.line(img_before, (x1, y1), (x2, y2), (255, 0, 0), 3)
    angle = math.degrees(math.atan2(y2 - y1, x2 - x1))
    angles.append(angle)

cv2.imshow("Detected lines", img_before)    
key = cv2.waitKey(0)

median_angle = np.median(angles)
img_rotated = ndimage.rotate(img_before, median_angle)

print(f"Angle is {median_angle:.04f}")
cv2.imwrite('rotated.jpg', img_rotated)  

This would give you an output as:

rotated image

It shows the lines that were detected to rotate. The angle calculated is:

Angle is 3.9793

statistics.median() could also be used instead of the numpy version if you are using Python 3.4 or later.

Note: angles holds the list of angles found which could be both vertical or horizontal. Depending on the image, you might find it better to use a different method to decide on which angle to use to rotate the image with. For example you could filter only angles which fall within a certain range before using the median function on them.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)