Ask Your Question

Revision history [back]

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