# Image rotation using AR tags

**URL:** <https://forum.opencv.org/t/image-rotation-using-ar-tags/17996>\
**Category:** Android/Java\
**Created:** [June 18, 2024, 5:31pm UTC](https://forum.opencv.org/t/image-rotation-using-ar-tags/17996 "2024-06-18T17:31:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![user4578](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/user4578/32/10661_2.png) [@user4578](https://forum.opencv.org/u/user4578)\
**Post date:** [June 18, 2024, 5:31pm UTC](https://forum.opencv.org/t/image-rotation-using-ar-tags/17996/1 "2024-06-18T17:31:57Z")

</div>

I am trying to rotate an image with reference to an AR tag. I need the AR tag to be in the top right corner of the white box and the full image to be rotated accordingly like the image below.

 ![image2 (2)](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0e584bb5e1907224fc600e452eb9c3f86ca2e49d.jpeg)

protected Mat processImage(Mat image){  
//detect AR  
Dictionary dictionary = Aruco.getPredefinedDictionary(Aruco.DICT\_5X5\_250);  
List corners = new ArrayList\<\>();  
Mat markerIds = new Mat();  
Aruco.detectMarkers(image, dictionary, corners, markerIds);

```
    //Get camera matrix
    Mat cameraMatrix = new Mat(3,3, CvType.CV_64F);
    cameraMatrix.put(0,0,api.getNavCamIntrinsics()[0]);
    Mat cameraCoefficients = new Mat(1,5, CvType.CV_64F);
    cameraCoefficients.put(0,0,api.getNavCamIntrinsics()[1]);
    cameraCoefficients.convertTo(cameraCoefficients,CvType.CV_64F);

    //Undistort image
    Mat undistortedImage = new Mat();
    Calib3d.undistort(image, undistortedImage, cameraMatrix, cameraCoefficients);

    if(!markerIds.empty()){
        // Get the first detected marker's corners
        MatOfPoint2f markerCorners = new MatOfPoint2f(corners.get(0).clone());

        // Top-left and top-right corners of the marker
        org.opencv.core.Point[] cornerPoints = markerCorners.toArray();
        org.opencv.core.Point topLeft = cornerPoints[0];
        org.opencv.core.Point topRight = cornerPoints[1];

        // Calculate the angle to rotate
        double deltaX = topRight.x - topLeft.x;
        double deltaY = topRight.y - topLeft.y;
        double angle = Math.toDegrees(Math.atan2(deltaY, deltaX));

        // Print the angle
        Log.i(TAG, "Rotation angle: " + angle);

        // Rotate the image
        Mat rotatedImage = rotateImage(undistortedImage, angle);
        return rotatedImage;
    }
    Log.i(TAG, "no AR tag");
    return undistortedImage;
}

private Mat rotateImage(Mat image, double angle) {
    // Get the image center
    org.opencv.core.Point center = new org.opencv.core.Point(image.width() / 2.0, image.height() / 2.0);

    // Get the rotation matrix
    Mat rotationMatrix = Imgproc.getRotationMatrix2D(center, angle, 1.0);

    // Determine the bounding box size after rotation
    Rect bbox = new RotatedRect(center, image.size(), angle).boundingRect();

    // Adjust the rotation matrix to take into account translation
    rotationMatrix.put(0, 2, rotationMatrix.get(0, 2)[0] + bbox.width / 2.0 - center.x);
    rotationMatrix.put(1, 2, rotationMatrix.get(1, 2)[0] + bbox.height / 2.0 - center.y);

    // Rotate the image
    Mat rotatedImage = new Mat();
    Imgproc.warpAffine(image, rotatedImage, rotationMatrix, bbox.size());

    return rotatedImage;
}

```

This code sometimes works (like the image above) but there are images that are very far off.  
How can I fix this issue?
