Ask Your Question

How to determine angular rotation in appropriate direction

asked 2016-07-13 17:47:27 -0600

asa gravatar image


I have this image scanning setup where I am scanning printed papers under a camera. I have this prior information that, when the paper passes under the camera it can rotate upto maximum 5 degrees in either clock wise or counter clock wise direction. I want to determine the value of the rotation angle in correct direction and then rotate the image back to make it zero degrees. My question is how can I determine the amount of rotation and the correct direction?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-14 07:43:12 -0600

Tetragramm gravatar image

Can I assume you can detect the edges of the paper? Yes? Good, otherwise you have more trouble than the rotation.

Since it's only up to 5 degrees, you know which edge is the top edge. You know the length of the top and bottom edge, which for now call width. You know the length of the left and right edges, which for now call height. You need to find the transform that makes this into a width by height rectangle. Fortunately, OpenCV provides a function for calculating the transform from one set of points to another: estimateRigidTransform. Finally, de-rotate it by calling warpAffine, which does the actual transforming of the image.

As always with my code, I may have swapped an x and y or width and height somewhere here, so don't just blindly copy.

std::vector<cv::Point2f> paperCorners; //Fill with the corners of the paper, in order TL, TR, BL, BR
std::vector<cv::Point2f> correctedCorners;
correctedCorners.push_back(cv::Point2f(width, height));//BR

cv::Mat affine = cv::estimateRigidTransform(paperCorners, correctedCorners, true);
//Setting true also corrects skew, which might be necessary.

cv::warpAffine(warpedScan, correctedScan, affine, cv::Size(width, height));
edit flag offensive delete link more


Thanks! It works great. There is another thing, the ultimate focus of the scanning system is to read and decode a certain type barcode like pattern on the paper. The paper size can be huge, so after scanning the system extracts a smaller sub image (ROI) where the barcode should be. The position of the code on each paper is fairly static (it's usually near the top left corner). So, I will be getting only the ROI ( apprximately 500px x 1000px). Now the same issue as above. I can see some ROIs are roated within +-5 degrees. I mean the code bars look tilted. How can I rotate the ROI? I am thinking of using corner detectors and use your idea to rotate the tilted images back to zero degrees. If this is correct how do I get the reference corner points (correctedCorners) ?

asa gravatar imageasa ( 2016-07-14 09:57:24 -0600 )edit

The ROI can have additional printed info (say characters) other than the code pattern. The code bars have a specific height and width, so I want to be as accurate as possible to measure the bars before further processing. hence I want to correct for the tilting / rotation. I extract contours and then extract rectangles and filter them based on size specification. The approximation of rectangles are more accurate when the image is not tilted than when it is, hence the angle correction. Thanks

asa gravatar imageasa ( 2016-07-14 10:02:21 -0600 )edit

I don't entirely understand the question. You say the width and height, and you already detect corners so... What exactly do you need?

Make a new topic, and include a sample image. Although I suggest trying some things first. It's always better to say "I've tried these things and I'm having this problem" than to say "Help me plz"

Tetragramm gravatar imageTetragramm ( 2016-07-14 18:05:44 -0600 )edit

Question Tools

1 follower


Asked: 2016-07-13 17:47:27 -0600

Seen: 909 times

Last updated: Jul 14 '16