Ask Your Question

How to detect multiple rectangles and rotate to vertical position

asked 2014-05-20 07:45:15 -0500

reebston gravatar image

Hi, Here is the picture of what I want to analyse:

I'm going to try to save each of these paper piece to a file, and before I do it, rotate them to vertical position. Is it possible and what should be steps, functions to use it? As you can see in the picture, the pieces are lying on background with other color, that should make it easier to detect them right? Please let me know if my goal is possible. Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-20 23:21:07 -0500

This is what I would roughly do:

I would first use findCountours using the parameter CV_RETR_EXTERNAL to detect only the external contours.

Then I would make sure that each contour contains 4 and only 4 corners (it is a square).

I would calculate the angle of each box respect to a vertical line(using for example the top-left and bottom-left corners of each detected contour) and use that angle to create a transformation matrix using getRotationMatrix2D

And finally use warpAffine and the transformation matrix that you just got to get each detected box aligned

Here are a couple of tutorials that might be useful to you:

finding contours in images

affine transformations

edit flag offensive delete link more

Question Tools


Asked: 2014-05-20 07:45:15 -0500

Seen: 2,074 times

Last updated: May 20 '14