1

I wrote an Android application using Opencv , my image processing algorithm needs a correct rotation for my detected rectangle, so as a beginning of the process, I

  1. detect the biggest rectangle as a RotatedRect.
  2. get the rotation angle and the center of the rect.
  3. create a rotation matrix with getRotationMatrix2D
  4. perform the affine transformation using warpAffine
  5. Extract the detected rectangle using getRectSubPix

The hole process is working fine, but I have a problem with the object skew.

When the object is rotated counterclockwise,the skew correction is working great, but if it's rotated clockwise the rectangle is rotated 90 degrees.

I need to mention that because the Opencv camera have a wrong orientation, I add -90 to the rectangle rotation angle at first.

Here is my code

         /* *******************************************************************************************
         * get angle and size from the rotated rect
         *******************************************************************************************/
        double rect_angle = rbox.angle - 90.0;
        Size rect_size = rbox.size;

        /* *******************************************************************************************
         * correct the orientation 
         *******************************************************************************************/
        double d = rect_size.width;
        rect_size.width = rect_size.height;
        rect_size.height = d;

        M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);
        Imgproc.warpAffine(origMat, rotated, M, origMat.size());

        /* *******************************************************************************************
         * crop the resulting image
         *******************************************************************************************/
        if (rect_size.width > 75 && rect_size.height > 75)
            Imgproc.getRectSubPix(rotated, new Size(rect_size.width - 75, rect_size.height - 75), rbox.center, rotated);

        /* *******************************************************************************************
         * resize the result image because rotated has the size of the rect not the original image
         * which cause the preview camera to be black because of wrong dimensions
         *******************************************************************************************/
        Imgproc.resize(rotated, rotated, origMat.size());

Here is a result image when the object is rotated clockwise

enter image description here

And here is the result image with object rotated counterclockwise

enter image description here

And here is the original image

enter image description here

I should mention that when the object isn't rotated the angle is -90 and if it's rotated clockwise the angle tends to -179 and if it's rotated counterclockwise the angle become -179 and tends to -90.

I tried to set condition like this

if (rbox.angle < -90.0) {
  rect_angle -= 90.0;
}

But nothing is working.

I know it's a matter of mathematics but I can't find a way to achieve it, I wish you guys can help me solve this problem.

0

1 Answer 1

0

For everyone concerned about the answer, I found a solution.

I worked on the width and height of the detected rectangle instead of the angle.

So before creating the rotation matrix i added this test code

if (rbox.size.width < rbox.size.height) {
         rect_angle += 90.0;
         double d1 = rect_size.height;
         rect_size.height = rect_size.width;
         rect_size.width = d1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.