How to rotate an image of a skewed rectangle?

117 Views Asked by At

I am creating a python program that can analyze an image of a circuit board and identify parts of the image that have a label on them:

The green text under the labels is what the program is attempting to read from the label. Obviously, it is incorrect due to the label being upside-down or askew. The text is being read by passing the segmented image into pytesseract.image_to_string. In order to help the program rotate the image, I added a black border to the label:

My question is, how can I align the segmented image so that the rectangle is oriented correctly? Is this even the right way to go about it? Or would a fiducial work better? I am open to any suggestions that can help me achieve my goal of reading text off these labels. Thank you!

1

There are 1 best solutions below

2
Tiger Ros On

If you know where the corners of the rectangle are, you can use math.atan2() to get the angle you need to rotate the image, eg:

import math
#TL=(1,2) is the top left corner (arbitrary units)
#TR=(3,4) is the top right corner
rot = -math.atan2(TL[1]-TR[1],TL[0]-TR[0])
rot *= 180/math.pi #if you want degrees instead of radians

If you know the angle, there's a lot of libraries that can rotate the image for you, like Pillow or OpenCV.