Python + opencv implementation of rotating text correction

Time:2021-5-1

Suppose we have an image in which the text is rotated at an unknown angle. In order to correct the angle of the text, we need to complete the following steps:

1. Detect the text range in the graph

2. Calculate the angle at which the text is rotated

3. Rotate the image at a specific angle

The first step is to read the image and do binary processing

#Read the image, do binary processing
img = cv.imread('img/imageTextR.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray)
#The pixels are reversed to white and black
# gray = cv.bitwise_not(gray)
ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
cv.imshow('thresh', thresh)

The second step is to calculate the minimum border containing the rotated text

#Calculates the minimum border that contains rotated text
coords = np.column_stack(np.where(thresh > 0))
print(coords)
#This function gives the rectangle border containing the whole text area, and the rotation angle of this border is the same as that of the text in the figure
angle = cv.minAreaRect(coords)[-1]
print(angle)

We pass these coordinates to the function cv. Minarearect, which will give a rectangular border containing the entire text area. The rotation angle of this border is the same as that of the text in the figure.

The third step is to adjust the angle

#Adjust the angle
if angle < -45:
  angle = -(90+ angle)
else:
  angle = -angle

When the angle is less than – 45, add 90 degrees to the original angle, and when the angle is greater than – 45 degrees, reverse the angle directly.

The fourth step is to execute affine transformation

#Affine transformation
h, w = img.shape[:2]
center = (w//2, h//2)
print(angle)
M = cv.getRotationMatrix2D(center, angle, 1.0)
rotated = cv.warpAffine(img, M, (w, h), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REPLICATE)
cv.putText(rotated, 'Angle: {:.2f} degrees'.format(angle), (10, 30), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

print('[INFO] angel :{:.3f}'.format(angle))
cv.imshow('Input', img)
cv.imshow('Rotated', rotated)

The above Python + opencv rotation text correction method is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developer more.