Ask Your Question

Cannot rotate warpAffine character in license plate?

asked 2016-06-08 01:52:32 -0500

Dinh Thap gravatar image

updated 2016-06-08 02:30:44 -0500

I warpAffine license plate before segmentation but cannot correct incline character.

            opencv_core.RotatedRect rr = opencv_imgproc.minAreaRect(currentPlate.countor);
            opencv_core.Point[] pt = ImageOpenCvUtils.getVerticesPoint(rr);
            for (int k = 0; k < 4; k++) {
                opencv_imgproc.line(input, pt[k], pt[(k + 1) % 4], opencv_core.Scalar.RED);
            double angle = rr.angle();
            if (angle < -45) {
                angle += 90;

            opencv_core.Mat rot_mat = opencv_imgproc.getRotationMatrix2D(, angle, 1);
            opencv_core.Mat rotated = new opencv_core.Mat();
            opencv_imgproc.warpAffine(clone, rotated, rot_mat, input.size());
            opencv_core.Size size = null;
            if (rr.angle() < -45) {
                size = new opencv_core.Size(Math.round(rr.size().height()), Math.round(rr.size().width()));
            } else {
                size = new opencv_core.Size((int) rr.size().width(), (int) rr.size().height());

            opencv_imgproc.rectangle(clone, rr.boundingRect(), opencv_core.Scalar.BLUE);

            opencv_core.Mat subImage = new opencv_core.Mat();
            opencv_imgproc.getRectSubPix(rotated, size,, subImage);


Can you help me? Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-06-08 09:40:41 -0500

kbarni gravatar image

updated 2016-06-08 11:03:57 -0500

The license plate in the image is not only rotated but also skewed.

The skewing transformation matrix has the following form

[ 1  sy  0 ]
[ sx  1  0 ]

In your case, sy=-0.3, sx=0 (approximative value). You have to transform the rotated image with this matrix, or multiply the rotation and the skewing matrix and use warpAffine with the combined matrix.

To get a robust solution, I suggest to detect the four corners of the license plate and determine directly the affine transformation matrix using the findHomography function.

[Update] The unskewing operation should be something like (untested code):

Mat skew_mat = Mat::eye(2,3,CV_64F);<double>(0,1)=-0.3;<double>(1,0)=0;
Mat skewed;
warpAffine(rotated, skewed, skew_mat, rotated.size());

For the second idea, there are a lots of tutorials on the web: e.g. this one:

edit flag offensive delete link more


How do you find out sx, sy value?Can you show me some code. That make me easy to understand. Thanks.

Dinh Thap gravatar imageDinh Thap ( 2016-06-08 10:44:55 -0500 )edit

That's great! I apply your matrix then get good result. Can you show me the code calculated sx,sy value? Thanks.

Dinh Thap gravatar imageDinh Thap ( 2016-06-08 11:06:56 -0500 )edit

Updated my answer with some code and a tutorial.

kbarni gravatar imagekbarni ( 2016-06-08 11:08:37 -0500 )edit

Thank you so much!

Dinh Thap gravatar imageDinh Thap ( 2016-06-08 11:14:20 -0500 )edit

I multiplied the rotation and the skewing matrix then I have bad result. If I do rotation first and skewed after that then I have good result. So How can I combined 2 matrix exactly?

Dinh Thap gravatar imageDinh Thap ( 2016-06-12 03:38:07 -0500 )edit

Matrix multiplication is not commutative! You have to follow the order of transformations when computing the final matrix. This is true when combining any kind of transformations.

Skew_mat*Rot_mat != Rot_mat*Skew_mat
kbarni gravatar imagekbarni ( 2016-06-13 02:48:17 -0500 )edit

Question Tools

1 follower


Asked: 2016-06-08 01:52:32 -0500

Seen: 347 times

Last updated: Jun 08 '16